1

When a product gets clicked on -> the product details page opens and shows detailed information and images of the product. I want to display (programmatically) some custom product attributes(which I created via admin panel) on this page under the 'SKU#: sku123' section.

What I have done:

1.edit 'vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml'

<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.demo" template="product/view/warranty.phtml" after="product.info.price">   <arguments>
                        <argument name="at_call" xsi:type="string">getWarranty</argument>
                        <argument name="at_code" xsi:type="string">warranty</argument>
                        <argument name="css_class" xsi:type="string">warranty </argument>
                        <argument name="at_label" xsi:type="string">warranty </argument>
                        <argument name="add_attribute" xsi:type="string">itemprop="warranty "</argument>
                    </arguments>
            </block>

2.create warranty.phtml

<?php 
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct(); // get current(selected) product object ?
$_code = $block->getAtCode();  
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();

if ($_attributeLabel && $_attributeLabel == 'default') {
$_attributeLabel = $_product->getResource()->getAttribute($_code)-   >getFrontendLabel();
}
$_attributeValue =$_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);
?>
<?php if ($_attributeValue): ?>
<div class="product attibute <?php echo $_className?>">
<?php if ($_attributeLabel != 'none'): ?><strong class="type"><?php echo     $_attributeLabel?>:</strong><?php endif; ?>
&ensp;<?php echo "test ". $_attributeValue; ?>
</div>
<?php endif; ?>

Code in Description.php:

    /**
 * @return Product
 */
public function getProduct()
{
    if (!$this->_product) {
        $this->_product = $this->_coreRegistry->registry('product');
    }
    return $this->_product;
}

This solves my problem, I can display my custom attribute 'warranty'.

What I understand: With registry-object we can set and get global variables. Here we get the global variable ('product'). And in phtml file we get the attribute value(from 'warranty') via this product-object.

What I dont understand: Who did set this global variable('product') ? How does _coreRegistry->registry('product'); magically give us the object of the currently (by the user) selected product ?

Also: Is there another way of getting the product-object (which corresponds to the currently selected product by the user) ?

I'm a little bit confused. Thanks in advance !

1 Answer 1

0

The registry value for 'product' is set when the Magento\Catalog\Controller\Product\View controller executes. More specifically, it occurs in Magento\Catalog\Helper\Product->initProduct. From what I can tell, the product Id and category Id are passed in as part of the actual product URL, which is rewritten to the product's url key. From the product view controller:

// Get initial data from request
        $categoryId = (int) $this->getRequest()->getParam('category', false);
        $productId = (int) $this->getRequest()->getParam('id');
        $specifyOptions = $this->getRequest()->getParam('options');
5
  • thanks for the reply ! Where can I read the documentation for the classes Magento\Catalog\Controller\Product\View and Magento\Catalog\Helper\Product and other classes ? Oct 29, 2016 at 9:09
  • On the official devdocs.magento site I can't find the documentations for classes and methods Oct 29, 2016 at 9:16
  • There's no documentation for individual classes and methods as far as I know. Oct 29, 2016 at 9:32
  • That's sad. Do you know any good articles(websites) where I can learn more about magento 2 development. I'm having a hard time getting into this topic. How did you start digging into this topic ? Oct 29, 2016 at 10:29
  • Alan Storm has a series of articles on magento 2 development on his site, I would recommend going there. There's a book, magento 2 developer's guide, that is useful although based on an older version so it's a little out of date now. Oct 29, 2016 at 13:04

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.