Add the following into your template file,
$myBlock = \Magento\Framework\App\ObjectManager::getInstance()->get('VendorName\ModuleName\Block\Product\CrossSellItems');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$currentProduct = $myBlock->getCurrentProduct();
if ($currentProduct = $myBlock->getCurrentProduct()) {
$crossSellProducts = $currentProduct->getCrossSellProducts();
if (!empty($crossSellProducts)) {
?>
<div class="category-right product-parts">
<h2><span id="_CrossSellDisplay_TitleLabel">Cross Sell Products</span></h2>
<div class="row">
<?php
$store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();
foreach ($crossSellProducts as $crossSellProduct) {
$product = $this->getLoadProduct($crossSellProduct->getId());
$imageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();
?>
<li class="col-sm-3 col-lg-3">
<div class="sub-cat-tile">
<a href="<?php echo $product->getProductUrl() ?>">
<img src="<?php echo $imageUrl ?>">
</a>
</div>
<h4 class="subcat-title">
<a href="<?php echo $product->getProductUrl() ?>"><?php echo $product->getName() ?></a>
</h4>
<p>
<?php $product->getPrice() ?>
</p>
</li>
<?php } ?>
</div>
</div>
<?php
} else
{ echo "<div class=\"category-right product-parts\">No Product Found!</div>"; }
}
?>
In your module create Block/Product/CrossSellItems.php class and add the following,
<?php
namespace VendorName\ModuleName\Block\Product;
class CrossSellItems extends \Magento\Framework\View\Element\Template {
protected $_registry; protected $_productloader;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Catalog\Model\ProductFactory $_productloader,
array $data = []
)
{
$this->_registry = $registry;
$this->_productloader = $_productloader;
parent::__construct($context, $data);
}
public function _prepareLayout()
{
return parent::_prepareLayout();
}
public function getCurrentProduct()
{
return $this->_registry->registry('current_product');
}
public function getLoadProduct($id)
{
return $this->_productloader->create()->load($id);
}
}