문제

How can i add every product as a new item into cart instead of updating cart quantity?

ie. Suppose a product is already added in cart. If i add that product again to cart it should add new product not only updating quantity of that product.

도움이 되었습니까?

해결책

I got the solution.

we need to override the Quote.php file(Magento\Quote\Model\Quote.php) and should manage if condition in addProduct() method.

 //if (!$item) {
            $item = $this->itemProcessor->init($candidate, $request);
            // Add only item that is not in quote already
            $this->addItem($item);
 //}

Then each product will be add as new item into cart instead of updating existing item

다른 팁

There is a method in \Magento\Quote\Model\Quote\Item class called representProduct. This method compare if item was added before or not. So you need to create plugin which should always return false for adding each item as new.

  1. Add a plugin in etc/di.xml file of your module
<type name="Magento\Quote\Model\Quote\Item">
    <plugin name="add_each_item_separately"
            type="Vendor\Module\Plugin\Quote\Model\Quote\ItemPlugin"
            sortOrder="10"/>
</type>
  1. Create plugin class in Vendor\Module\Plugin\Quote\Model
namespace Vendor\Module\Plugin\Quote\Model\Quote;

use Magento\Quote\Model\Quote\Item;

class ItemPlugin
{
    public function afterRepresentProduct(Item $item, bool $result): bool
    {
        return false;
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top