How to display add to block on related products similar to it is displaying on list page?
Magento 2 – Add to Cart and Wishlist on Related Products
addtocartmagento2magento2.2.4related-products
Related Solutions
By default Magento, there is no setting option in Admin for Add to Cart button on the related products block. If we want to custom the related, upsell, crosssel and new product template, we should override this template vendor/magento/module-catalog/view/frontend/templates/product/list/items.phtml
.
Change $showCart = true;
in case - related product.
We can refer here: Related Products template customize
You can write an observer for catalog_product_collection_load_after
, then add products to the loaded collection if the collection is the related products collection:
use Mage_Catalog_Model_Product as Product;
use Mage_Catalog_Model_Product_Link as RelatedProduct;
use Mage_Catalog_Model_Resource_Product_Link_Product_Collection as RelatedProductCollection;
class IntegerNet_AutoRelated_Model_Observer
{
/**
* @see event catalog_product_collection_load_after
* @param Varien_Event_Observer $observer
* @throws Mage_Core_Exception
*/
public function addToRelatedCollection(Varien_Event_Observer $observer)
{
$collection = $observer->getCollection();
if ($collection instanceof RelatedProductCollection
&& $collection->getLinkModel()->getLinkTypeId() === RelatedProduct::LINK_TYPE_RELATED
) {
$this->addItems($collection);
}
}
protected function addItems(RelatedProductCollection $collection)
{
/** @var Mage_Catalog_Model_Resource_Product_Collection $productsToAdd */
$productsToAdd = Mage::getResourceModel('catalog/product_collection');
$productsToAdd
->addStoreFilter()
->addIdFilter(array_diff([1,5,7,3], [$collection->getProduct()->getId()], $collection->getAllIds()))
->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->setPageSize($numberOfItems)
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addUrlRewrite();
foreach ($productsToAdd as $product) {
$collection->addItem($product);
}
}
}
Some parts that I'd like to highlight:
->addIdFilter(array_diff([1,5,7,3], [$collection->getProduct()->getId()], $collection->getAllIds()))
This loads the products [1,5,7,3]
but excludes the product itself and the products that are already manually defined as related products. Otherwise we would get an error due to duplicates in the collection. You probably want to move these hard coded ids to a configuration.
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addUrlRewrite();
This prepares the product collection to load the necessary data to display prices, the product link and any attributes configured as "used in product listing", but not more.
This is a very similar solution as in my AutoUpsell module described in Get product list by category id in view.phtml, I copied most of the code from there with slight modifications.
Best Answer
I am assuming you want to show add to cart, add to wish-list just like as on list page for related product on product view page.
As you know for showing
related
,upsell
product there is a common file located atCopy above file to your theme, if not exist.
You can see in this phtml there is
switch case
for related and upsell product with some option.There is already Add to cart button is available in this phtml, but with following condition:
if ($showAddTo || $showCart):
That means you need to set
true
to both above variables. Search $showAddTo and $showCart for related case and you will need to change$showCart = true;
from
$showCart = false;
Above code will enable add to cart.
Note: If you need to hide add to cart checkbox and its label, it can be easily hide using CSS