Magento 2.2: How to remove add_to_wishlist button from, and add SKU view in, recently viewed products widget in module
-
05-02-2021 - |
Question
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
{
}
Solution
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