For upsell products in Magento2, how can edit the text above the upsell products that says: "We found other products you might like!"
I would like it to say "Customers also viewed"
magento2upselling
For upsell products in Magento2, how can edit the text above the upsell products that says: "We found other products you might like!"
I would like it to say "Customers also viewed"
I recently implemented something very similar and will share the relevant code below. But first some comments about performance, since that was the question:
ORDER BY RAND()
because it results in a resource intensive temp table copy. It has to load all results into a temporary table, assign a random number to each row and then sort without any index. Instead we retrieve all candidate ids (this is faster and the amount of data is managable even for large catalogs), pick some randomly and retrieve these rows directly. You can read about it in detail in my blog: http://www.schmengler-se.de/en/2015/09/show-random-products-in-magento-you-are-doing-it-wrong/addAttributeToSelect('*')
with EAV collections, only select what you neednew Zend_Db_Expr('FIELD(category_id, ' . implode(',', $_cat_order).')'))
can efficiently use the MySQL indexesThis is an observer for the catalog_product_upsell
event. It uses the product collection provided by the product's category because I didn't need to fall back to the parent, but I'm sure you can adjust it.
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_AutoUpsell_Model_Observer
{
/**
* @see event catalog_product_upsell
* @param Varien_Event_Observer $observer
* @throws Mage_Core_Exception
*/
public function fillUpsellCollection(Varien_Event_Observer $observer)
{
$collection = $observer->getCollection();
if ($collection instanceof RelatedProductCollection
&& $collection->getLinkModel()->getLinkTypeId() === RelatedProduct::LINK_TYPE_UPSELL
&& $collection->count() < $observer->getLimit('upsell')
) {
$this->addItemsFromCategory($collection, $observer->getLimit('upsell') - $collection->count(), $observer->getProduct());
}
}
protected function addItemsFromCategory(RelatedProductCollection $collection, $numberOfItems, Product $product)
{
/** @var Mage_Catalog_Model_Resource_Product_Collection $productsToAdd */
$productsToAdd = $this->_getProductCategory($product)->getProductCollection();
$productsToAdd
->addStoreFilter()
->addIdFilter(array_merge([$product->getId()], $collection->getAllIds()), true)
->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$candidateIds = $productsToAdd->getAllIds();
$choosenIds = [];
$maxKey = count($candidateIds)-1;
while (count($choosenIds) < $numberOfItems)) {
$randomKey = mt_rand(0, $maxKey);
$choosenIds[$randomKey] = $candidateIds[$randomKey];
}
$productsToAdd
->addIdFilter($choosenIds)
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addUrlRewrite();
foreach ($productsToAdd as $product) {
$collection->addItem($product);
}
}
/**
* @param Mage_Catalog_Model_Product $product
* @return Mage_Catalog_Model_Category
*/
protected function _getProductCategory(Product $product)
{
$category = $product->getCategoryCollection()
->setPageSize(1)
->getFirstItem();
return $category;
}
}
->addIdFilter(array_merge($product->getId(), $collection->getAllIds()), true)
This excludes the product itself and the products that are already manually defined as upsell products (I leave them and only fill the blanks with random products)
->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.
By default, Magento 2 shows the most viewed product in Magento Admin > DASHBOARD > choose tab Most Viewed Products. The _prepareCollection()
method in vendor/magento/module-backend/Block/Dashboard/Tab/Products/Viewed.php
is used for selecting products.
We can copy some code lines from _prepareCollection()
above. For example, in your block, use the following code below:
class Index extends \Magento\Framework\View\Element\Template
{
/**
* @var \Magento\Reports\Model\ResourceModel\Product\CollectionFactory
*/
protected $_productsFactory;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory,
array $data = []
) {
$this->_productsFactory = $productsFactory;
parent::__construct($context, $data);
}
/**
* Getting most viewed products
*/
public function getCollection()
{
$currentStoreId = $this->_storeManager->getStore()->getId();
$collection = $this->_productsFactory->create()
->addAttributeToSelect(
'*'
)->addViewsCount()->setStoreId(
$currentStoreId
)->addStoreFilter(
$currentStoreId
);
$items = $collection->getItems();
return $items;
}
}
The initial class \Magento\Reports\Model\ResourceModel\Product\CollectionFactory
is the most important class because this class will get our products report. The getCollection
method will return the most viewed items by current store.
In your template, you can get the items. For example:
<?php
$items = $this->getCollection();
foreach($items as $item) {
echo $item->getName() .'<br/>';
}
?>
Best Answer
With Custom Theme:
Override
items.phtml
in your theme, Copyitems.phtml
From:
vendor/magento/module-catalog/view/frontend/templates/product/list/items.phtml
To
app/design/frontend/Vendor/theme/Magento_Catalog/templates/produc‌t/list/items.phtml
Now change
to
Flush cache and try.
With Custom Module:
Create
catalog_index_index.xml
atapp/code/Vendor/Module/view/frontend/layout
Copy
vendor/magento/module-catalog/view/frontend/templates/product/list/items.phtml
toapp/code/Vendor/Module/view/frontend/templates/product/list/items.phtml
Now change
to
Flush cache and try.