0

I want to load another Product on the catalog product view page using the the default Magento\Catalog\Block\Product\View Block and trying to load the product via SKU in an custom template file

<block class="Magento\Catalog\Block\Product\View" name="product.bundle.options" as="bundle.options" template="product/view/bundle-options.phtml" group="detailed_info">

I thought I could use the View.php because it uses the ProductRepository inside Magento\Catalog\Block\Product\View.php:

use Magento\Catalog\Api\ProductRepositoryInterface Line: 8

ProductRepositoryInterface $productRepository Line: 89


and inside the Magento\Catalog\Api\ProductRepositoryInterface.php resides the function

 /**
 * Get info about product by product SKU
 *
 * @param string $sku
 * @param bool $editMode
 * @param int|null $storeId
 * @param bool $forceReload
 * @return \Magento\Catalog\Api\Data\ProductInterface
 * @throws \Magento\Framework\Exception\NoSuchEntityException
 */
public function get($sku, $editMode = false, $storeId = null, $forceReload = false);

Now trying to load the a product in the template file

<?php
    $sku = '235039';
    $product = $this->productRepository->get($sku);
    var_dump($product);
?>

Result: Page Breaks

trying to use $this->_productRepository->get($sku); (with underscore) give me this output:

1 exception(s): Exception #0 (Exception): Notice: Undefined property: Magento\Catalog\Block\Product\View\Interceptor::$_productRepository in /var/www/<magentoroot>/vendor/magento/framework/View/TemplateEngine/Php.php on line 110

Question:

do i have missed something? Why can't I load the product using the get Function. And I do not want to Integrate the Object Manager because the ProductRepository should be included in the View.php and I also do not want to create a own block using the Dependency Injection, just so I can load a product. I just want to use the default View.php. It should be possible, right?

1 Answer 1

1

The most useful action in this scenario is getting your error reporting correct in php, so you can actually see whats wrong.

If you look into the block class again, you will see that the productRepository variable is protected. You cannot just access it outside of the class.

/**
 * @var ProductRepositoryInterface
 */
protected $productRepository;

I believe your options are:

  • use object manager
  • extend the block and add function that will do this for you
  • look if there is perhaps a helper in catalog/bundle module that can do this for you
1
  • Aww thanks, kinda missed that.Okay I will create my own block and adding this function, that's enough information for me
    – juhanix
    Sep 16, 2016 at 11:40

Your Answer

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

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