diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..147ebf4
--- /dev/null
+++ b/CHANGELOG.md
@@ -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
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..ed06b93
--- /dev/null
+++ b/README.md
@@ -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.**
\ No newline at end of file
diff --git a/app/code/local/Prominc/Adminhtml/etc/adminhtml.xml b/app/code/local/Prominc/Adminhtml/etc/adminhtml.xml
new file mode 100644
index 0000000..44f220d
--- /dev/null
+++ b/app/code/local/Prominc/Adminhtml/etc/adminhtml.xml
@@ -0,0 +1,26 @@
+
+
The customer is a Guest and thus does not have an account.
Linking an order to a Guest is not possible. The customer will need to create an account proior to making this link.
Ask the customer to create an account first by visiting this page: '.$accountUrl.'
'; + } + + 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'][] = 'This order is already linked to a customer.
Currently Linked Customer:
'.$existingCustomer->getFirstname().' '.$existingCustomer->getLastname().' (Customer ID:'.$existingCustomer->getEntityId().')
The Store for this customer account does not match the store on the order.
Reset the link parameters to ensure the store for the customer search matches the store of the order.
'; + } + + 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'][] = 'This customer can be linked to this order.
To finalize processing this link, click the button below.
'; + } + + return $canLink; + } + + +} \ No newline at end of file diff --git a/app/code/local/Prominc/PromincLinkOrder/Helper/Data.php b/app/code/local/Prominc/PromincLinkOrder/Helper/Data.php new file mode 100644 index 0000000..859d10e --- /dev/null +++ b/app/code/local/Prominc/PromincLinkOrder/Helper/Data.php @@ -0,0 +1,4 @@ +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; + } + + +} \ No newline at end of file diff --git a/app/code/local/Prominc/PromincLinkOrder/Model/Mysql4/Prominclinkorder.php b/app/code/local/Prominc/PromincLinkOrder/Model/Mysql4/Prominclinkorder.php new file mode 100644 index 0000000..0248626 --- /dev/null +++ b/app/code/local/Prominc/PromincLinkOrder/Model/Mysql4/Prominclinkorder.php @@ -0,0 +1,11 @@ +_init('prominclinkorder/order', 'entity_id'); + } + + +} \ No newline at end of file diff --git a/app/code/local/Prominc/PromincLinkOrder/Model/Mysql4/Prominclinkordergrid.php b/app/code/local/Prominc/PromincLinkOrder/Model/Mysql4/Prominclinkordergrid.php new file mode 100644 index 0000000..5c4ebd0 --- /dev/null +++ b/app/code/local/Prominc/PromincLinkOrder/Model/Mysql4/Prominclinkordergrid.php @@ -0,0 +1,11 @@ +_init('prominclinkorder/ordergrid', 'entity_id'); + } + + +} \ No newline at end of file diff --git a/app/code/local/Prominc/PromincLinkOrder/Model/PromincLinkOrder.php b/app/code/local/Prominc/PromincLinkOrder/Model/PromincLinkOrder.php new file mode 100644 index 0000000..aff554b --- /dev/null +++ b/app/code/local/Prominc/PromincLinkOrder/Model/PromincLinkOrder.php @@ -0,0 +1,12 @@ +_init('prominclinkorder/prominclinkorder'); + } + + +} \ No newline at end of file diff --git a/app/code/local/Prominc/PromincLinkOrder/Model/PromincLinkOrderGrid.php b/app/code/local/Prominc/PromincLinkOrder/Model/PromincLinkOrderGrid.php new file mode 100644 index 0000000..2ffa122 --- /dev/null +++ b/app/code/local/Prominc/PromincLinkOrder/Model/PromincLinkOrderGrid.php @@ -0,0 +1,12 @@ +_init('prominclinkorder/prominclinkordergrid'); + } + + +} \ No newline at end of file diff --git a/app/code/local/Prominc/PromincLinkOrder/controllers/Adminhtml/Prominclinkorder/prominclinkorderadminController.php b/app/code/local/Prominc/PromincLinkOrder/controllers/Adminhtml/Prominclinkorder/prominclinkorderadminController.php new file mode 100644 index 0000000..6dc33c6 --- /dev/null +++ b/app/code/local/Prominc/PromincLinkOrder/controllers/Adminhtml/Prominclinkorder/prominclinkorderadminController.php @@ -0,0 +1,117 @@ +loadLayout() + ->_setActiveMenu('promincparentmenu/prominclinkorder'); + return $this; + } + + + /** + * Display a form to enter in the required information to make a link + */ + public function indexAction() + { + /* Get parameters */ + $store = $this->getRequest()->getParam('s'); + $email = $this->getRequest()->getParam('e'); + $order_id = $this->getRequest()->getParam('o'); + + /* Register parameters */ + Mage::register('store', $store); + Mage::register('email', $email); + Mage::register('order_id', $order_id); + + $this->_initAction() + ->_addContent($this->getLayout()->createBlock('prominclinkorder/adminhtml_' . $this->_modelName)) + ->renderLayout(); + } + + + + /** + * Preview the customer to order relationship + * Display customer and order information for manual review + * Do some automated checks to ensure this association is allowed/ok + */ + public function previewAction() + { + /* Get parameters */ + $store = $this->getRequest()->getParam('s'); + $email = $this->getRequest()->getParam('e'); + $order_id = $this->getRequest()->getParam('o'); + $saved = $this->getRequest()->getParam('saved'); + + /* Register parameters */ + Mage::register('store', $store); + Mage::register('email', $email); + Mage::register('order', Mage::getModel('sales/order')->loadByIncrementId( $order_id ) ); + Mage::register('order_id', $order_id); + Mage::register('saved', $saved); + + /* Load page */ + $this->_initAction(); + $this->_addContent($this->getLayout()->createBlock('prominclinkorder/adminhtml_' . $this->_modelName . '_preview')); + $this->renderLayout(); + } + + + /** + * Update the database with the customer to order association + * Redirects to the preview page with success message + */ + public function processAction() + { + /* Get parameters */ + $cid = $this->getRequest()->getParam('cid'); + $oid = $this->getRequest()->getParam('oid'); + $email = $this->getRequest()->getParam('c_e'); + + $tablesToSave = 2; + $tablesSaved = 0; + + /* Update Data to table: sales_flat_order */ + $model_order = Mage::getModel('prominclinkorder/PromincLinkOrder'); + $model_order->load( $oid ); + $model_order->setCustomerId( $cid ); + $model_order->setCustomerIsGuest( 0 ); + if( $model_order->save() ) { $tablesSaved += 1; } + + /* Update Data to table: sales_flat_order_grid */ + $model_orderGrid = Mage::getModel('prominclinkorder/PromincLinkOrderGrid'); + $model_orderGrid->load( $oid ); + $model_orderGrid->setCustomerId( $cid ); + if( $model_orderGrid->save() ) { $tablesSaved += 1; } + + /* Render messages */ + if( $tablesSaved == $tablesToSave ) { + Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('prominclinkorder')->__('This Customer has been linked to this Order.')); + } else { + Mage::getSingleton('adminhtml/session')->addError(Mage::helper('prominclinkorder')->__('An error occured in linking this Customer to the Order.')); + } + + /* Redirect to preview page with success message */ + $this->_redirect('*/*/preview', array('e' => $email, 'o' => $model_order->getIncrementId(), 's' => $model_order->getStoreId(), 'saved' => '1')); + return; + } + + + /** + * Bug fix from patch SUPEE-6285 + */ + protected function _isAllowed() { + return Mage::getSingleton('admin/session')->isAllowed( 'prominclinkorder/link_customer_to_order' ); + } + + +} \ No newline at end of file diff --git a/app/code/local/Prominc/PromincLinkOrder/etc/adminhtml.xml b/app/code/local/Prominc/PromincLinkOrder/etc/adminhtml.xml new file mode 100644 index 0000000..eb49ab6 --- /dev/null +++ b/app/code/local/Prominc/PromincLinkOrder/etc/adminhtml.xml @@ -0,0 +1,53 @@ + +No customer found for getStoreName($this->getStore()) ?> with email address of getEmail() ?>.
+ +__('Order Information:') ?> | +View Order | +
Customer Name | +getCustomerFirstname() ?> getCustomerLastname() ?> | +
Email Address | +getCustomerEmail() ?> | +
Order ID | +getIncrementId() ?> | +
Shipping Method | +getShippingDescription() ) ?> | +
Store Id | +getStoreId() ?> | +
Store Name | +", $order->getStoreName() ) ?> | +
helper('sales')->__('Product') ?> | +helper('sales')->__('Item Status') ?> | +helper('sales')->__('Original Price') ?> | +helper('sales')->__('Price') ?> | +helper('sales')->__('Qty') ?> | +helper('sales')->__('Subtotal') ?> | +helper('sales')->__('Tax Amount') ?> | +helper('sales')->__('Tax Percent') ?> | +helper('sales')->__('Discount Amount') ?> | +helper('sales')->__('Row Total') ?> | +
---|
No order found for order number getOrderId() ?>.
+ +