将客户报价与客户报价合并(如果custommer登录)magento 2 rest api


9

如果购物车中有2个项目(如果客户登录,则两个项目都需要添加购物车),两个报价(登录前和登录后)合并并创建包含客户报价的所有项目的最终报价

我在Google中获得的参考链接

https://magento.stackexchange.com/a/62481

https://magento.stackexchange.com/a/30460


请进一步澄清问题。因为magento 2.2提供了默认功能。
Yogesh

如果客户登录可以将购物车项目添加到客户购物车,请尽可能说明“使用REST API”。
Nagendra Kodi

@Yogesh我的产品API返回的产品,如果我的客户我得到的错误尝试,我的网址:192.168.1.65/anusthana/api/rest/customers错误:snag.gy/0jbhTr.jpg u能帮助我
社会保险机构

Answers:


2

默认情况下,当客户登录时,API端的Magento 2不提供任何用于将客户卡与客户卡合并的API。

但是,您可以用客户推车替换来宾推车。

API : (/V1/carts/:cartId) 
File : vendor/magento/module-quote/Model/QuoteManagement.php
Function : public function assignCustomer($cartId, $customerId, $storeId)

但是,如果您要开发功能合并购物车实时Magento Web端,则需要创建自定义API。


0

您需要在自定义扩展中创建“ 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>

应用程序/代码/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 /代码/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;
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.