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?
-
please check in which layout you override.– MsquareCommented Apr 2, 2020 at 8:59
1 Answer
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
-
-
-
I updated the answer. If you don't know how to create a custom module you can take a look here: inchoo.net/magento-2/how-to-create-a-basic-module-in-magento-2 Commented Apr 2, 2020 at 11:40
-
well, somehow i get an 404 cant find cant find that page Commented Apr 2, 2020 at 12:20