Skip to content

Commit

Permalink
v1.0.5 optional data
Browse files Browse the repository at this point in the history
  • Loading branch information
nghelo committed Oct 11, 2022
1 parent 8a116f8 commit 4d03c9b
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 109 deletions.
198 changes: 99 additions & 99 deletions Block/Product/ProductList/Related.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @category Tun2U
* @package Tun2U_AutoRelated
Expand All @@ -15,102 +16,101 @@
class Related extends \Magento\Catalog\Block\Product\ProductList\Related
{

/**
* @var \Magento\Core\Model\Factory\Helper
*/
protected $_helper;


/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;


/**
* @var \Magento\Framework\Registry
*/
protected $_registry;


/**
* @var \Tun2U\AutoRelated\Model\Collection
*/
protected $_collection;


public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Checkout\Model\ResourceModel\Cart $checkoutCart,
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Framework\Module\Manager $moduleManager,
array $data = [],
ObjectManagerInterface $helperFactory,
Collection $collection
) {
$this->_helper = $helperFactory;
$this->_registry = $context->getRegistry();
$this->_storeManager = $context->getStoreManager();
;
$this->_collection = $collection;
parent::__construct(
$context,
$checkoutCart,
$catalogProductVisibility,
$checkoutSession,
$moduleManager,
$data
);

// Only cache if we have something thats keyable..
$_time = $this->_helper->get('Tun2U\AutoRelated\Helper\Data')->get_cache_lifetime();

if ($_time > 0 && $cacheKey = $this->_cacheKey()) {
$this->addData(array(
'cache_lifetime' => $_time,
'cache_tags' => array(\Magento\Store\Model\Store::CACHE_TAG),
'cache_key' => $cacheKey,
));
}
}

protected function _cacheKey()
{
$product = $this->_registry->registry('product');

if ($product) {
return get_class() . '::' . $this->_storeManager->getStore()->getCode() . '::' . $product->getId();
}

return false;
}

protected function _prepareData()
{
parent::_prepareData();

$_enabled = $this->_helper->get('Tun2U\AutoRelated\Helper\Data')->get_enabled();

if ($_enabled && count($this->getItems()) == 0) {
$_products = $this->_collection->getRelatedProducts();
if ($_products) {
$this->_itemCollection = $_products;
}
}

return $this;
}

public function getIdentities()
{
$identities = [];
//Bugfix: Warning: Invalid argument supplied for foreach()
if (is_array($this->getItems()) || is_object($this->getItems())) {
foreach ($this->getItems() as $item) {
$identities = array_merge($identities, $item->getIdentities());
}
}
return $identities;
}
}
/**
* @var \Magento\Core\Model\Factory\Helper
*/
protected $_helper;


/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;


/**
* @var \Magento\Framework\Registry
*/
protected $_registry;


/**
* @var \Tun2U\AutoRelated\Model\Collection
*/
protected $_collection;


public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Checkout\Model\ResourceModel\Cart $checkoutCart,
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Framework\Module\Manager $moduleManager,
ObjectManagerInterface $helperFactory,
Collection $collection,
array $data = []
) {
$this->_helper = $helperFactory;
$this->_registry = $context->getRegistry();
$this->_storeManager = $context->getStoreManager();;
$this->_collection = $collection;
parent::__construct(
$context,
$checkoutCart,
$catalogProductVisibility,
$checkoutSession,
$moduleManager,
$data
);

// Only cache if we have something thats keyable..
$_time = $this->_helper->get('Tun2U\AutoRelated\Helper\Data')->get_cache_lifetime();

if ($_time > 0 && $cacheKey = $this->_cacheKey()) {
$this->addData(array(
'cache_lifetime' => $_time,
'cache_tags' => array(\Magento\Store\Model\Store::CACHE_TAG),
'cache_key' => $cacheKey,
));
}
}

protected function _cacheKey()
{
$product = $this->_registry->registry('product');

if ($product) {
return get_class() . '::' . $this->_storeManager->getStore()->getCode() . '::' . $product->getId();
}

return false;
}

protected function _prepareData()
{
parent::_prepareData();

$_enabled = $this->_helper->get('Tun2U\AutoRelated\Helper\Data')->get_enabled();

if ($_enabled && count($this->getItems()) == 0) {
$_products = $this->_collection->getRelatedProducts();
if ($_products) {
$this->_itemCollection = $_products;
}
}

return $this;
}

public function getIdentities()
{
$identities = [];
//Bugfix: Warning: Invalid argument supplied for foreach()
if (is_array($this->getItems()) || is_object($this->getItems())) {
foreach ($this->getItems() as $item) {
$identities = array_merge($identities, $item->getIdentities());
}
}
return $identities;
}
}
23 changes: 13 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
{
"name": "tun2u/m2-autorelated",
"description": "Auto generate related products for Magento 2",
"keywords": ["magento2","autorelated","related","products"],
"keywords": [
"magento2",
"autorelated",
"related",
"products"
],
"homepage": "https://github.com/Tun2U/M2-AutoRelated",
"type": "magento2-module",
"version": "1.0.4",
"version": "1.0.5",
"minimum-stability": "stable",
"license": "GPL-3.0",
"authors": [
{
"name": "Tun2U S.r.l.",
"email": "[email protected]",
"homepage": "https://www.tun2u.com"
}
],
"authors": [{
"name": "Tun2U S.r.l.",
"email": "[email protected]",
"homepage": "https://www.tun2u.com"
}],
"support": {
"email": "[email protected]"
},
Expand All @@ -28,4 +31,4 @@
"Tun2U\\AutoRelated\\": ""
}
}
}
}

0 comments on commit 4d03c9b

Please sign in to comment.