64 lines
2.4 KiB
PHP
Executable File
64 lines
2.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace IpSupply\HotPick\Block\Widget;
|
|
|
|
use Magento\Framework\View\Element\Template;
|
|
use Magento\Widget\Block\BlockInterface;
|
|
use Magento\Framework\View\Element\Template\Context;
|
|
use Magento\Framework\App\ObjectManager;
|
|
use Magento\Catalog\Model\Product\Visibility;
|
|
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
|
|
use Magento\Catalog\Block\Product\Context as ProductContext;
|
|
|
|
class HotPickWidget extends Template implements BlockInterface {
|
|
protected $_objectManager;
|
|
protected $_catalogProductVisibility;
|
|
protected $_productCollectionFactory;
|
|
protected $_imageBuilder;
|
|
|
|
public function __construct(Context $context, Visibility $_catalogProductVisibility,
|
|
CollectionFactory $_productCollectionFactory, ProductContext $productContext, array $data = []) {
|
|
$this->_objectManager = ObjectManager::getInstance();
|
|
$this->_catalogProductVisibility = $_catalogProductVisibility;
|
|
$this->_productCollectionFactory = $_productCollectionFactory;
|
|
$this->_imageBuilder = $productContext->getImageBuilder();
|
|
parent::__construct($context, $data);
|
|
}
|
|
|
|
/**
|
|
* Retrieve product image
|
|
*
|
|
* @param \Magento\Catalog\Model\Product $product
|
|
* @param string $imageId
|
|
* @param array $attributes
|
|
* @return \Magento\Catalog\Block\Product\Image
|
|
*/
|
|
public function getImage($product, $imageId, $attributes = [])
|
|
{
|
|
return $this->_imageBuilder->create($product, $imageId, $attributes);
|
|
}
|
|
|
|
function getAssetUrl($asset) {
|
|
$assetRepository = $this->_objectManager->get('Magento\Framework\View\Asset\Repository');
|
|
return $assetRepository->createAsset($asset)->getUrl();
|
|
}
|
|
|
|
function getProductHotPick() {
|
|
$visibleProducts = $this->_catalogProductVisibility->getVisibleInCatalogIds();
|
|
$collection = $this->_productCollectionFactory->create()->setVisibility($visibleProducts);
|
|
$collection->addMinimalPrice()
|
|
->addFinalPrice()
|
|
->addTaxPercents()
|
|
->addAttributeToSelect('*')
|
|
->addStoreFilter($this->getStoreId())
|
|
->setPageSize($this->getProductsCount())
|
|
->addAttributeToFilter('is_featured', '1');
|
|
return $collection;
|
|
}
|
|
|
|
function getPriceFormat($price) {
|
|
$priceHelper = $this->_objectManager->create('Magento\Framework\Pricing\Helper\Data');
|
|
$formatedPrice = $priceHelper->currency($price, true, false);
|
|
return $formatedPrice;
|
|
}
|
|
} |