Magento在下订单后将客户与订单联系起来

时间:2018-05-28 12:45:43

标签: magento

我的自定义模块会观察sales_order_place_after事件,并通过为订单设置customerId来创建客户并将客户与订单相关联。

什么有效?

  • 下订单
  • 创建了客户
  • customerId在订单数据库中更新

哪些不起作用?

  • customerId立即被另一个脚本
  • 重新设置为NULL

在我的观察员完成运行后,如何找出哪个脚本再次更新customerIdNULL

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题-我的假设是,在订单上调用save方法会触发正在监听sales_order_save_before/after事件的事件,其中之一就是将客户ID设置回{{1 }}。通过仅保存所需的属性来解决此问题,而不是触发整个订单的保存:

null

这使我能够使用$order ->setCustomerId($customer->getId()) ->setCustomerIsGuest(0) ->setCustomerGroupId($customer->getGroupId()); $order->getResource() ->saveAttribute($order, 'customer_id' ) ->saveAttribute($order, 'customer_is_guest') ->saveAttribute($order, 'customer_group_id'); 事件将客户与Magento EE 1.14.3.10中的订单成功关联。

答案 1 :(得分:0)

您应该将活动更改为 sales_model_service_quote_submit_success

示例代码(Magento 2版本): events.xml

<event name="sales_model_service_quote_submit_success">
        <observer name="quote_submit_success_observer" instance="Namespace\ModuleName\Observer\GenerateObserver" />
    </event>

<强> GenerateObserver.php

 ...
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
    /** @var \Magento\Sales\Model\Order $order */
        $order = $observer->getEvent()->getData('order');

    // Ensure customer is registered
    $this->registerCustomer($order);
    $order->getResource()->save($order);
 }

protected function registerCustomer(
    \Magento\Sales\Api\Data\OrderInterface $order
) {
    /** @var \Magento\Sales\Model\Order $order */

    if ($order->getCustomerId() === null) {
        // Load customer by email, create if not exists.
        try {
            $customerData = $this->customerRepository->get($order->getCustomerEmail());
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
            $customerData = $this->orderCustomerManager->create($order->getId());
        }

        $order->setCustomerId($customerData->getId());
    }

    return $this;
}
 ...

答案 2 :(得分:0)

我没有足够的代表对此发表评论,但是我能够使用@gregdev的答案来完成此操作。

Config.xml添加事件观察器:

<sales_model_service_quote_submit_success>
            <observers>
                <your_observer_name_here>
                    <class>module/observer</class>
                    <method>checkOrderCustomer</method>
                </your_observer_name_here>
            </observers>
</sales_model_service_quote_submit_success>

在您的Observer.php

public function checkOrderCustomer($observer){
    $order = $observer->getEvent()->getOrder();
    //however you want to check for customer
    $customerEmail = $order->getCustomerEmail();
    $customer = Mage::getModel('customer/customer')->loadByEmail($customerEmail);
        if($customer->getId()){
            //assign order to customer
            $order
                ->setCustomerId($customer->getId())
                ->setCustomerIsGuest(0)
                ->setCustomerGroupId($customer->getGroupId());

            $order->getResource()
                ->saveAttribute($order, 'customer_id'      )
                ->saveAttribute($order, 'customer_is_guest')
                ->saveAttribute($order, 'customer_group_id');
        }
    }
}

我认为sales_order_place_after的问题与Mage / Sales / Model / Service / Quote.php中的以下内容有关

$transaction->addObject($order);
$transaction->addCommitCallback(array($order, 'place'));
$transaction->addCommitCallback(array($order, 'save'));

我不确定传递给第二个回调的$order变量是否获得在sales_order_place_after事件期间应用的任何客户更改。