0

I have block named custom.product

namespace Myaddon\CustomProduct\Block\Product;
use Magento\Framework\View\Element\Template;

class Custom extends \Magento\Catalog\Block\Product\View
{       
    protected function _toHtml()
    {           
        $this->setModuleName($this->extractModuleName('Magento\Catalog\Block\Product\View'));

        $this->getLayout()->getBlock('custom.product')->setTemplate('Myaddon_CustomProduct::custom.phtml');

        return parent::_toHtml();;
    }

    public function getCustomProduct(\Myaddon\CustomProduct\Model\CustomProduct $customProduct){

        $a = $this->customProduct->getCollection();
        echo count($a); die('xxx');

    }
}

When i run the above code, I get error. So please tell me what's wrong?

Recoverable Error: Argument 1 passed to Myaddon\CustomProduct\Block\Product\Custom::getCustomProduct() must be an instance of Myaddon\CustomProduct\Model\CustomProduct, none given, called in C:\localhost\magento2-2.0.0-rc\app\code\Myaddon\CustomProduct\view\frontend\templates\custom.phtml on line 1 and defined in C:\localhost\magento2-2.0.0-rc\app\code\Myaddon\CustomProduct\Block\Product\Custom.php on line 21 [] []

7
  • 2
    I don't understand what it is that you are asking. The error message is telling exactly why you are getting the error: expects object Myaddon\CustomProduct\Model\CustomProduct, none given
    – ProxiBlue
    Commented Nov 23, 2015 at 9:19
  • I want add new block with temaplate custom.phtml in product view. when i do it get error.
    – xanka
    Commented Nov 23, 2015 at 9:22
  • Then that should be your question, or at least, the leading part of the question. ;)
    – ProxiBlue
    Commented Nov 23, 2015 at 9:29
  • I want get model Myaddon\CustomProduct\Model\CustomProduct in this Block. how do it? @ProxiBlue. tk
    – xanka
    Commented Nov 23, 2015 at 9:32
  • I don't know, sorry.
    – ProxiBlue
    Commented Nov 23, 2015 at 9:35

2 Answers 2

1

use plugin concept to extend How to Override Core Block, Model and controller in Magento2


or if toy need collection in your custom block then use

<?php
namespace Sugarcode\Test\Block;

class Test extends \Magento\Framework\View\Element\Template
{
    protected $_coreRegistry = null;
    protected $_testmodel;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Model\Product $testmodel,
        array $data = []
    ) {
        $this->_testmodel = $testmodel;
        $this->_coreRegistry = $registry;
        parent::__construct($context, $data);
    }



    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
    public function getAllData()
    {

        //$data = $this->_testmodel->load(1);
        $datas = $this->_testmodel->getCollection();
        return $datas;
    }


}

in phtml file call

$this->getAllData();
0

As per the error, you are not passing any argument to getCustomProduct. You also have to declare usage of customProductFactory at the top of the file. In order to use colleciton like $this->customproduct->getCollection(), you need to declare protected variable and initialize it with customProduct model, or use an object manager. Also for getting collection from model you have to write create()->getCollection()...

so try this code:

namespace Myaddon\CustomProduct\Block\Product;
use Myaddon\CustomProduct\Model\CustomProductFactory;
use Magento\Framework\View\Element\Template;

class Custom extends \Magento\Catalog\Block\Product\View
{       
    protected $customProduct; 

    public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\Url\EncoderInterface $urlEncoder,
    \Magento\Framework\Json\EncoderInterface $jsonEncoder,
    \Magento\Framework\Stdlib\StringUtils $string,
    \Magento\Catalog\Helper\Product $productHelper,
    \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig,
    \Magento\Framework\Locale\FormatInterface $localeFormat,
    \Magento\Customer\Model\Session $customerSession,
    ProductRepositoryInterface $productRepository,
    \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
    CustomProductFactory $_customProduct, 
    array $data = []
) {
    $this->_productHelper = $productHelper;
    $this->urlEncoder = $urlEncoder;
    $this->_jsonEncoder = $jsonEncoder;
    $this->productTypeConfig = $productTypeConfig;
    $this->string = $string;
    $this->_localeFormat = $localeFormat;
    $this->customerSession = $customerSession;
    $this->productRepository = $productRepository;
    $this->priceCurrency = $priceCurrency;
    $this->customProduct    = $_customProduct;
    parent::__construct(
        $context,
        $data
    );
}

    protected function _toHtml()
    {           
        $this->setModuleName($this->extractModuleName('Magento\Catalog\Block\Product\View'));

        $this->getLayout()->getBlock('custom.product')->setTemplate('Myaddon_CustomProduct::custom.phtml');

        return parent::_toHtml();
    }

    public function getCustomProduct(){

        $a = $this->customProduct->create()->getCollection();
        echo count($a); die('xxx');

    }
}

Hope this helps !!

4
  • tks you. $this->customProduct must be an instance of Myaddon\CustomProduct\Model\CustomProduct. if not declare $this->customProduct->getCollection() return?
    – xanka
    Commented Nov 23, 2015 at 10:23
  • i try this. but get error same with __construct() CustomProductFactory $_customProduct.
    – xanka
    Commented Nov 23, 2015 at 10:41
  • This error [2015-11-23 17:39:17] main.CRITICAL: Recoverable Error: Argument 2 passed to Myaddon\CustomProduct\Block\Product\Custom::__construct() must be an instance of Myaddon\CustomProduct\Model\CustomProductFactory, instance of Magento\Framework\Url\Encoder given, called in C:\localhost\magento2-2.0.0-rc\var\generation\Myaddon\CustomProduct\Block\Product\Custom\Interceptor.php on line 14 and defined in C:\localhost\magento2-2.0.0-rc\app\code\Myaddon\CustomProduct\Block\Product\Custom.php on line 24 [] []
    – xanka
    Commented Nov 23, 2015 at 10:44
  • ok pls use updated construct function.. most probably that should work but if that doesnt work either then sorry i cant figure out what is causing the problem. Commented Nov 23, 2015 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.