0

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

4
  • try this The interceptor file needs to be re-create. So kindly run di:compile and clean cache php bin/magento setup:di:compile php bin/magento cache:clean Jun 2, 2020 at 9:16
  • @MohitPatel I did that a few times during creation of the module and enabling it.
    – isc
    Jun 2, 2020 at 10:17
  • you re-create interceptor file . So you run php bin/magento setup:di:compile php bin/magento cache:clean Jun 2, 2020 at 12:45
  • I re-enabled the module after yesterday, and re-ran the commands but still it says : "Error filtering template: Type Error occurred when creating object: Company\Module\Model\Catalog\Product\Interceptor "
    – isc
    Jun 3, 2020 at 7:11

1 Answer 1

1

The original answer: Move your inject \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $_productCollectionFactory Before array $data = [] And empty generated folder

THE SOLUTION :

As the original author noticed and suggested below in comments, I have made some mistakes with the parent::construct parameters. Fixed them, and the error is gone.

10
  • thank you for the answer, however as for the business times restriction I will be able to try it about 12 hours or so later. But I will definitely comment on whether it did work or not.
    – isc
    Jun 2, 2020 at 14:02
  • I did as you told, but still the same : "Error filtering template: Type Error occurred when creating object: Company\Module\Model\Catalog\Product\Interceptor"
    – isc
    Jun 3, 2020 at 7:28
  • Did you try php bin/magento setup:di:compile? Jun 3, 2020 at 7:31
  • yes, yesterday I have disabled the module, today I reenabled it, and ran all the commands setup:upgrade, setup:di:compile. cache:clean and cache:flush
    – isc
    Jun 3, 2020 at 7:36
  • Wrong in parent::__construct(. Reference github.com/magento/magento2/blob/2.3.5/app/code/Magento/Catalog/… Jun 3, 2020 at 7:43

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.