9
9
class Tag extends \Magento \Framework \Model \ResourceModel \Db \AbstractDb
10
10
{
11
11
protected $ _tagProductTable = '' ;
12
-
12
+
13
13
protected function _construct ()
14
14
{
15
15
$ this ->_init ('lof_producttags_tag ' , 'tag_id ' );
16
16
}
17
-
17
+
18
18
/**
19
19
* Process page data after saving
20
20
*
@@ -69,7 +69,7 @@ protected function _saveTagProducts($tag)
69
69
70
70
$ connection = $ this ->getConnection ();
71
71
72
- /**
72
+ /**
73
73
* Delete products from tag
74
74
*/
75
75
if (!empty ($ delete )) {
@@ -107,7 +107,7 @@ protected function _saveTagProducts($tag)
107
107
108
108
foreach ($ newPositions as $ delta => $ productIds ) {
109
109
$ bind = ['position ' => new \Zend_Db_Expr ("position + ( {$ delta }) " )];
110
- $ where = ['tag_id = ? ' => (int )$ id , 'product_id IN (?) ' => $ productIds ];
110
+ $ where = ['tag_id = ? ' => (int )$ id , 'product_id IN (?) ' => $ productIds ];
111
111
$ connection ->update ($ this ->getTagProductTable (), $ bind , $ where );
112
112
}
113
113
}
@@ -125,8 +125,36 @@ protected function _saveTagProducts($tag)
125
125
$ tag ->setAffectedProductIds ($ productIds );
126
126
}
127
127
128
+ $ productCount = $ this ->getProductCount ($ tag );
129
+ $ tableName = $ this ->getTable ('lof_producttags_tag ' ); //gives table name with prefix
130
+ $ connection ->update (
131
+ ['main_table ' => $ tableName ],
132
+ ['number_products ' => (int )$ productCount ],['tag_id = ? ' => (int )$ id ]);
128
133
return $ this ;
129
134
}
135
+ /**
136
+ * Count product selected on Product Tags
137
+ */
138
+ public function getProductCount ($ tag )
139
+ {
140
+ $ productTable = $ this ->getTable ('lof_producttags_product ' );
141
+
142
+ $ select = $ this ->getConnection ()->select ()->from (
143
+ ['main_table ' => $ productTable ],
144
+ [new \Zend_Db_Expr ('COUNT(main_table.product_id) ' )]
145
+ )->where (
146
+ 'main_table.tag_id = :tag_id '
147
+ );
148
+
149
+ $ bind = ['tag_id ' => (int )$ tag ->getId ()];
150
+ $ counts = $ this ->getConnection ()->fetchOne ($ select , $ bind );
151
+
152
+ return intval ($ counts );
153
+ }
154
+
155
+ /**
156
+ * Save Tag Store
157
+ */
130
158
protected function _saveTagStores ($ tag )
131
159
{
132
160
$ oldStores = $ this ->lookupStoreIds ($ tag ->getId ());
@@ -150,9 +178,7 @@ protected function _saveTagStores($tag)
150
178
}
151
179
return $ this ;
152
180
}
153
-
154
181
155
-
156
182
/**
157
183
* Perform operations after object load
158
184
*
@@ -164,6 +190,8 @@ protected function _afterLoad(\Magento\Framework\Model\AbstractModel $object)
164
190
if ($ object ->getId ()) {
165
191
$ stores = $ this ->lookupStoreIds ($ object ->getId ());
166
192
$ object ->setData ('store_id ' , $ stores );
193
+ // $productCount = $this->getProductCount($object);
194
+ // print_r($productCount);die();
167
195
}
168
196
return parent ::_afterLoad ($ object );
169
197
}
0 commit comments