4

I am trying to remove default product detail page content, can i load custom phtml file for product detail page along with the image?

I need to remove all the default blocks of product detail page and show only my template content.

Is that can be done? Because i need to custom code in that template file to interact with external api with the product id.

How this can be achieved? Please anyone suggest me.

  • Yes , you can override a view files and achieve what you are looking for . create custom module/theme for this and override the core files . Please go through this it might help you - classyllama.com/blog/template-override-m2 – Rahul Makwana Sep 14 '18 at 15:06
  • You can remove the blocks from xml for your theme. – Sukumar Gorai Sep 14 '18 at 15:08
  • Please can you update me what code i need to add in catalog_product_view.xml file? – jafar pinjar Sep 14 '18 at 15:11
  • Hello @jafarpinjar, did you got your solution ? – Pawan Sep 14 '18 at 16:35
  • no, i am trying the answer which is posted here, and confirm you once it worked. – jafar pinjar Sep 15 '18 at 6:33
2

There is no need of module, You just need to create a catalog_product_view.xml at:

app/design/frontend/[Package]/[theme]/Magento_Catalog/layout/catalog_product_view.xml

<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="content">
        <block class="Magento\Catalog\Block\Product\View" name="custom.product.view" template="Magento_Catalog::product/customview.phtml" before="-"/>
    </referenceContainer>
    <referenceContainer name="product.info.main" remove="true"></referenceContainer>
    <referenceContainer name="product.info.media" remove="true"></referenceContainer>
    <referenceContainer name="product.info.details" remove="true"></referenceContainer>
    <referenceBlock name="page.main.title" remove="true" />
</body>
</page>

Create a customview.phtml at:

app/design/frontend/[Package]/[theme]/Magento_Catalog/templates/product/customview.phtml

<?php $product = $block->getProduct(); ?>
<?php echo $product->getName();?> <!-- whatever you want -->

Result will be: enter image description here

  • Nice, i used your code, its working great, but small change i need , i have created custom attribute for product called "is_custom", if that value is yes then i need to load this file else i need to use default one. is that can be checked and use? – jafar pinjar Sep 17 '18 at 6:25
  • Hello @jafarpinjar, Good to know that it worked. If you want to load this layout based on condition you can follow emiprotechnologies.com/news/magento-technical-notes-60/post/… – Pawan Sep 17 '18 at 16:50
  • if still you found any issue, Please let me know. – Pawan Sep 17 '18 at 16:50
  • hello @pavan, i couldn't get from the above link, What i need exactly is i need to use that layout file code based on the product attribute value, i have created product attribute called 'is_custom' if it is set as Yes then i need to load the above phtml else i need to use default magento product detail page, is that can be done? – jafar pinjar Sep 18 '18 at 6:35
2

This is to just give an idea, please change the code accordingly as the code is neither complete or verified.

Setup a new magento 2 extension.

Create catalog_product_view.xml on the following path

app/code/{YourVendor}/{YourExtension}/view/frontend/layout/catalog_product_view.xml

with something like

<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="content">
        <block class="{YourVendor}\{YourModule}\Block\Product\View" name="{vendor}.{module}.product.view" template="product/view.phtml" before="-"/>
        <referenceContainer name="product.info.main" remove="true"></referenceContainer>
    </referenceContainer>
</body>

Now Create a Block file View.php on path

app/code/{YourVendor}/{YourModule}/Block/Product/View.php

Extend magento's base block class for product view, something like

namespace {YourVendor}\{YourModule}\Block\Product;
class View extends \Magento\Catalog\Block\Product\View {
    // DEFINE FUNCTIONS TO SUIT YOUR REQUIREMENTS OR OVERRIDE FUNCTIONS FROM VIEW CLASS
}

Also create the template file view.phtml on path

app/code/{YourVendor}/{YourExtension}/view/frontend/templates/product/view.phtml

You can use the functions you defined in the View Block class

  • hi. @Hammad, I tried your code, from that i can only remove name and price details, but still i can see image and review section, i need to remove entirely all the default blocks of product detail page , ans i need to add my own code there, is that possible? – jafar pinjar Sep 15 '18 at 10:06
  • @jafarpinjar, if you open the main catalog_product_view.xml file, you can see a lot of block created there, you will have to remove them as well. it is a good approach if you use the newly created xml file to remove them, rather than removing them from the main file – Hammad Khalid Sep 17 '18 at 11:48
  • hi, @Hammad, yes i used your code only but, as i asked is it possible to do like that? – jafar pinjar Sep 17 '18 at 14:33
  • @jafarpinjar, that is what i mean with my above comment.. it is possible, you just have to remove the default blocks loaded on the product pages – Hammad Khalid Sep 17 '18 at 15:44

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.