Skip to content

Commit

Permalink
Initial Release - v1.0.0
Browse files Browse the repository at this point in the history
Link a customer to a guest order in the Magento Admin.
  • Loading branch information
PromInc committed Jan 5, 2016
1 parent 597ddca commit 9dcf360
Show file tree
Hide file tree
Showing 19 changed files with 918 additions and 0 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## 1.0.0 - 2016-01-05
###Initial Release
### Features
- Link a customer to an order which has no customer associated to it (guest order)
- Once linked, the order is connected in the Magento Admin and in the customers order history
- A preview of the customer information and order information are displayed prior to making the link to allow the site administrator verify that the connection is correct
- Automated checks to ensure error free use:
- Verify email address entered belongs to a customer
- Verify that customer found via the email address is regsistered to the selected store
- Verify the store selected matches the store the customer is registered to as well as the store the order came from
- Verify that the order number entered exists
- Verify that the order is not already associated to a customer
- Multiple entry methods
- Manual Entry
- Manually enter store, customer email address and order number.
- Order Link
- From the Edit Order page in the Magento Admin, click the Link Order button to have the order number pre-populated.
- Customer Link
- From the Customer view page in the Magento Admin, click the Link Customer button to have the customers email address and store pre-populated
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Magento 1.x Link Customer to Order

Some customers forget that they have an account with your website and/or forget to login prior to making a purchase. By default, Magento does not offer a method to link the customer to their order if they were to contact you.

The Magento extension allows you to make that connection with the simple click of a button.

## Features
Link a customer to an order which has no customer associated to it (guest order)

Once linked, the order is connected in the Magento Admin and in the customers order history

A preview of the customer information and order information are displayed prior to making the link to allow the site administrator verify that the connection is correct.

Automated checks to ensure error free use:
- Verify email address entered belongs to a customer
- Verify that customer found via the email address is regsistered to the selected store
- Verify the store selected matches the store the customer is registered to as well as the store the order came from
- Verify that the order number entered exists
- Verify that the order is not already associated to a customer

Multiple entry methods
- Manual Entry
- Manually enter store, customer email address and order number.
- Order Link
- From the Edit Order page in the Magento Admin, click the Link Order button to have the order number pre-populated.
- Customer Link
- From the Customer view page in the Magento Admin, click the Link Customer button to have the customers email address and store pre-populated


## Developed and tested on
- Magento 1.6.2

## Installation
- Install the files in the *app* directory into their respective directories on your server.
- Clear the Magento Cache

**As always, it is best practice to test this extension on a development server prior to adding to your live site to ensure there are no errors or conflicts. This code is provided for free and the author of this code is not responsible or liable for any negative effects that may be incurred by using this code for your own purposes.**
26 changes: 26 additions & 0 deletions app/code/local/Prominc/Adminhtml/etc/adminhtml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<config>
<menu>
<promincparentmenu translate="title" module="adminhtml">
<title>PromInc</title>
<sort_order>80</sort_order>
</promincparentmenu>
</menu>

<acl>
<resources>
<all>
<title>Allow Everything</title>
</all>
<admin>
<children>
<promincparentmenu translate="title" module="adminhtml">
<title>Menu Options - PromInc Modules</title>
<sort_order>600</sort_order>
</promincparentmenu>
</children>
</admin>
</resources>
</acl>

</config>
20 changes: 20 additions & 0 deletions app/code/local/Prominc/Adminhtml/etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<config>
<modules>
<Prominc_Adminhtml>
<version>0.1.1</version>
<depends></depends>
</Prominc_Adminhtml>
</modules>

<admin>
<routers>
<adminhtml>
<args>
<modules>
<adminhtml_prominc before="Mage_Adminhtml">Prominc_Adminhtml</adminhtml_prominc>
</modules>
</args>
</adminhtml>
</routers>
</admin>
</config>
19 changes: 19 additions & 0 deletions app/code/local/Prominc/PromincLinkOrder/Block/Adminhtml/Admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
class Prominc_PromincLinkOrder_Block_Adminhtml_Admin extends Mage_Adminhtml_Block_Widget_Grid_Container
{


/**
*
*/
public function __construct()
{
$this->_blockGroup = 'prominclinkorder';
$this->_controller = 'adminhtml_admin';
$this->_headerText = Mage::helper('prominclinkorder')->__('Link Customer to Order');
parent::__construct();
$this->_removeButton('add');
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
class Prominc_PromincLinkOrder_Block_Adminhtml_Admin_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

/**
*
*/
public function __construct() {
parent::__construct();
$this->setId('prominclinkorderAdminhtmlAdminGrid');
$this->setTemplate('prominclinkorder/admin/find.phtml');
$this->setData('store', Mage::registry('store') )
->setData('email', Mage::registry('email') )
->setData('order_id', Mage::registry('order_id') );
$this->setUseAjax(false);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php
class Prominc_PromincLinkOrder_Block_Adminhtml_Admin_Preview extends Mage_Adminhtml_Block_Sales_Items_Abstract
{


protected $_order;


/**
*
*/
public function __construct() {
parent::__construct();
$this->setId('prominclinkorderAdminhtmlAdminPreview');

if( Mage::registry('order') ) {
$_order = $this->getOrder( Mage::registry('order') );
}

$this->addItemRender("default", "adminhtml/sales_order_view_items_renderer_default", "sales/order/view/items/renderer/default.phtml");

$this->setTemplate('prominclinkorder/admin/preview.phtml')
->setData('store', Mage::registry('store') )
->setData('email', Mage::registry('email') )
->setData('order_id', Mage::registry('order_id') )
->setData('saved', Mage::registry('saved') )
;
}


/**
* Retrieve order items collection
*
* @return unknown
*/
public function getItemsCollection()
{
return $this->getOrder()->getItemsCollection();
}


/**
* Get customer object
*/
public function getCustomer() {
return Mage::getModel("customer/customer")
->setWebsiteId( $this->getData( 'store' ) )
->loadByEmail( $this->getData('email') );
}


/**
* Get customer object by ID
*
* @param mixed $id
* @return Mage_Core_Model_Abstract
*/
public function getCustomerById($id) {
return Mage::getModel('customer/customer')->load($id);;
}


/**
* Get customer group name by customer group ID number
*
* @param mixed $id
*/
public function getGroupName($id) {
if( $id ) {
return Mage::getModel('customer/group')
->load($id)
->getCustomerGroupCode();
}
}


/**
* Get store name by store ID
*
* @param int $id
*/
public function getStoreName($id) {
if( $id ) {
return Mage::getModel('core/store')->load($id)->getName();
}
}


/**
* Get customer address HTML output address ID
*
* @param mixed $addressId
* @param mixed $format (billing, shipping)
*/
public function getAddressHtml( $addressId, $format="billing" ) {
$address = Mage::getModel('customer/address')->load( $addressId )->getData();

$addressHtml = '<address class="box-right">' .
'<strong>Default ' . ucfirst( $format ) . ' Address</strong><br>' .
$address['firstname'] . ' ' . $address['lastname'] . '<br>' .
$address['street'] . '<br>' .
$address['city'] . ', ' . $address['region'] . ', ' . $address['postcode'] . ' ' . $address['country_id'] . '<br>' .
'T: ' . $address['telephone'] . '<br>' .
'</address>';

return $addressHtml;
}


/**
* Check if a link between the customer and order can be made.
*
* Based on various criteria, a link may not be allowed,
* an appropriate message will be returned in that case.
*
* If a link can be made, a button is returned to allow the
* user to complete the link process.
*
* @Retrun array
* canLink bool If a link can be made or not
* explanation array HTML strings with error or success messages
*/
public function canMakeLink() {
$canLink = array( 'canLink' => NULL, 'explanation' => array() );
$customer = $this->getCustomer();
$order = $this->getOrder();

if( $customer->getIsGuest() == 1 ) {
/* Error Checking | If customer has account */
$accountURI = 'customer/account/login/';
$accountUrl = Mage::app()->getStore($customer->getStoreId())->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK, true).$accountURI;
$canLink['canLink'] = false;
$canLink['explanation'][] = '<p>The customer is a <b>Guest</b> and thus does not have an account.</p><p>Linking an order to a <b>Guest</b> is not possible. The customer will need to create an account proior to making this link.</p><p>Ask the customer to create an account first by visiting this page: <a href="'.$accountUrl.'" target="_blank">'.$accountUrl.'</a></p>';
}

if( $customer->getIsGuest() != 1 && $order->getCustomerId() ) {
/* Error Checking | Order is already linked to a customer */
$existingCustomer = $this->getCustomerById( $order->getCustomerId() );
$existingOrderUrl = Mage::helper('adminhtml')->getUrl('adminhtml/sales_order/view/order_id/'.$order->getEntityId());
$existingCustomerUrl = Mage::helper('adminhtml')->getUrl('adminhtml/customer/edit/id/'.$order->getCustomerId());
$canLink['canLink'] = false;
$canLink['explanation'][] = '<p>This <a href="'.$existingOrderUrl.'" target="_blank">order</a> is already linked to a customer.</p><p><b>Currently Linked Customer:</b><br><a href="'.$existingCustomerUrl.'" target="_blank">'.$existingCustomer->getFirstname().' '.$existingCustomer->getLastname().' (Customer ID:'.$existingCustomer->getEntityId().')</a></p>';
}

if( $customer->getIsGuest() != 1 && $customer->getStoreId() != $order->getStoreId() ) {
/* Error Checking | Order Store and Customer Store Do Not Match */
$searchForLinkUrl = Mage::helper('adminhtml')->getUrl('*/prominclinkorder_prominclinkorderadmin/', array('s' => $this->getData('store'), 'e' => $this->getData('email'), 'o' => $this->getData('order')));
$canLink['canLink'] = false;
$canLink['explanation'][] = '<p>The <b>Store</b> for this customer account does not match the store on the order.</p><p>Reset the link parameters to ensure the store for the customer search matches the store of the order.</p><p><a href="'.$searchForLinkUrl.'">Update Search</a></p>';
}

if( is_null( $canLink['canLink'] ) ) {
$makeLinkUrl = Mage::helper('adminhtml')->getUrl('*/prominclinkorder_prominclinkorderadmin/process/', array('cid' => $customer->getEntityId(), 'oid' => $order->getEntityId(), 'c_e' => $customer->getEmail()));
$canLink['canLink'] = true;
$canLink['explanation'][] = '<p>This customer can be linked to this order.</p><p>To finalize processing this link, click the button below.</p><p><a href="'.$makeLinkUrl.'"><span class="form-button">Process Link</span></a></p>';
}

return $canLink;
}


}
4 changes: 4 additions & 0 deletions app/code/local/Prominc/PromincLinkOrder/Helper/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
class Prominc_PromincLinkOrder_Helper_Data extends Mage_Core_Helper_Abstract
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
class Prominc_PromincLinkOrder_Model_Adminhtml_Observer
{


/**
* Add button on Admin Order View page to link this order to a customer
*
* @param mixed $observer
* @return Prominc_PromincLinkOrder_Model_Adminhtml_Observer
*/
public function addButtonLinkToCustomer($observer) {
$block = Mage::app()->getLayout()->getBlock('sales_order_edit');
if (!$block){
return $this;
}
$order = Mage::registry('current_order');
if( $order->getEntityId() ) {
if( ! $order->getCustomerId() ) {
$url = Mage::helper("adminhtml")->getUrl(
"*/prominclinkorder_prominclinkorderadmin",
array('o'=>$order->getIncrementId())
);
$block->addButton('cygtest_resubmit', array(
'label' => Mage::helper('sales')->__('Link to Customer'),
'onclick' => 'setLocation(\'' . $url . '\')',
'class' => 'go'
), 0, 2);
return $this;
}
}
}


/**
* Add button on Admin Customer View page to link this order to an order
*
* @param mixed $observer
* @return Prominc_PromincLinkOrder_Model_Adminhtml_Observer
*/
public function addButtonLinkToOrder($observer) {
$block = Mage::app()->getLayout()->getBlock('customer_edit');
if (!$block){
return $this;
}
$customer = Mage::registry('current_customer');
$url = Mage::helper("adminhtml")->getUrl(
"*/prominclinkorder_prominclinkorderadmin",
array('e'=>$customer->getEmail(), 's'=>$customer->getStoreId())
);
$block->addButton('cygtest_resubmit', array(
'label' => Mage::helper('sales')->__('Link to Order'),
'onclick' => 'setLocation(\'' . $url . '\')',
'class' => 'go'
), 0, 25);
return $this;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
class Prominc_PromincLinkOrder_Model_Mysql4_PromincLinkOrder extends Mage_Core_Model_Mysql4_Abstract
{


protected function _construct() {
$this->_init('prominclinkorder/order', 'entity_id');
}


}
Loading

0 comments on commit 9dcf360

Please sign in to comment.