0

I try to extend this class from my own module, because I need to inject another dependency to the constructor and add a few new methods:

vendor\aheadworks\module-wbtab\Block\Wbtab\ProductList.php

namespace Aheadworks\Wbtab\Block\Wbtab;

use Aheadworks\Wbtab\Model\Source\BlockLayout;
use Aheadworks\Wbtab\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Checkout\Model\ResourceModel\Cart;
use Magento\Checkout\Model\Session;
use Magento\Framework\Module\Manager;
use Magento\Framework\Data\Helper\PostHelper;
use Magento\Catalog\Pricing\Price\FinalPrice;
use Magento\Framework\Pricing\Render as PricingRender;

/**
 * Class ProductList
 *
 * @method int|null getProductId()
 * @method string getBlockTitle()
 * @method string getBlockLayout()
 * @method int getProductsLimit()
 * @method bool getDisplayAddToCart()
 *
 * @package Aheadworks\Wbtab\Block\Wbtab
 */
class ProductList extends \Magento\Catalog\Block\Product\ProductList\Related
{
    /**
     * Path to template.
     *
     * @var string
     */
    protected $_template = 'wbtab/product_list.phtml';

    /**
     * Product collection factory
     *
     * @var ProductCollectionFactory
     */
    private $productCollectionFactory;

    /**
     * @var PostHelper
     */
    private $postHelper;

    /**
     * @param Context $context
     * @param Cart $checkoutCart
     * @param Visibility $catalogProductVisibility
     * @param Session $checkoutSession
     * @param Manager $moduleManager
     * @param ProductCollectionFactory $productCollectionFactory
     * @param PostHelper $postHelper
     * @param array $data
     */
    public function __construct(
        Context $context,
        Cart $checkoutCart,
        Visibility $catalogProductVisibility,
        Session $checkoutSession,
        Manager $moduleManager,
        ProductCollectionFactory $productCollectionFactory,
        PostHelper $postHelper,
        array $data = []
    ) {
        $this->productCollectionFactory = $productCollectionFactory;
        $this->postHelper = $postHelper;
        parent::__construct(
            $context,
            $checkoutCart,
            $catalogProductVisibility,
            $checkoutSession,
            $moduleManager,
            $data
        );
    }

    ...

}

I created my extend block in:

app\code\Company\Extend\Block\Wbtab\ProductList.php

<?php

namespace Company\Extend\Block\Wbtab;

use \Magento\Catalog\Block\Product\Context;
use \Aheadworks\Wbtab\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use \Magento\Catalog\Model\Product\Visibility;
use \Magento\Checkout\Model\ResourceModel\Cart;
use \Magento\Checkout\Model\Session;
use \Magento\Framework\Module\Manager;
use \Magento\Framework\Data\Helper\PostHelper;
use \Company\Base\Helper\Product as HfProductHelper;

class ProductList extends \Aheadworks\Wbtab\Block\Wbtab\ProductList
{

    private $productCollectionFactory;

    /**
     * @var PostHelper
     */
    private $postHelper;

    private $hfProductHelper;

    public function __construct(
        Context $context,
        Cart $checkoutCart,
        Visibility $catalogProductVisibility,
        Session $checkoutSession,
        Manager $moduleManager,
        ProductCollectionFactory $productCollectionFactory,
        PostHelper $postHelper,
        HfProductHelper $hfProductHelper,
        array $data = []
    ) {
        $this->productCollectionFactory = $productCollectionFactory;
        $this->postHelper = $postHelper;
        $this->hfProductHelper = $hfProductHelper;

        parent::__construct(
            $context,
            $checkoutCart,
            $catalogProductVisibility,
            $checkoutSession,
            $moduleManager,
            $data
        );
    }

    public function getProductById($id)
    {
        return $this->hfProductHelper->getProductById($id);
    }

    public function isProductNew($_product = null)
    {
        return $this->hfProductHelper->isProductNew($_product);
    }

    public function testFooBar()
    {
        return "test foo bar";
    }

}

app\code\Company\Extend\etc\frontend\di.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <preference for="\Aheadworks\Wbtab\Block\Wbtab\ProductList"
                type="\Company\Extend\Block\Wbtab\ProductList" />
</config>

But if I execute php bin/magento setup:di:compile then I get:

Company\Extend\Block\Wbtab\ProductList Incompatible argument type: Required type: \Aheadworks\Wbtab\Model\ResourceModel\Product\CollectionFactory. Actual type: array; File: /home/company/company.de/app/code/Company/Extend/Block/Wbtab/ProductList.php

1

you're missing passing two non-optional params ($productCollectionFactory, $postHelper) while calling parent::__construct. So that's how the call to parent::__construct should look like in extended class:

parent::__construct(
            $context,
            $checkoutCart,
            $catalogProductVisibility,
            $checkoutSession,
            $moduleManager,
            $productCollectionFactory,
            $postHelper,
            $data
        );
1

In your \Company\Extend\Block\Wbtab\ProductList constructor you are calling the constructor of the parent class \Aheadworks\Wbtab\Block\Wbtab\ProductList which expects 8 parameters, but you're only giving 6.

Update your constructor to the following:

public function __construct(
    Context $context,
    Cart $checkoutCart,
    Visibility $catalogProductVisibility,
    Session $checkoutSession,
    Manager $moduleManager,
    ProductCollectionFactory $productCollectionFactory,
    PostHelper $postHelper,
    HfProductHelper $hfProductHelper,
    array $data = []
) {
    $this->productCollectionFactory = $productCollectionFactory;
    $this->postHelper = $postHelper;
    $this->hfProductHelper = $hfProductHelper;

    parent::__construct(
        $context,
        $checkoutCart,
        $catalogProductVisibility,
        $checkoutSession,
        $moduleManager,
        $productCollectionFactory,
        $postHelper,
        $data
    );
}

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.