@@ -41,10 +41,11 @@ public function __construct(Context $context)
4141 * Takes provided list of product (lazy) arrays and converts it to a format that GA4 requires.
4242 *
4343 * @param array $productList
44+ * @param bool $useProvidedQuantity Should provided quantity be used, usually for cart related events
4445 *
4546 * @return array Item data standardized for GA
4647 */
47- public function prepareItemListFromProductList ($ productList , $ isCartItem = false )
48+ public function prepareItemListFromProductList ($ productList , $ useProvidedQuantity = false )
4849 {
4950 $ items = [];
5051
@@ -56,7 +57,7 @@ public function prepareItemListFromProductList($productList, $isCartItem = false
5657 // Prepare each item and override the counter
5758 $ counter = 0 ;
5859 foreach ($ productList as $ product ) {
59- $ product = $ this ->prepareItemFromProduct ($ product , $ isCartItem );
60+ $ product = $ this ->prepareItemFromProduct ($ product , $ useProvidedQuantity );
6061 $ product ['index ' ] = $ counter ;
6162 $ items [] = $ product ;
6263 ++$ counter ;
@@ -71,12 +72,14 @@ public function prepareItemListFromProductList($productList, $isCartItem = false
7172 * - ProductListingLazyArray from presented listings
7273 * - ProductListingLazyArray from presented cart
7374 * - Raw $cart->getProducts()
75+ * - Legacy product object converted to an array enriched with Product::getProductProperties
7476 *
7577 * @param ProductLazyArray|ProductListingLazyArray|array $product
78+ * @param bool $useProvidedQuantity Should provided quantity be used, usually for cart related events
7679 *
7780 * @return array Item data standardized for GA
7881 */
79- public function prepareItemFromProduct ($ product , $ isCartItem = false )
82+ public function prepareItemFromProduct ($ product , $ useProvidedQuantity = false )
8083 {
8184 // Standardize product ID
8285 $ product_id = 0 ;
@@ -103,23 +106,23 @@ public function prepareItemFromProduct($product, $isCartItem = false)
103106 // Add manufacturer info if we have it
104107 if (!empty ($ product ['manufacturer_name ' ])) {
105108 $ item ['item_brand ' ] = $ product ['manufacturer_name ' ];
109+ // TODO - missing in some events?
106110 }
107111
108- if ( $ isCartItem === true ) {
109- if (!empty ($ product ['id_product_attribute ' ])) {
110- $ item ['item_id ' ] .= '- ' . $ product ['id_product_attribute ' ];
111- }
112+ // We will specify variant ID if we have it
113+ if (!empty ($ product ['id_product_attribute ' ])) {
114+ $ item ['item_id ' ] .= '- ' . $ product ['id_product_attribute ' ];
115+ }
112116
113- // Info about quantity in cart, if we have it
114- if (isset ($ product ['cart_quantity ' ])) {
115- $ item ['quantity ' ] = $ product ['quantity ' ];
116- }
117+ // Information about a chosen variant, if we have it
118+ if (!empty ($ product ['attributes_small ' ])) {
119+ $ item ['item_variant ' ] = $ product ['attributes_small ' ];
120+ // TODO - get manually if missing and we have id_product_attribute
121+ }
117122
118- // In case of products from a cart, we will add more information
119- // Information about a chosen variant, if we have it
120- if (!empty ($ product ['attributes_small ' ])) {
121- $ item ['item_variant ' ] = $ product ['attributes_small ' ];
122- }
123+ if ($ useProvidedQuantity === true ) {
124+ // Info about quantity in cart, if we have it
125+ $ item ['quantity ' ] = $ product ['quantity ' ];
123126 }
124127
125128 // Prepare category information, put default category as the main one
0 commit comments