Skip to content

Magento 2 Plugin not working for class MagentoQuoteModelCartDataCartItemFactory

Does anyone know how to create a plugin for the class MagentoQuoteModelCartDataCartItemFactory 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="MagentoQuoteModelCartDataCartItemFactory">
   <plugin name="custom_store_qty" 
      type="VendorModulePluginModelCartDataCartItemFactory" 
      sortOrder="1" />
</type>

VendorModulePluginModelCartDataCartItemFactory.php:

<?php
namespace VendorModulePluginModelCartData;

class CartItemFactory
{
    public function aroundCreate(MagentoQuoteModelCartDataCartItemFactory $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.