<?php
/**
* Landofcoder
*
* NOTICE OF LICENSE
*
* This source file is subject to the Landofcoder.com license that is
* available through the world-wide-web at this URL:
* https://landofcoder.com/license-agreement.html
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Landofcoder
* @package Lofmp_Blog
* @copyright Copyright (c) 2016 Landofcoder (https://landofcoder.com/)
* @license https://landofcoder.com/LICENSE-1.0.html
*/
namespace Lofmp\Blog\Block\Post;
use Lofmp\Blog\Model\Post;
class ProductsRelated extends \Magento\Catalog\Block\Product\AbstractProduct implements \Magento\Widget\Block\BlockInterface
{
/**
* @var \Lofmp\Blog\Helper\Data
*/
protected $_blogHelper;
/**
* @var mixed
*/
protected $_postsBlock;
/**
* @var mixed
*/
protected $_collection;
/**
* Catalog product visibility
*
* @var \Magento\Catalog\Model\Product\Visibility
*/
protected $catalogProductVisibility;
/**
* Product collection factory
*
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
*/
protected $productCollectionFactory;
/**
* @param \Magento\Framework\View\Element\Template\Context
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
* @param \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility
* @param \Lofmp\Blog\Helper\Data $blogHelper
* @param array
*/
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
\Lofmp\Blog\Helper\Data $blogHelper,
array $data = []
)
{
$this->_blogHelper = $blogHelper;
$this->productCollectionFactory = $productCollectionFactory;
$this->catalogProductVisibility = $catalogProductVisibility;
parent::__construct($context, $data);
}
/**
* @inheritdoc
*/
public function _toHtml()
{
$post = $this->getPost();
if ($post) {
$postId = $post->getId();
//$productsIds = $post->getProducts();
$collection = $this->productCollectionFactory->create();
$collection->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds());
$collection = $this->_addProductAttributesAndPrices($collection);
$collection->getSelect()
->join(
['post_products_table' => $collection->getResource()->getTable("lofmp_blog_post_products_related")],
'e.entity_id = post_products_table.entity_id',
[
'post_id'
]
)
->where('post_products_table.post_id = ?', (int)$postId)
->group(
'e.entity_id'
);
//->addAttributeToFilter('entity_id', ['in' => $productsIds])
$collection->setCurPage(1);
$this->setCollection($collection);
return parent::_toHtml();
}
return "";
}
/**
* get current post data
*
* @param \Lofmp\Blog\Model\Post|mixed
*/
public function getPost()
{
$post = $this->_coreRegistry->registry('current_post');
return $post;
}
/**
* Set post collection
* @param \Lofmp\Blog\Model\ResourceModel\Post\Collection
* @return $this
*/
public function setCollection($collection)
{
$this->_collection = $collection;
return $this;
}
/**
* Get post collection
* @return \Lofmp\Blog\Model\ResourceModel\Post\Collection
*/
public function getCollection()
{
return $this->_collection;
}
/**
* Return HTML block with price
*
* @param \Magento\Catalog\Model\Product $product
* @return string
*/
public function getLofmpProductPriceHtml(
\Magento\Catalog\Model\Product $product,
$priceType = null,
$renderZone = \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
array $arguments = []
) {
if (!isset($arguments['zone'])) {
$arguments['zone'] = $renderZone;
}
$arguments['price_id'] = isset($arguments['price_id'])
? $arguments['price_id']
: 'old-price-' . $product->getId() . '-' . $priceType;
$arguments['include_container'] = isset($arguments['include_container'])
? $arguments['include_container']
: true;
$arguments['display_minimal_price'] = isset($arguments['display_minimal_price'])
? $arguments['display_minimal_price']
: true;
$priceRender = $this->getLayout()->getBlock('product.price.render.default');
$price = '';
if ($priceRender) {
$price = $priceRender->render(
\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
$product,
$arguments
);
}
return $price;
}
}
|