0

Does anyone know how to create a plugin for the class Magento\Quote\Model\Cart\Data\CartItemFactory method public function create(array $data): CartItem?

I have tried creating before, after, and around plugins but didn't work. below is the reference code:

di.xml:

<type name="Magento\Quote\Model\Cart\Data\CartItemFactory"> <plugin name="custom_store_qty" type="Vendor\Module\Plugin\Model\Cart\Data\CartItemFactory" sortOrder="1" /> </type> 

Vendor\Module\Plugin\Model\Cart\Data\CartItemFactory.php:

<?php namespace Vendor\Module\Plugin\Model\Cart\Data; class CartItemFactory { public function aroundCreate(\Magento\Quote\Model\Cart\Data\CartItemFactory $subject, callable $proceed) { //Custom logic //die("--"); } } 

Note: I need to add a new parameter in the addProductsToCart graphql mutation. ex:

mutation { addProductsToCart( cartId: "jdAxK6pbDHsMCQvHG0Uc98esIRbG2YZy" cartItems: { sku: "product-sku" quantity: 2 test_attribute: "test attribute data as additional option." } } 

Thank you.

2 Answers 2

0

For this i think plugin will not work. You have to override

Magento\Quote\Model\Cart\Data\CartItem

this file in you app/code module and then on same path you have to create

Vendor\Module\Plugin\Model\Cart\Data\CartItemFactory File.

Thanks

0

plugin and preference will not work because for this specific class Magento doesn't load that class or its factory through dependency injection

`$cartItems[] = (new CartItemFactory())->create($cartItemData);` 

overriding the resolver.

Magento\QuoteGraphQl\Model\Resolver\AddProductsToCart

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.