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.