0

I have two situations that I cant solve and need help.

I'm trying to show installments information in my product page, so I've created a new module, with a block, a helper (to get module configs) and a simple template file, so I link the template with a block throuh the catalog_product_view.xml layout file:

<?xml version="1.0"?>
<page layout="2columns-right" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="content">
        <referenceContainer name="product.info.price">
            <block class="Vendor\Module\Block\Parcelamento" after="product.price.final"
                name="product.info.price.parcelamento.block"
                template="Vendor_Module::list.phtml">
            </block>
        </referenceContainer>
    </referenceContainer>
</body>

The first situation is: See the after property, the block should be showed bellow the price info, but it is showed on top, see the arrows in my print: enter image description here

The second situation is: In my print, see de price value as zero (red marked), this occurs because my Block PHP needs my product instance, and how can I pass the product object to my block using layout files? Is that possible? I've read somethings about <arguments> tags inside a <block>, but I think it is just for simple arguments, like strings.

Any help is apreciated.

Thanks in advance.

  • 1
    Try to discuss only 1 issue per thread. That helps everyone keep track of the discussion. – nshiff Feb 28 '18 at 19:19
  • thanks for your suggestion, you think it is a problem? If yes, I can edit my question and keep only one issue. – lpFranz Feb 28 '18 at 19:37
1

Regarding your second situation, on product pages Magento stores a reference to the current product object in the registry. You can get it from your block by injecting an instance of the registry. Here is an example of the code needed :

/**
 * @var Registry
 */
private $coreRegistry;
/**
 * @var ProductInterface
 */
private $product;
public function __construct(Registry $coreRegistry)
{
    $this->coreRegistry = $coreRegistry;
}
/**
 * @return ProductInterface
 * @throws NotFoundException
 */
public function getProduct() : ProductInterface
{
    if (!$this->product) {
        $product = $this->coreRegistry->registry('current_product');
        if (!$product instanceof ProductInterface) {
            throw new NotFoundException(__("Current product not found, check that this block is shown in the correct page."));
        }
        $this->product = $product;
    }
    return $this->product;
}

Then you can get the product object from the template by calling $block->getProduct()

Note: Full path to classes used in the code above are:

use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Registry;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\View\Element\Block\ArgumentInterface;
| improve this answer | |
  • Thanks, it solves my problem, but I think it is not the right way to do this, am I right? It is like use global variables. I'll use it for while, but if there is another way to do this, I want to try. But thanks so much! – lpFranz Mar 1 '18 at 12:43
  • Yes I agree with you, but it's the Magento way as far as I know unfortunately. – デリエゴくん Mar 2 '18 at 8:31

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.