0

I want a extra tab on product page with 4 previously viewed products. Besides that I want to remove "add_to_cart,add_to_compare,add_to_wishlist" buttons and add SKU attribute to recently viewed products in my module.

Probably not the right approach, but I do get 4 recently viewed Products. I have this code now:

Knot\PreviousTab\registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Knot_PreviousTabPDP',
__DIR__
);

Knot\PreviousTabPDP\etc\module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Knot_PreviousTabPDP" setup_version="1.0.0">
    </module>
</config>

Knot/PreviousTabPDP/view/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.details">
        <block class="Knot\PreviousTabPDP\Block\PreviousProductsTab" name="previous_products.tab" as="previous_products_tab" template="Knot_PreviousTabPDP::previous_products.phtml" group="detailed_info">
            <arguments>
                <argument name="title" translate="true" xsi:type="string">Previous Products</argument>
            </arguments>
        </block>
    </referenceBlock>
</body>

Knot/PreviousTabPDP/view/frontend/templates/previous_products.phtml

<?php 
echo $this->getLayout()
->createBlock("Magento\Reports\Block\Product\Widget\Viewed")
->setDisplayType("recently.view.products")
->setPageSize("4")
->setTemplate("widget/viewed/content/viewed_grid.phtml")->toHtml()
; ?>

and a empty

Knot/PreviousTabPDP/Block/PreviousProductsTab.php

<?php

namespace Knot\PreviousTabPDP\Block;

use Magento\Catalog\Model\Product;

class PreviousProductsTab extends \Magento\Framework\View\Element\Template
{

}
0

Based on your approach, first you need to copy the template file to your current theme and make the needed customization.

Copy the file located in vendor/magento/module-reports/view/frontend/templates/widget/viewed/content/viewed_grid.phtml

To app/design/frontend/ThemeVendor/YourThemeName/Magento_Reports/templates/widget/viewed/content/viewed_grid.phtml

Now around line 26 make the below changes

$showWishlist = false; $showCompare = false; $showCart = false;

To add the Sku around line 50 add the below code SKU:- escapeHtml($_item->getSku()) ?>

Final output will look as shown in the screenshot
https://prnt.sc/lbo12b

  • Thank you for your answer. Is there a way to do this with module only, keep it separated from Themes? – Bart Oct 29 '18 at 8:06
  • @Bart Of course it is possible, instead of copying the template file to theme directory you can place it in your module's view directory. Change your previous_products.phtml file with the below content <code>echo $this->getLayout() ->createBlock("Magento\Reports\Block\Product\Widget\Viewed") ->setDisplayType("recently.view.products") ->setPageSize("4") ->setTemplate("Knot_PreviousTabPDP::widget/viewed/content/viewed_grid.phtml")->toHtml(); </code> – mydons Oct 29 '18 at 9:13

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.