【问题标题】:magento 2 Rest api cart merge guest cart and already customer active cartmagento 2 Rest api 购物车合并访客购物车和已经客户活动的购物车
【发布时间】:2018-04-19 08:22:00
【问题描述】:
As Alex Gusev cleared in their [reply][1]  Magento 2 REST can not distinguish between authenticated and anonymous customers. Web services ACL is applied for admin users only. All customers are anonymous for REST services.
But there is a method in magento 2 for rest api swagger http://devdocs.magento.com/swagger/ Here we can assign guest cart to customer using quoteGuestCartManagementV1 using method. 
But The only problem is that if customer already have active cart (item in cart). then it throw exception. 

无法将客户分配到给定的购物车。客户已经有活动的购物车。否则它工作正常。 是否有将客人报价/购物车合并到客户购物车/报价的任何解决方案。如果客户已经有活跃的购物车?

【问题讨论】:

  • 我认为link 会帮助你
  • 不,我问的是 magento 2 而不是 1

标签: api magento magento2


【解决方案1】:

您需要在自定义扩展中创建“Around”插件。

app/code/MageKnight/Quote/etc/module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="MageKnight_Quote">
        <sequence>
            <module name="Magento_Quote"/>            
        </sequence>
    </module>
</config>

app/code/MageKnight/Quote/registration.php

<?php

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'MageKnight_Quote', __DIR__);

app/code/MageKnight/Quote/etc/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <type name="Magento\Quote\Api\CartManagementInterface">
        <plugin name="mergeGuestCart"
                type="MageKnight\Quote\Plugin\Model\CartManagement"/>
    </type>
</config>

app/code/MageKnight/Quote/Plugin/Model/CartManagement.php

<?php

namespace MageKnight\Quote\Plugin\Model;

use Magento\Framework\Exception\StateException;

/**
 * Class CartManagement
 */
class CartManagement
{
    /**
     * @var \Magento\Quote\Api\CartRepositoryInterface
     */
    protected $quoteRepository;

    /**
     * @var \Magento\Customer\Api\CustomerRepositoryInterface
     */
    protected $customerRepository;

    /**
     * @var \Magento\Customer\Model\CustomerFactory
     */
    protected $customerModelFactory;

    /**
     * @var \Magento\Quote\Model\QuoteIdMaskFactory
     */
    private $quoteIdMaskFactory;

    /**
     * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
     * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
     * @param \Magento\Customer\Model\CustomerFactory $customerModelFactory
     * @param \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory
     */
    public function __construct(
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
        \Magento\Customer\Model\CustomerFactory $customerModelFactory,
        \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory
    ) {
        $this->quoteRepository = $quoteRepository;
        $this->customerRepository = $customerRepository;
        $this->customerModelFactory = $customerModelFactory;
        $this->quoteIdMaskFactory = $quoteIdMaskFactory;
    }

    /**
     * Around plugin to assign customer to guest cart
     *
     * @param \Magento\Quote\Api\CartManagementInterface $subject
     * @param callable $proceed
     * @param int $cartId The cart ID.
     * @param int $customerId The customer ID.
     * @param int $storeId
     * @return boolean
     */
    public function aroundAssignCustomer(
        \Magento\Quote\Api\CartManagementInterface $subject,
        callable $proceed,
        $cartId,
        $customerId,
        $storeId
    ) {
        $quote = $this->quoteRepository->getActive($cartId);
        $customer = $this->customerRepository->getById($customerId);
        $customerModel = $this->customerModelFactory->create();

        if (!in_array($storeId, $customerModel->load($customerId)->getSharedStoreIds())) {
            throw new StateException(
                __("The customer can't be assigned to the cart. The cart belongs to a different store.")
            );
        }
        if ($quote->getCustomerId()) {
            throw new StateException(
                __("The customer can't be assigned to the cart because the cart isn't anonymous.")
            );
        }
        try {
            $customerActiveQuote = $this->quoteRepository->getForCustomer($customerId);
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
            $customerActiveQuote = false;
        }
        if ($customerActiveQuote) {
            /** Merge carts */
            $quote->merge($customerActiveQuote);
            $this->quoteRepository->delete($customerActiveQuote);
        }
        $quote->setCustomer($customer);
        $quote->setCustomerIsGuest(0);
        $quote->setStoreId($storeId);
        $quote->setIsActive(1);
        /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
        $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'quote_id');
        if ($quoteIdMask->getId()) {
            $quoteIdMask->delete();
        }
        $this->quoteRepository->save($quote);
        return true;
    }
}

【讨论】:

  • 我遇到了问题无法将客户分配到购物车,因为购物车不是匿名的。。不知道这车有什么问题。谁能帮帮我?
猜你喜欢
  • 1970-01-01
  • 2016-02-20
  • 2012-11-22
  • 2011-04-09
  • 1970-01-01
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多