I am trying to get cross sell products in cart page programmatically.
Below code i have used in layout/checkout_cart_index.xml
<referenceContainer name="content">
<block class="Vendor\Module\Block\Customcross" name="cross.custom.details" after="-" template="Vendor_Module::cart/cart_products.phtml"/>
</referenceContainer>
So I need to write one function in my Block file that should return cross sell items.
Right now i am using like below in block file.
namespace Vendor\Module\Block;
use Magento\Catalog\Api\ProductRepositoryInterface;
use \Magento\Catalog\Block\Product\AbstractProduct;
class Customcross extends \Magento\Framework\View\Element\Template
{
protected $_storeManager;
protected $_productCollectionFactory;
protected $_coreRegistry;
protected $_urlInterface;
protected $_productloader;
protected $_productRepository;
protected $_productData;
protected $_itemCollection;
protected $_catalogConfig;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Registry $coreRegistry,
\Magento\Framework\UrlInterface $urlInterface,
\Magento\Catalog\Model\ProductFactory $_productloader,
ProductRepositoryInterface $productRepository,
\Magento\Catalog\Model\Product $productData,
\Magento\Catalog\Model\Config $catalogConfig
)
{
$this->_productCollectionFactory = $productCollectionFactory;
$this->_storeManager = $storeManager;
$this->_coreRegistry = $coreRegistry;
$this->_urlInterface = $urlInterface;
$this->_productloader = $_productloader;
$this->_productRepository = $productRepository;
$this->_productData = $productData;
$this->_catalogConfig = $catalogConfig;
parent::__construct($context);
}
public function getCurrencySymbol() {
return $this->_storeManager->getStore()->getBaseCurrency()->getCurrencySymbol();
}
public function getBasetUrl() {
return $this->_storeManager->getStore()->getBaseUrl();
}
public function getCurrentUrl() {
return $this->_urlInterface->getCurrentUrl();
}
public function getStore()
{
return $this->_storeManager->getStore();
}
public function getProductData($productId)
{
$productObj = $this->_productData->load($productId);
return $productObj;
}
protected function _prepareData()
{
$product = $this->_coreRegistry->registry('product');
/* @var $product \Magento\Catalog\Model\Product */
$this->_itemCollection = $product->getCrossSellProductCollection()->addAttributeToSelect(
$this->_catalogConfig->getProductAttributes()
)->setPositionOrder()->addStoreFilter();
$this->_itemCollection->load();
foreach ($this->_itemCollection as $product) {
$product->setDoNotUseCategoryId(true);
}
return $this;
}
public function getItems()
{
return $this->_itemCollection;
}
}
Anyone help me. How to get the cross sell items like this.
Best Answer
I haven't tested it, but perhaps you can do something similar to vendor/magento/module-catalog/Block/Product/ProductList/Crosssell.php
_prepareData()
function: