Skip to content

Commit 93e69ca

Browse files
committed
Add variant info everywhere, fix manufacturer
1 parent 4ce4bf6 commit 93e69ca

1 file changed

Lines changed: 50 additions & 5 deletions

File tree

classes/Wrapper/ProductWrapper.php

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
namespace PrestaShop\Module\Ps_Googleanalytics\Wrapper;
2222

2323
use Configuration;
24+
use Db;
2425
use Context;
2526
use PrestaShop\PrestaShop\Adapter\Presenter\Product\ProductLazyArray;
2627
use PrestaShop\PrestaShop\Adapter\Presenter\Product\ProductListingLazyArray;
2728
use Product;
2829
use Shop;
29-
use Tools;
3030

3131
class ProductWrapper
3232
{
@@ -80,7 +80,7 @@ public function prepareItemListFromProductList($productList, $useProvidedQuantit
8080
* @return array Item data standardized for GA
8181
*/
8282
public function prepareItemFromProduct($product, $useProvidedQuantity = false)
83-
{
83+
{
8484
// Standardize product ID
8585
$product_id = 0;
8686
if (!empty($product['id_product'])) {
@@ -106,18 +106,29 @@ public function prepareItemFromProduct($product, $useProvidedQuantity = false)
106106
// Add manufacturer info if we have it
107107
if (!empty($product['manufacturer_name'])) {
108108
$item['item_brand'] = $product['manufacturer_name'];
109-
// TODO - missing in some events?
109+
// If we don't, which can happen due to some bugs in getProductProperties, we will fetch it manually
110+
} else if (!empty($product['id_manufacturer'])) {
111+
$manufacturerName = Manufacturer::getNameById((int) $product['id_manufacturer']);
112+
if (!empty($manufacturerName)) {
113+
$item['item_brand'] = $manufacturerName;
114+
}
110115
}
111116

112117
// We will specify variant ID if we have it
113118
if (!empty($product['id_product_attribute'])) {
114119
$item['item_id'] .= '-' . $product['id_product_attribute'];
115120
}
116121

117-
// Information about a chosen variant, if we have it
122+
// Information about a chosen variant, if we have it (cart list has this out of the box)
118123
if (!empty($product['attributes_small'])) {
119124
$item['item_variant'] = $product['attributes_small'];
120-
// TODO - get manually if missing and we have id_product_attribute
125+
126+
// If we don't, we will construct it in the same format
127+
} else if (!empty($product['id_product_attribute'])) {
128+
$variant = $this->getProductVariant((int) $product['id_product_attribute']);
129+
if (!empty($variant)) {
130+
$item['item_variant'] = $variant;
131+
}
121132
}
122133

123134
if ($useProvidedQuantity === true) {
@@ -149,4 +160,38 @@ public function prepareItemFromProduct($product, $useProvidedQuantity = false)
149160

150161
return $item;
151162
}
163+
164+
/**
165+
* Method that will provide product combination attribute in the same format and order as cart does.
166+
*
167+
* @param int $id_product_attribute ID of the combination
168+
*
169+
* @return string Attribute list
170+
*/
171+
public function getProductVariant($id_product_attribute)
172+
{
173+
$result = Db::getInstance()->executeS(
174+
'SELECT al.`name` AS attribute_name
175+
FROM `' . _DB_PREFIX_ . 'product_attribute_combination` pac
176+
LEFT JOIN `' . _DB_PREFIX_ . 'attribute` a ON a.`id_attribute` = pac.`id_attribute`
177+
LEFT JOIN `' . _DB_PREFIX_ . 'attribute_group` ag ON ag.`id_attribute_group` = a.`id_attribute_group`
178+
LEFT JOIN `' . _DB_PREFIX_ . 'attribute_lang` al ON (
179+
a.`id_attribute` = al.`id_attribute`
180+
AND al.`id_lang` = ' . (int) $this->context->language->id . '
181+
)
182+
WHERE pac.`id_product_attribute` = ' . $id_product_attribute . '
183+
ORDER BY ag.`position` ASC, a.`position` ASC'
184+
);
185+
186+
$attributes = array_column($result, 'attribute_name');
187+
188+
// Prepare our separator
189+
$separator = Configuration::get('PS_ATTRIBUTE_ANCHOR_SEPARATOR');
190+
if ($separator === '-') {
191+
// Add a space before the dash between attributes
192+
$separator = ' - ';
193+
}
194+
195+
return implode($separator, $attributes);
196+
}
152197
}

0 commit comments

Comments
 (0)