0

I tried to override a model in Magento\Quote\Model\Cart\Data\CartItem but I cannot override it.

here is my 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">
    <preference for="Magento\Quote\Model\Cart\Data\CartItem" type="Vendor\AddProductWithVendorGraphQl\Model\Cart\Data\CartItem" />
</config>

and this is my class

class CartItem extends \Magento\Quote\Model\Cart\Data\CartItem
{

        /**
     * @var string
     */
    private $sku;

    /**
     * @var float
     */
    private $quantity;


    /**
     * @var string
     */
    private $parentSku;

    /**
     * @var SelectedOption[]
     */
    private $selectedOptions;

    /**
     * @var EnteredOption[]
     */
    private $enteredOptions;

    /**
     * @param string $sku
     * @param float $quantity
     * @param string|null $parentSku
     * @param array|null $selectedOptions
     * @param array|null $enteredOptions
     */
    public function __construct(
        string $sku,
        float $quantity,
        string $parentSku = null,
        array $selectedOptions = null,
        array $enteredOptions = null
    ) {
        $this->sku = $sku;
        $this->quantity = $quantity;
        $this->parentSku = $parentSku;
        $this->selectedOptions = $selectedOptions;
        $this->enteredOptions = $enteredOptions;
    }
    public function getQuantity(): float
    {
        var_dump('tes');die();
    }

I've tried to use plugins too. But it still didn't work. Can anyone help me?

4
  • why you think, this override is not working.Code seems ok.
    – Amit Bera
    Commented Mar 7, 2021 at 5:41
  • because when the var_dump didn't return anything. and when I try to var-dump on CartItem.php from the Magento, it return the value of the var_dump Commented Mar 7, 2021 at 6:57
  • @Rosario in which Magento version you find this class?
    – ZealousWeb
    Commented Mar 11, 2021 at 14:30
  • Magento version 2.4.1 Commented Mar 12, 2021 at 4:25

2 Answers 2

0

Please try with the below code.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */    

namespace Magento\Quote\Model\Cart\Data;

class CartItem extends \Magento\Quote\Model\Cart\Data\CartItem
{

    /**
     * @var string
     */
    private $sku;

    /**
     * @var float
     */
    private $quantity;


    /**
     * @var string
     */
    private $parentSku;

    /**
     * @var SelectedOption[]
     */
    private $selectedOptions;

    /**
     * @var EnteredOption[]
     */
    private $enteredOptions;

    /**
     * @param string $sku
     * @param float $quantity
     * @param string|null $parentSku
     * @param array|null $selectedOptions
     * @param array|null $enteredOptions
     */
    public function __construct(
        string $sku,
        float $quantity,
        string $parentSku = null,
        array $selectedOptions = null,
        array $enteredOptions = null
    ) {
        $this->sku = $sku;
        $this->quantity = $quantity;
        $this->parentSku = $parentSku;
        $this->selectedOptions = $selectedOptions;
        $this->enteredOptions = $enteredOptions;
        
        parent::__construct($sku, $quantity, $parentSku, $selectedOptions, $enteredOptions);
        //or
        parent::__construct($sku, $quantity, null, null, null);
    }
    public function getQuantity(): float
    {
        var_dump('tes');die();
    }

}

Might be the issue of you can't pass the parent argument.

0

I was struggling to understand the same here this week.

You probably already realized the "preference" method won't work for this class. It's because Magento doesn't load that class or its factory through dependency injection. I don't know why but for this specific class Magento is not following the design patterns.

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

You'll be able to solve that by just overriding the resolver.

Magento\QuoteGraphQl\Model\Resolver\AddProductsToCart

Then you can use DI to inject a custom factory class and build what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.