0

I have following problem: In my case i have to override my grouped.phtml file BUT not for every group product. Is it possible to have multiple grouped.phtml files and that i can choose witch of them should be loaded for witch site?

1
  • please check in which layout you override.
    – Msquare
    Commented Apr 2, 2020 at 8:59

1 Answer 1

0

Yes it's possible. You have to extend \Magento\GroupedProduct\Block\Product\View\Type\Grouped and insert your conditions in the setTemplate method.

Block: app/code/Vendor/Module/Block/Product/View/Type/Grouped.php

namespace Vendor\Module\Block\Product\View\Type;

/**
 * Class Grouped
 *
 * @package Vendor\Module\Block\Product\View\Type
 */
class Grouped extends \Magento\GroupedProduct\Block\Product\View\Type\Grouped
{
    /**
     * @param string $template
     * @return \Magento\Catalog\Block\Product\View\AbstractView
     */
    public function setTemplate($template)
    {
        $contition = 'something';
        if ($contition == 'test'){
            $template = "Vendor_Module::product/view/type/grouped-test.phtml";
        }

        if ($contition == 'hello'){
            $template = "Vendor_Module::product/view/type/grouped-hello.phtml";
        }
        return parent::setTemplate($template);
    }
}

app/code/Vendor/Module/view/frontend/layout/catalog_product_view_type_grouped.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>
        <referenceContainer name="product.info.form.content">
            <block class="Vendor\Module\Block\Product\View\Type\Grouped" name="product.info.grouped" before="product.info.addtocart"/>
        </referenceContainer>
    </body>
</page>

Than create template files: app/code/Vendor/Module/view/frontend/templates/product/view/type/grouped-test.phtml app/code/Vendor/Module/view/frontend/templates/product/view/type/grouped-hello.phtml

Or you can also create a plugin method for setTemplate without editing the layout file and set your custom template on afterSetTemplate. You can find more information about plugins here: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/plugins.html

Hope it helps

4

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.