I am working with Magento2, so as per my requirement I need to override the "Magento\Checkout\Model\Cart.php" class, but the cart.php class look like as follow

namespace Magento\Checkout\Model;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Checkout\Model\Cart\CartInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\DataObject;

 * Shopping cart model
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 * @deprecated 
class Cart extends DataObject implements CartInterface {}

So, from the above, I am not sure how to override this class. Kindly share your idea and thoughts it'll help me.


Better option to use plugin instead of override but if you want override then you can manage this by below code: Your module di.xml code will be:

<?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\Checkout\Model\Cart" type="Vendor\Module\Model\Cart" />

And app/code/[Vendor]/[Module]/Model/Cart.php code will be:

namespace Vendor\Module\Model;

class Cart extends \Magento\Checkout\Model\Cart


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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