2323
2424namespace Lof \ProductTags \Block \Tag \Product ;
2525
26+ use Magento \Catalog \Model \Product ;
27+ use Magento \Framework \Registry ;
28+ use Magento \Framework \Exception \LocalizedException ;
29+
2630class 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 }
0 commit comments