1

I encountered the situation where I needed to use a slightly different/modified view.phtml, list.phtml and page template for specific category, so I found the following solution:

I had used the following code (which I inserted in Catalog -> Manage Categories -> Custom Layout Update) and I set up the option: Applay to Product to Yes!

Here is my 1rst part of code and explanation:

<reference name="product_list">
<action method="setTemplate"> <template>catalog/product/list_custom.phtml</template></action>
</reference>

Above code is working great and because I wanted to have a slightly different list view templajt I created a new file called list_custom.phtml and there make some changes.

Here is my 2nd part of code and explanation:

<reference name="product.info">
<action method="setTemplate"> <template>catalog/product/view_custom.phtml</template></action>
</reference>

Also, above code is working great and because I wanted to have a slightly different view templajt I created a new file called view_custom.phtml and there make some changes. This is OK!

I, also, use the following code:

<reference name="root">
<action method="setTemplate"><template>page/custom_list.phtml</template></action>
</reference>

Here is where I need help :(

Above code is listening all the products as I want to, but when I click on the product I want that product (from specific category) to have different/custom page layout! Now, it is opened in custom_list.phtml templejt.

0

Using the product id in your layout, you can use the following layout handle to make modification to your page for this specific product:

<catalog_product_view_id_productid>

Or using the product sku, you can use the same method with the following layout handle:

<catalog_product_view_sku_productsku>

For more reference check : here

  • I will try this, but quick question, please? What if I want to show not only one specific product, but all the products from specific category? – user15151 Jun 27 '18 at 10:01
  • @user15151 I have not idea for that case. – Nikunj Vadariya Jun 27 '18 at 10:06
0

Create plugins for the list and view blocks. So for the product_list block, you would create a di.xml file in a module with these contents:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Block\Product\ListProduct">
        <plugin name="your_module_catalog_productlist_block_plugin" type="Your_Vendor\Your_Module\Block\Catalog\Product\ListProductPlugin"/>
    </type>
</config>

Then create a plugin file app/code/Your_Vendor/Your_Module/Block/Catalog/Product/ListProductPlugin.php with these contents:

<?php    
namespace Your_Vendor\Your_Module\Block\Catalog\Product;

use \Magento\Catalog\Block\Product\ListProduct;

class ListProductPlugin
{
    /**
     * @param \Magento\Catalog\Block\Product\ListProduct $subject
     */
    public function beforeToHtml(ListProduct $subject)
    {
        if (/*Your conditions go here*/) {
            $subject->setTemplate('your_template_file.phtml');
        }

        return;
    }
}

For the other blocks you would add another element in the di.xml file and create a similar plugin class.

Hope it helps.

  • Hi Rimšiakas, thank you for your replay. To be honest, I can't follow your :( – user15151 Jun 27 '18 at 14:03
  • alanstorm.com/magento_2_object_manager_plugin_system In this link you'll find a pretty good explanation on how to work with plugins, in case that's the problem. – Rimšiakas Jun 27 '18 at 14:17
  • Great, I will take a look at it. Thanks one more time. – user15151 Jun 27 '18 at 14:19

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.