5

I want to override a class Magento\Catalog\Model\Product\Type\Simple

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\Catalog\Model\Product\Type\Simple"
                type="<vendor_name>\<module_name>\Model\Product\Type\Simple"/>
</config>

I override and added one function :

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace <vendor_name>\<module_name>\Model\Product\Type;


/**
 * Simple product type implementation
 */
class Simple extends \Magento\Catalog\Model\Product\Type\Simple
{    

    /**
     * Attribute collection factory
     *
     * @var
     * \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\CollectionFactory
     */
    protected $_attributeCollectionFactory;

    /**
     * @codingStandardsIgnoreStart/End
     *
     * @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\CollectionFactory $attributeCollectionFactory
     *
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
     */
    public function __construct(
        \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\CollectionFactory $attributeCollectionFactory      
    ) {
        $this->_attributeCollectionFactory = $attributeCollectionFactory;        
    }


    /**
     * Retrieve configurable attribute collection
     *
     * @param \Magento\Catalog\Model\Product $product
     * @return \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\Collection
     */
    public function getConfigurableAttributeCollection(\Magento\Catalog\Model\Product $product)
    {
        return $this->_attributeCollectionFactory->create()->setProductFilter($product);
    }
}

But it cause fatal error while I tried to add product in cart as below:

Uncaught Error: Call to a member function dispatch() on null in .../vendor/magento/module-catalog/Model/Product/Type/AbstractType.php:602

I add getConfigurableAttributeCollection function because when I load product collection of ordered Items including configurable Items it throw error like :

undefined getConfigurableAttributeCollection()

So I added this function into Sample.php

But no luck, Any Suggestions would be appreciated.

  • 1
    Try calling parent construct with parameters – Amit Singh May 22 '17 at 11:19
  • okay let me try @AmitSingh – Ronak Chauhan May 22 '17 at 11:32
0

You don't need to override all class, if you just need to modify one method you can use for this Magento Plugins: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html

0

You are very close to the right answer :)

Everything is correct except you forgot to call the original constructor, which is defined in AbstractType.php. The constructor is very important because it injects all the dependencies.

If you want to inject additional dependencies you still need to call the original constructor with all it's dependencies.

Your constructor should look like this:

public function __construct(
    \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\CollectionFactory $attributeCollectionFactory,

    \Magento\Catalog\Model\Product\Option $catalogProductOption,
    \Magento\Eav\Model\Config $eavConfig,
    \Magento\Catalog\Model\Product\Type $catalogProductType,
    \Magento\Framework\Event\ManagerInterface $eventManager,
    \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb,
    \Magento\Framework\Filesystem $filesystem,
    \Magento\Framework\Registry $coreRegistry,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
) {
    $this->_attributeCollectionFactory = $attributeCollectionFactory;

    parent::__construct(
        $catalogProductOption,
        $eavConfig,
        $catalogProductType,
        $eventManager,
        $fileStorageDb,
        $filesystem,
        $coreRegistry,
        $logger,
        $productRepository
    );
}

Hope this helps.

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.