https://magenticians.com/override-model-magento-2/
I have followed the above tutorial to extend the Product class to show our custom compatible products with the product that the customer views. However after registering the module and completing all the steps, when I open the site, it shows the error "Type Error occurred when creating object: Company\Module\Model\Catalog\Product\Interceptor"
here is my extended Product.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Company\Module\Model\Catalog;
use Magento\Authorization\Model\UserContextInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductLinkRepositoryInterface;
use Magento\Catalog\Model\Product\Attribute\Backend\Media\EntryConverterPool;
use Magento\Catalog\Model\FilterProductCustomAttribute;
use Magento\Framework\Api\AttributeValueFactory;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\AuthorizationInterface;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Pricing\SaleableInterface;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
class Product extends \Magento\Catalog\Model\Product
{
private $productCollectionFactory;
protected $compatibilitySettingsArray = array(
//some values
);
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
AttributeValueFactory $customAttributeFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Api\ProductAttributeRepositoryInterface $metadataService,
\Magento\Catalog\Model\Product\Url $url,
\Magento\Catalog\Model\Product\Link $productLink,
\Magento\Catalog\Model\Product\Configuration\Item\OptionFactory $itemOptionFactory,
\Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory $stockItemFactory,
\Magento\Catalog\Model\Product\OptionFactory $catalogProductOptionFactory,
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
\Magento\Catalog\Model\Product\Attribute\Source\Status $catalogProductStatus,
\Magento\Catalog\Model\Product\Media\Config $catalogProductMediaConfig,
\Magento\Catalog\Model\Product\Type $catalogProductType,
\Magento\Framework\Module\Manager $moduleManager,
\Magento\Catalog\Helper\Product $catalogProduct,
\Magento\Catalog\Model\ResourceModel\Product $resource,
\Magento\Catalog\Model\ResourceModel\Product\Collection $resourceCollection,
\Magento\Framework\Data\CollectionFactory $collectionFactory,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
\Magento\Catalog\Model\Indexer\Product\Flat\Processor $productFlatIndexerProcessor,
\Magento\Catalog\Model\Indexer\Product\Price\Processor $productPriceIndexerProcessor,
\Magento\Catalog\Model\Indexer\Product\Eav\Processor $productEavIndexerProcessor,
CategoryRepositoryInterface $categoryRepository,
\Magento\Catalog\Model\Product\Image\CacheFactory $imageCacheFactory,
\Magento\Catalog\Model\ProductLink\CollectionProvider $entityCollectionProvider,
\Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider,
\Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory,
\Magento\Catalog\Api\Data\ProductLinkExtensionFactory $productLinkExtensionFactory,
EntryConverterPool $mediaGalleryEntryConverterPool,
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
array $data = [],
\Magento\Eav\Model\Config $config = null,
\Magento\Catalog\Model\FilterProductCustomAttribute $filterCustomAttribute = null,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $_productCollectionFactory
) {
$this->productCollectionFactory = $_productCollectionFactory;
parent::__construct(
$metadataService,
$itemOptionFactory,
$stockItemFactory,
$catalogProductOptionFactory,
$catalogProductVisibility,
$catalogProductStatus,
$catalogProductMediaConfig,
$catalogProductType,
$moduleManager,
$catalogProduct,
$collectionFactory,
$url,
$productLink,
$filesystem,
$indexerRegistry,
$productFlatIndexerProcessor,
$productPriceIndexerProcessor,
$productEavIndexerProcessor,
$categoryRepository,
$imageCacheFactory,
$entityCollectionProvider,
$linkTypeProvider,
$productLinkFactory,
$productLinkExtensionFactory,
$mediaGalleryEntryConverterPool,
$dataObjectHelper,
$joinProcessor
);
}
function startsWith ($string, $startString)
{
$len = strlen($startString);
return (substr($string, 0, $len) === $startString);
}
function getResolution($product){
//some resolution based code for the product
}
function isCameraOrRecorder(){
//code to get the type of the product
}
public function getCompatibleProducts()
{
//code to get the compatible products
}
}
I have researched some google and stackoverflow resources but almost all of them come with duplicate ID problems. but mine is not. as I am also new to magento I am not sure exactly where to look for this kind of problem.
I have read this (https://webkul.com/blog/magento2-interceptor-pattern-code-generation/) article but this only talks about plugins, which is not my case.
I would appreciate any help I can get to resolve this issue.
Thanks in advance
php bin/magento setup:di:compile php bin/magento cache:cleanphp bin/magento setup:di:compile php bin/magento cache:clean