在magento中的单一结帐流程中为不同商店生成不同的订单ID

问题描述 投票:0回答:2

我们的 magento 网站上有 4 家不同的商店。

onepagecontroller.php代码:

 /**
     * 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));
    }

我的网站当前发生的流程:

客户从两个不同的商店购买两种单独的产品。

例如:客户在 Store1 中购买 Product1,在 Store2 中购买 Product2。

两家商店的订单 ID 相同。 商店 ID 和商店名称与他结帐所有产品时的位置相同。

我想为不同的商店生成不同的订单ID。

php magento store checkout
2个回答
0
投票

如果您只是在寻找一种方法来破译在哪家商店购买的订单;您可以添加交易注释来注明该订单是在哪个商店购买的,而不是为每个商店创建重复的订单。 您可以创建一个事件来挂钩成功控制器操作。您的自定义模块中的类似内容config.xml文件:

<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>

然后在文件 Your/Module/Model/Payment/Response.php 中使用以下方法创建一个类:

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

现在,每当您需要从订单中获取商店 ID 时,您都可以抓取评论并搜索“Store =”。

但是,如果您仍然希望复制订单,您可以使用上面提供的相同挂钩,并以编程方式创建另一个订单,如下所示

希望这有帮助

[更新]

经过评论澄清:设置多网站视图将实现订单分离,而当前的多商店视图设置则不能。


0
投票

好的,我们已经确定您希望在单次结账时为每个商店视图创建多个订单 ID。

查看评论后,多商店网站可能是最好的设置,能够将多个网站视图中的所有产品放入同一个购物篮中会更加棘手。至少通过多商店设置,您可以共享购物车。

要分隔您需要的订单 ID;

  • 为订单添加自定义属性(每个商店一个)。
  • 结账后加入活动。
  • 根据其所属商店检查报价中的每个产品,并使用自定义属性为每个商店创建单独的 ID。
  • 只要您希望在访问订单的任何块中使用类似 $order->getMyCustomAttribute() 的内容,即可显示此自定义属性。

对于自定义订单属性请参阅此处的答案,解释得很好。

正如我之前的回答中提到的,您仍然希望在结帐后事件中添加观察者: checkout_onepage_controller_success_action 但是,在您的观察者中类似:

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();
}

这将设置您所有的自定义订单属性,我假设您每个产品只有 1 个商店。这也是未经测试的代码。

然后,每当您希望从任何有权访问此订单的块访问此自定义属性时:

$order->getData('my_custom_store_attr_1');

或者

$order->getMyCustomStoreAttr1();

这有帮助吗?

© www.soinside.com 2019 - 2024. All rights reserved.