Skip to content

Commit 1187a7f

Browse files
authored
Merge pull request #47 from landofcoder/develop
updated files
2 parents df080b9 + 9ac030b commit 1187a7f

File tree

7 files changed

+66
-11
lines changed

7 files changed

+66
-11
lines changed

Block/AbstractWidget.php

100644100755
File mode changed.

Block/Tag/Product/TagProduct.php

+47-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
namespace Lof\ProductTags\Block\Tag\Product;
2525

26+
use Magento\Catalog\Model\Product;
27+
use Magento\Framework\Registry;
28+
use Magento\Framework\Exception\LocalizedException;
29+
2630
class TagProduct extends \Magento\Framework\View\Element\Template
2731
{
2832
protected $resultPageFactory;
@@ -33,16 +37,28 @@ class TagProduct extends \Magento\Framework\View\Element\Template
3337

3438
protected $_tagHelper;
3539

40+
/**
41+
* @var Registry
42+
*/
43+
protected $registry;
44+
45+
/**
46+
* @var Product
47+
*/
48+
private $product;
49+
3650
public function __construct(
3751
\Magento\Framework\View\Element\Template\Context $context,
3852
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
3953
\Lof\ProductTags\Model\TagFactory $tagFactory,
4054
\Lof\ProductTags\Helper\Data $tagdata,
55+
Registry $registry,
4156
array $data = []
4257
) {
4358
$this->resultPageFactory = $resultPageFactory;
4459
$this->_tagFactory = $tagFactory;
4560
$this->_tagHelper = $tagdata;
61+
$this->registry = $registry;
4662
parent::__construct($context, $data);
4763
}
4864
public function _toHtml(){
@@ -54,21 +70,43 @@ public function _toHtml(){
5470
}
5571
return "";
5672
}
73+
74+
/**
75+
* @return Product
76+
*/
77+
private function getProduct()
78+
{
79+
if (is_null($this->product)) {
80+
$this->product = $this->registry->registry('product');
81+
82+
if (!$this->product->getId()) {
83+
throw new LocalizedException(__('Failed to initialize product'));
84+
}
85+
}
86+
87+
return $this->product;
88+
}
89+
5790
function getTagHelper(){
5891
return $this->_tagHelper;
5992
}
6093
public function getTagCollection()
6194
{
6295
if(!$this->_tagcollection){
63-
$limit = $this->_tagHelper->getGeneralConfig('number_tags');
64-
$limit = $limit?(int)$limit:10;
65-
$tag = $this->_tagFactory->create();
66-
$collection = $tag->getCollection();
67-
$collection->addFieldToFilter("status", 1);
68-
$collection->setOrder("tag_id","DESC");
69-
$collection->setPageSize($limit);
70-
//$collection->setLimit($limit);
71-
$this->_tagcollection = $collection;
96+
$product = $this->getProduct();
97+
$product_id = $product->getId();
98+
if($product_id){
99+
$limit = $this->_tagHelper->getGeneralConfig('number_tags');
100+
$limit = $limit?(int)$limit:10;
101+
$tag = $this->_tagFactory->create();
102+
$collection = $tag->getCollection();
103+
$collection->addFieldToFilter("status", 1);
104+
$collection->addProductToFilter($product_id);
105+
$collection->setOrder("tag_id","DESC");
106+
$collection->setPageSize($limit);
107+
//$collection->setLimit($limit);
108+
$this->_tagcollection = $collection;
109+
}
72110
}
73111
return $this->_tagcollection;
74112
}

Controller/Router.php

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public function __construct(
2727
}
2828
public function match(\Magento\Framework\App\RequestInterface $request)
2929
{
30-
die("Awetae");
3130
if (!$this->dispatched) {
3231

3332
$identifier = trim($request->getPathInfo(), '/');

Model/ResourceModel/AbstractCollection.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ protected function performAddStoreFilter($store, $withAdmin = true)
142142

143143
$this->addFilter('store', ['in' => $store], 'public');
144144
}
145+
145146

146147
/**
147148
* Join store relation table if there is store filter
@@ -157,13 +158,30 @@ protected function joinStoreRelationTable($tableName, $linkField)
157158
['store_table' => $this->getTable($tableName)],
158159
'main_table.' . $linkField . ' = store_table.' . $linkField,
159160
[]
160-
)->group(
161+
)->where('')->group(
161162
'main_table.' . $linkField
162163
);
163164
}
165+
164166
parent::_renderFiltersBefore();
165167
}
166168

169+
170+
public function addProductToFilter($product_id = 0){
171+
if($product_id) {
172+
$this->getSelect()->join(
173+
['product_table' => $this->getTable('lof_producttags_product')],
174+
'main_table.tag_id = product_table.tag_id',
175+
[]
176+
)->where('product_table.product_id = ?', (int)$product_id)->group(
177+
'main_table.tag_id'
178+
);
179+
}
180+
return $this;
181+
}
182+
183+
184+
167185
/**
168186
* Get SQL for get record count
169187
*

Samples/Lof_product_tags_sample.json

100644100755
File mode changed.

view/frontend/layout/default.xml

100644100755
File mode changed.

view/frontend/web/css/styles.css

100644100755
File mode changed.

0 commit comments

Comments
 (0)