2

enter image description here

we have 4 different stores in our magento website.

onepagecontroller.php code:

 /**
     * Create order action
     */
    public function saveOrderAction()
    {
        if ($this->_expireAjax()) {
            return;
        }

        $result = array();
        try {
            if ($requiredAgreements = Mage::helper('checkout')->getRequiredAgreementIds()) {
                $postedAgreements = array_keys($this->getRequest()->getPost('agreement', array()));
                if ($diff = array_diff($requiredAgreements, $postedAgreements)) {
                    $result['success'] = false;
                    $result['error'] = true;
                    $result['error_messages'] = $this->__('Please agree to all the terms and conditions before placing the order.');
                    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
                    return;
                }
            }
            if ($data = $this->getRequest()->getPost('payment', false)) {
                $this->getOnepage()->getQuote()->getPayment()->importData($data);
            }
            $this->getOnepage()->saveOrder();

            $redirectUrl = $this->getOnepage()->getCheckout()->getRedirectUrl();
            $result['success'] = true;
            $result['error']   = false;
        } catch (Mage_Payment_Model_Info_Exception $e) {
            $message = $e->getMessage();
            if( !empty($message) ) {
                $result['error_messages'] = $message;
            }
            $result['goto_section'] = 'payment';
            $result['update_section'] = array(
                'name' => 'payment-method',
                'html' => $this->_getPaymentMethodsHtml()
            );
        } catch (Mage_Core_Exception $e) {
            Mage::logException($e);
            Mage::helper('checkout')->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
            $result['success'] = false;
            $result['error'] = true;
            $result['error_messages'] = $e->getMessage();

            if ($gotoSection = $this->getOnepage()->getCheckout()->getGotoSection()) {
                $result['goto_section'] = $gotoSection;
                $this->getOnepage()->getCheckout()->setGotoSection(null);
            }

            if ($updateSection = $this->getOnepage()->getCheckout()->getUpdateSection()) {
                if (isset($this->_sectionUpdateFunctions[$updateSection])) {
                    $updateSectionFunction = $this->_sectionUpdateFunctions[$updateSection];
                    $result['update_section'] = array(
                        'name' => $updateSection,
                        'html' => $this->$updateSectionFunction()
                    );
                }
                $this->getOnepage()->getCheckout()->setUpdateSection(null);
            }
        } catch (Exception $e) {
            Mage::logException($e);
            Mage::helper('checkout')->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
            $result['success']  = false;
            $result['error']    = true;
            $result['error_messages'] = $this->__('There was an error processing your order. Please contact us or try again later.');
        }
        $this->getOnepage()->getQuote()->save();
        /**
         * when there is redirect to third party, we don't want to save order yet.
         * we will save the order in return action.
         */
        if (isset($redirectUrl)) {
            $result['redirect'] = $redirectUrl;
        }

        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
    }

Current process happening in my site:

Customer Purchases two individual products from two different stores.

Ex: Customer purchases Product1 in Store1 & Product2 in Store2.

Order Id is being same for two stores. Store ID & Store name is being the same from where he checkouts of all the products.

i want to generate different order id's for different stores.

  • hi please help me..it is possible to generate different order id's for different stores –  Nov 13 '15 at 08:17

2 Answers2

0

If you're only looking for a way to decipher what order was bought on which store; rather than creating duplicate orders per store, you could add transaction comments to note which store that order was bought on. You can create an event to hook into a success controller action. Something like this in your custom modules config.xml file:

<frontend>
    <events>
        <!-- checkout_submit_all_after for testing before payment capture -->
        <checkout_onepage_controller_success_action>
            <observers>
                <your_module_payment_response>
                    <type>singleton</type>
                    <class>Your_Module_Model_Payment_Response</class>
                    <method>checkInvoicePaymentOnline</method>
                </your_module_payment_response>
            </observers>
        </checkout_onepage_controller_success_action>
    </events>
</frontend>

Then in the file Your/Module/Model/Payment/Response.php create a class with the method:

public function checkInvoicePaymentOnline(Varien_Event_Observer $observer) {
    $order = $this->getOrderFromObserver();
    $comment = 'Store = ' . Mage::app()->getStore()->getStoreId();
    $order->addStatusHistoryComment($comment);
    $order->save();
}

Now whenever you need to get the store ID from order you can grab the comments and search for "Store = ".

However if you still wish to duplicate the order, you can use the same hook provided and above and create another order programmatically like this.

Hope this helps

[UPDATE]

After clarification from the comments: setting up a multi website view would achieve order separation as opposed to the current multi store view setup which does not.

Chris Rogers
  • 1,525
  • 2
  • 20
  • 40
  • ->Chris Rogers : thanks for the response. actually my requirement is generate different order id's for different stores. –  Nov 12 '15 at 10:01
  • So one order with multiple IDs? – Chris Rogers Nov 12 '15 at 10:17
  • no..If Customer purchases Product1 in Store1 & Product2 in Store2..i have to generate two order id's for two stores –  Nov 12 '15 at 10:22
  • hi please help me..it is possible to generate different order id's for different stores –  Nov 13 '15 at 08:17
  • Right, can you give me an example of why you want to achieve this so I can better my answer, because it looks like what you want to do could get rather complex; splitting orders per store view. But there could be a better solution that'll achieve the same thing. Cheers – Chris Rogers Nov 13 '15 at 09:43
  • i just updated my question..please find it..that might be helpfull to you –  Nov 13 '15 at 11:12
  • Okay yes this better explains it! Thank you. Now, couple questions: 1. Have you setup multi store views or multi websites? 2. Can any customer view / buy products from different stores / websites? E.g 1 shopping cart with many products from different stores / websites. – Chris Rogers Nov 16 '15 at 08:08
  • 1) yeah it is a single website with multiple stores 2) Any customer can view and buy products from any store. E.g shopping cart with many products from different stores. –  Nov 16 '15 at 12:27
  • Awesome, I think however for this purpose you are going to need to setup a multi website store rather than a multi store website. This way, orders are separate for different views as your diagram required. See the differences here: http://stackoverflow.com/a/6027721/1238280 – Chris Rogers Nov 16 '15 at 18:03
  • In regards to multiple products from multiple stores creating multiple orders: bear with me here I'll have to do a little research, but you may have to use the hook **checkout_submit_all_after** and rewrite the order process to check each product and it's store and create a separate order for each. – Chris Rogers Nov 16 '15 at 18:09
  • hi, how to get database last inserted id at first place –  Nov 23 '15 at 09:33
  • You may have to be more specific but if you mean sort order descending then **$your_collection->setOrder('id', 'DESC');** will give you the order reversed. How is your issue going? – Chris Rogers Nov 23 '15 at 16:46
  • not moving yaar..i have one doubt.,is it possible to create multiple order id for single website with multiple stores.. i mean seperate order id for each store –  Nov 26 '15 at 06:54
  • Yes but you would have to create a new attribute on the order, override the order success controller and generate them there. You would then have to extend any block you wish to see these IDs on the website – Chris Rogers Nov 26 '15 at 08:56
  • Am not familiar with magento code, how to create a new attribute on the order.and how to override success controller..please help me –  Nov 27 '15 at 06:03
  • Okay this may be a little long for a comment so I will add another answer for you – Chris Rogers Nov 27 '15 at 07:54
  • brother one more humble request from me...please teach me magento..iam a fresher with basic php and magento knowledge...i want to gain indepth knowledge in magento...please help me..if you dont mind..hope for the best –  Nov 27 '15 at 09:57
0

Okay, we've already established you wish to create multiple order IDs per store view on a single checkout.

After reviewing the comments a multi store site would probably be the best setup, being able to place all products from multiple website views into the same basket would be more tricky. At least with a multi store setup you can share carts.

To separate the order IDs you would need to;

  • Add custom attributes for orders (one for each store).
  • Hook into an event after checkout.
  • Check for each product in the quote against the store it belongs to and create an separate ID for every store using the custom attribute.
  • Display this custom attribute whenever you wish using something like $order->getMyCustomAttribute() in any block which accesses your order.

For custom order attributes see this answer here which is nicely explained.

As mentioned in my previous answer you would still want to add an observer on the after checkout event: checkout_onepage_controller_success_action but instead, in your observer something like:

public function checkInvoicePaymentOnline(Varien_Event_Observer $observer) {
    $order = $this->getOrderFromObserver();
    $items = $order->getAllVisibleItems();
    $x = 0;
    foreach($items as $i) {
        $pId = $i->getProductId();
        $_product = Mage::getModel('catalog/product')->load($pId);
        $storeIds = $_product->getStoreIds();
        foreach ($storeIds as $sId) {
            // I assume you will only have 1 store view per product
            $ID = new Mage_Eav_Model_Entity_Increment_Order();
            $order->setData("set_custom_attr_".$x, $ID->getNextId());
        }
        $x++;
    }

    $order->save();
}

This will set all your custom order attribute, I assume you only have 1 store per product. This is also untested code.

Then whenever you wish to access this custom attribute, from any block with access to this order:

$order->getData('my_custom_store_attr_1');

Or

$order->getMyCustomStoreAttr1();

Does this help?

Community
  • 1
  • 1
Chris Rogers
  • 1,525
  • 2
  • 20
  • 40
  • You will need to create your own module: http://code.tutsplus.com/tutorials/magento-custom-module-development--cms-20643 – Chris Rogers Nov 27 '15 at 09:25
  • sorry i can't understand –  Nov 27 '15 at 09:27
  • first i need to create custom order attribute or, new custom module –  Nov 27 '15 at 09:32
  • In Magento, you can't just overwrite core files, you have to extend them for an up-gradable sake. You create your own module (using XML) to tell Magento "Hey here's my module, and this is want I wish to extend / overwrite". Magento will then use your code instead of the core files. You may wish to research into Magento development more. – Chris Rogers Nov 27 '15 at 09:35
  • please suggest me..iam in full confusion..i have to place above code in app/code/core/mage/checkout/model/type/onepage.php or not –  Nov 27 '15 at 09:41
  • brother one more humble request from me...please teach me magento..iam a fresher with basic php and magento knowledge...i want to gain indepth knowledge in magento...please help me..if you dont mind..hope for the best –  Nov 27 '15 at 09:56
  • Okay bro not going to sugar coat this, Magento is easily one of the most complex things I've had to learn, mainly because of they develop in such a unique way that you must adapt to. Having said that; whilst I was learning Magento I created notes to help me remember. I have made it public for you here: https://www.dropbox.com/s/w2uobq38q498ood/MagentoGuide2014.docx?dl=0 - Good luck! I really hope this helps you bro! – Chris Rogers Nov 27 '15 at 12:28
  • Also, you're in luck if you act fast! Udemy are having a massive Black Friday sale, and guess what I found: https://www.udemy.com/mastering-magento/ - It's seriously reduced right now so get on it fast. Really hope this helps! Magento is an incredible skill to know. – Chris Rogers Nov 27 '15 at 12:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96399/discussion-between-backbenchstudent-and-chris-rogers). –  Nov 28 '15 at 05:59
  • hi bro...thanks for the information you provided. after placing observer code, check out does not works properly,only the last added store item will check outs and remaining all cart items were in the cart only.and i have check in database table sales_flat_order_item, only last added store product order id has been saving. –  Nov 28 '15 at 06:01