0

I am developing a module and would like to put a block on the product page, immediately underneath the short description and above the dropdown selection. I have looked at every single answered question on this site and none of the options work. I honestly have no idea why this should be so difficult but I have spent half a day trying various solutions and can get it near where I want it but not where I actually want it.

Magento 2.3, developing a module (not a theme), I just need to put my block immediately beneath product short description.

This is the closest I can get:

app/code/MyVendor/MyModule/frontend/layout/catalog_product_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product.info.options.wrapper.bottom">
            <block class="Magento\Framework\View\Element\Template"
                   name="product.info.my_block_name"
                   template="MyVendor_MyModule::template_name.phtml"
                   before="-" />
        </referenceBlock>
    </body>
</page>

This code puts the block above the quantity field but still below the selection dropdowns which I don't want. I'm assuming something else needs to go into referenceBlock name but I can't find it.

2 Answers 2

0

Which theme are you using?

The default catalog_product_view.xml shows that the Short description appears at the bottom of product.info.main

The below should work:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product.info.main">
            <block class="Magento\Framework\View\Element\Template"
                   name="product.info.my_block_name"
                   template="MyVendor_MyModule::template_name.phtml"
                   after="product.info.overview" />
        </referenceBlock>
    </body>
</page>
3
  • My theme is by Template Monster, it's called Magetigue. When I use the code above it puts the block all the way at the end of all the product info (underneath add to cart and wish list). So my theme is overriding the default layout then?
    – S Adamson
    May 7, 2020 at 15:44
  • Okay I switched to Luma just to see and I can see you're correct, the short description is already at the very end. It seems that my template overrides this. In my setup the short description is underneath price and sku and above the dropdowns. I found this line in my theme's catalog_product_view: <move element="product.info.overview" destination="product.info.main" after="product.info.price"/> What should I use to put my new block after the overview in its new changed location?
    – S Adamson
    May 7, 2020 at 15:51
  • referenceBlock product.info.main and after product.info.price (just like my theme did with the short description in the first place) did fix my problem. Thank you for pointing me in the right direction!
    – S Adamson
    May 7, 2020 at 16:15
0

For this, you can override attribute file and apply condition as per your need.

First you have to create a catalog_product_view.xml under 'MyVendor/MyModule/view/frontend/layout/' folder and add below code in it.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>       
        <referenceBlock name="product.info.overview">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">MyVendor_MyModule::attribute.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

Now, Create attribute.phtml under 'MyVendor/MyModule/view/frontend/templates/' folder and add below code. You can add condition as per attribute code & attribute value in this file.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis

/**
 * Product view template
 *
 * @var $block \Magento\Catalog\Block\Product\View\Description
 */
?>
<?php
$_helper = $this->helper(Magento\Catalog\Helper\Output::class);
$_product = $block->getProduct();

if (!$_product instanceof \Magento\Catalog\Model\Product) {
    return;
}

$_call = $block->getAtCall();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();

$renderLabel = true;
// if defined as 'none' in layout, do not render
if ($_attributeLabel == 'none') {
    $renderLabel = false;
}

if ($_attributeLabel && $_attributeLabel == 'default') {
    $_attributeLabel = $_product->getResource()->getAttribute($_code)->getStoreLabel();
}
if ($_attributeType && $_attributeType == 'text') {
    $_attributeValue = ($_helper->productAttribute($_product, $_product->$_call(), $_code))
        ? $_product->getAttributeText($_code)
        : '';
} else {
    $_attributeValue = $_helper->productAttribute($_product, $_product->$_call(), $_code);
}
?>

<?php if ($_attributeValue) :?>
<div class="product attribute <?= $block->escapeHtmlAttr($_className) ?>">
    <?php if ($renderLabel) :?>
        <strong class="type"><?= $block->escapeHtml($_attributeLabel) ?></strong>
    <?php endif; ?>
    <div class="value" <?= /* @noEscape */ $_attributeAddAttribute ?>><?= /* @noEscape */ $_attributeValue ?></div>
</div>
<?php endif; ?>
<?php if($_code === 'short_description') { echo "test"; } ?>

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.