From 571c6dd4e72ecb5425f7baf58d8a219d8d1e09b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Frankiewicz?= Date: Tue, 9 Jul 2024 14:59:14 +0200 Subject: [PATCH 1/5] adds product model created hook --- src/Hook/Hooks.php | 3 +++ src/Model/Product.php | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/Hook/Hooks.php b/src/Hook/Hooks.php index 35dcb84..79e83ae 100644 --- a/src/Hook/Hooks.php +++ b/src/Hook/Hooks.php @@ -18,4 +18,7 @@ class Hooks /** @var string */ public const TC_DISPLAY_BEFORE_GTM_HEAD_SNIPPET = 'tcDisplayBeforeGtmHeadSnippet'; + + /** @var string */ + public const TC_ACTION_PRODUCT_MODEL_CREATED = 'tcActionProductModelCreated'; } diff --git a/src/Model/Product.php b/src/Model/Product.php index 517eee6..6324aa1 100644 --- a/src/Model/Product.php +++ b/src/Model/Product.php @@ -6,11 +6,13 @@ use Category; use Context; use Manufacturer; +use PrestaShop\Module\TagConciergeFree\Hook\Hooks; use Tools; +use Hook; class Product { - /** @var int */ + /** @var int|string */ private $id; /** @var string */ @@ -28,13 +30,16 @@ class Product /** @var string */ private $variant; + /** @var int */ + private $variantId; + /** @var int */ private $minimalQuantity; /** @var int */ private $stockQuantity; - public function getId(): int + public function getId() { return $this->id; } @@ -44,7 +49,7 @@ public function getId(): int * * @return $this */ - public function setId(int $id): self + public function setId($id): self { $this->id = $id; @@ -156,6 +161,18 @@ public function setMinimalQuantity(int $minimalQuantity): self return $this; } + public function getVariantId(): int + { + return $this->variantId; + } + + public function setVariantId(int $variantId): self + { + $this->variantId = $variantId; + + return $this; + } + public function toArray(): array { return [ @@ -165,6 +182,7 @@ public function toArray(): array 'brand' => $this->getBrand(), 'category' => $this->getCategory(), 'variant' => $this->getVariant(), + 'variant_id' => $this->getVariantId(), 'stock_quantity' => $this->getStockQuantity(), 'minimal_quantity' => $this->getMinimalQuantity(), ]; @@ -188,6 +206,10 @@ public static function fromArray($array): self $array['attributes'] = ''; } + if (false === isset($array['id_product_attribute']) || false === is_int($array['id_product_attribute'])) { + $array['id_product_attribute'] = 0; + } + if (true === \is_array($array['attributes'])) { $attributes = array_map(static function ($attribute) { return Tools::strtolower(trim( @@ -204,14 +226,19 @@ public static function fromArray($array): self $calledClass = get_called_class(); - return (new $calledClass()) + $product = (new $calledClass()) ->setId($array['id_product']) ->setName(Tools::replaceAccentedChars($array['name'])) ->setPrice((float) ($array['price_amount'] ?? $array['price'])) ->setBrand($manufacturer->name ?? '') ->setCategory($category->name ?? '') ->setVariant($variant) + ->setVariantId($array['id_product_attribute']) ->setStockQuantity($array['quantity']) ->setMinimalQuantity($array['minimal_quantity']); + + Hook::exec(Hooks::TC_ACTION_PRODUCT_MODEL_CREATED, ['product' => $product]); + + return $product; } } From c06d6febff4fbc7e9de28f34d3b1f3d377f7f822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Frankiewicz?= Date: Tue, 9 Jul 2024 15:24:07 +0200 Subject: [PATCH 2/5] adds frontend assets hook --- src/Hook/Hooks.php | 6 ++++-- src/Model/Product.php | 4 +--- .../hooks/add_to_cart/display_before_body_closing_tag.tpl | 1 + views/templates/hooks/frontend_assets/display_header.tpl | 5 ++++- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Hook/Hooks.php b/src/Hook/Hooks.php index 79e83ae..916e6c2 100644 --- a/src/Hook/Hooks.php +++ b/src/Hook/Hooks.php @@ -19,6 +19,8 @@ class Hooks /** @var string */ public const TC_DISPLAY_BEFORE_GTM_HEAD_SNIPPET = 'tcDisplayBeforeGtmHeadSnippet'; - /** @var string */ - public const TC_ACTION_PRODUCT_MODEL_CREATED = 'tcActionProductModelCreated'; + /** + * @var string + */ + public const TC_DISPLAY_AFTER_FRONTEND_ASSETS = 'tcDisplayAfterFrontendAssets'; } diff --git a/src/Model/Product.php b/src/Model/Product.php index 6324aa1..13fa84b 100644 --- a/src/Model/Product.php +++ b/src/Model/Product.php @@ -236,9 +236,7 @@ public static function fromArray($array): self ->setVariantId($array['id_product_attribute']) ->setStockQuantity($array['quantity']) ->setMinimalQuantity($array['minimal_quantity']); - - Hook::exec(Hooks::TC_ACTION_PRODUCT_MODEL_CREATED, ['product' => $product]); - + return $product; } } diff --git a/views/templates/hooks/add_to_cart/display_before_body_closing_tag.tpl b/views/templates/hooks/add_to_cart/display_before_body_closing_tag.tpl index 2a62fea..02bcbcb 100644 --- a/views/templates/hooks/add_to_cart/display_before_body_closing_tag.tpl +++ b/views/templates/hooks/add_to_cart/display_before_body_closing_tag.tpl @@ -15,6 +15,7 @@ brand: product['manufacturer_name'], category: product['category'], variant: attributes.join('___'), + variant_id: product['id_product_attribute'], stock_quantity: parseInt(product['stock_quantity']), minimal_quantity: parseInt(product['minimal_quantity']), cart_quantity: parseInt(product['cart_quantity']), diff --git a/views/templates/hooks/frontend_assets/display_header.tpl b/views/templates/hooks/frontend_assets/display_header.tpl index bc204c8..a80005a 100644 --- a/views/templates/hooks/frontend_assets/display_header.tpl +++ b/views/templates/hooks/frontend_assets/display_header.tpl @@ -1,3 +1,4 @@ +{literal} +{/literal} +{hook h='tcDisplayAfterFrontendAssets'} \ No newline at end of file From 3002679e03b82de96b707bdfc3634ac797a3f95a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Frankiewicz?= Date: Tue, 9 Jul 2024 15:25:38 +0200 Subject: [PATCH 3/5] restores typing --- src/Model/Product.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Model/Product.php b/src/Model/Product.php index 13fa84b..816e4bf 100644 --- a/src/Model/Product.php +++ b/src/Model/Product.php @@ -12,7 +12,7 @@ class Product { - /** @var int|string */ + /** @var int */ private $id; /** @var string */ @@ -39,7 +39,7 @@ class Product /** @var int */ private $stockQuantity; - public function getId() + public function getId(): int { return $this->id; } @@ -49,7 +49,7 @@ public function getId() * * @return $this */ - public function setId($id): self + public function setId(int $id): self { $this->id = $id; @@ -226,7 +226,7 @@ public static function fromArray($array): self $calledClass = get_called_class(); - $product = (new $calledClass()) + return (new $calledClass()) ->setId($array['id_product']) ->setName(Tools::replaceAccentedChars($array['name'])) ->setPrice((float) ($array['price_amount'] ?? $array['price'])) @@ -236,7 +236,5 @@ public static function fromArray($array): self ->setVariantId($array['id_product_attribute']) ->setStockQuantity($array['quantity']) ->setMinimalQuantity($array['minimal_quantity']); - - return $product; } } From e0c9a54e2cee785dca111b029e5ae4404dcaeac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Frankiewicz?= Date: Tue, 9 Jul 2024 15:26:30 +0200 Subject: [PATCH 4/5] missing semicolon --- views/templates/hooks/frontend_assets/display_header.tpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/templates/hooks/frontend_assets/display_header.tpl b/views/templates/hooks/frontend_assets/display_header.tpl index a80005a..f56ae86 100644 --- a/views/templates/hooks/frontend_assets/display_header.tpl +++ b/views/templates/hooks/frontend_assets/display_header.tpl @@ -19,7 +19,7 @@ item_category: product.category, item_variant: product.variant, quantity: product.minimal_quantity, - } + }; }, eventBase: () => { return { From a8f53b4e239a267df354df525927d1011165ad70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Frankiewicz?= Date: Tue, 9 Jul 2024 15:27:29 +0200 Subject: [PATCH 5/5] unused import --- src/Model/Product.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Model/Product.php b/src/Model/Product.php index 816e4bf..0261554 100644 --- a/src/Model/Product.php +++ b/src/Model/Product.php @@ -6,9 +6,7 @@ use Category; use Context; use Manufacturer; -use PrestaShop\Module\TagConciergeFree\Hook\Hooks; use Tools; -use Hook; class Product {