Magento 2.3.0 How to override wishlist & compare product list page using plugin
-
17-02-2021 - |
Question
I want to override wishlist & compare product grid using plugin. after that i would like to append custom div under class="product-image-container"
.
I try & search a lot but no luck. is there any way to achieve thing?
I try below way for wishlist grid.
Namespace/Modulename/etc/forntend/di.xml
<type name="Magento\Wishlist\Block\Customer\Wishlist\Items">
<plugin name="Namespace_Modulename::wishlistProduct" type="namespace\Modulename\Plugin\Customer\Wishlist\Items" sortOrder="18" />
</type>
Namespace/Modulename/Plugin/Customer/Wishlist/items.php
namespace Namespace\Modulename\Plugin\Customer\Wishlist;
class Items
{
/**
* @var \Magento\Framework\View\LayoutInterface
*/
protected $layout;
public function __construct(
\Magento\Framework\View\LayoutInterface $layout
) { //die('test called');
$this->layout = $layout;
}
public function afterGetColumns(\Magento\Wishlist\Block\Customer\Wishlist\Items $subject, $result
) {
var_dump($subject->getProduct());die('called');
$labelHtml = $this->layout->createBlock('Namespace\Modulename\Block\Label')->setProduct($product)->setTemplate('Namespace_Modulename::product/label.phtml')->toHtml();
return $result.$labelHtml;
}
}
What i missing or wrong in above code?
Edit :
How can i create after plugin for class Magento\Catalog\Block\Product\Compare\ListCompare
?
I try with below way but not working.
<type name="Magento\Catalog\Block\Product\Compare\ListCompare">
<plugin disabled="false" name="Namespace_Modulename::compareProduct" type="Namespace\Modulename\Plugin\Catalog\Product\Compare\ListCompare" sortOrder="19" />
</type>
Any help would be appreciated!
Solution
I found solution using below way.
For Wishlist product page.
app/code/Namespace/Modulename/etc/frontend/di.xml
<type name="Magento\Wishlist\Block\Customer\Wishlist\Item\Column\Image">
<plugin name="Namespace_Modulename::wishlistProduct" type="Namaespace\Modulename\Plugin\Customer\Wishlist\Item\Column\Image" sortOrder="18" />
</type>
app/code/Namespace/Modulename/Plugin/Customer/Wishlist/Item/Column/Image.php
<?php
namespace Namespace\Modulename\Plugin\Customer\Wishlist\Item\Column;
use Namespace\Modulename\Helper\Data;
class Image
{
/**
* @var Data
*/
private $dataHelper;
/**
* @var \Magento\Framework\Registry
*/
private $registry;
/**
* @var \Magento\Framework\View\LayoutInterface
*/
protected $layout;
public function __construct(
\Magento\Framework\View\LayoutInterface $layout,
\Magento\Framework\Registry $registry,
Data $dataHelper
) {
$this->layout = $layout;
$this->registry = $registry;
$this->dataHelper = $dataHelper;
}
public function afterToHtml(
\Magento\Wishlist\Block\Customer\Wishlist\Item\Column\Image $subject,
$result
) {
$item = $subject->getItem();
$product = $item->getProduct();
if($product){
if($this->dataHelper->isEnabled()){
$result .= $this->layout->createBlock('Namespace\Modulename\Block\Label')->setProduct($product)->setTemplate('Namespace_Modulename::product/label.phtml')->toHtml();
}
}
return $result;
}
}
For Compare product page.
app/code/Namespace/Modulename/etc/frontend/di.xml
<type name="Magento\Catalog\Block\Product\Compare\ListCompare">
<plugin name="Namespace_Modulename::compareProduct" type="Namespace\Modulename\Plugin\Catalog\Product\Compare\ListCompare" sortOrder="19" />
</type>
app/code/Namespace/Modulename/Plugin/Catalog/Product/Compare/ListCompare.php
<?php
namespace Namespace\Modulename\Plugin\Catalog\Product\Compare;
use Namespace\Modulename\Helper\Data;
class ListCompare
{
/**
* @var Data
*/
private $dataHelper;
/**
* @var \Magento\Framework\View\LayoutInterface
*/
protected $layout;
public function __construct(
\Magento\Framework\View\LayoutInterface $layout,
Data $dataHelper
) {
$this->layout = $layout;
$this->dataHelper = $dataHelper;
}
public function afterGetReviewsSummaryHtml(
\Magento\Catalog\Block\Product\Compare\ListCompare $subject, $result,
\Magento\Catalog\Model\Product $product,
$templateType = false,
$displayIfNoReviews = false
) {
if($product){
if($this->dataHelper->isEnabled()){
$result .= $this->layout->createBlock('Namespace\Modulename\Block\Label')->setProduct($product)->setTemplate('Namespace_Modulename::product/label.phtml')->toHtml();
}
}
return $result;
}
}