|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright since 2007 PrestaShop SA and Contributors |
| 4 | + * |
| 5 | + * NOTICE OF LICENSE |
| 6 | + * |
| 7 | + * This source file is subject to the Academic Free License 3.0 (AFL-3.0) |
| 8 | + * that is bundled with this package in the file LICENSE.md. |
| 9 | + * It is also available through the world-wide-web at this URL: |
| 10 | + * https://opensource.org/licenses/AFL-3.0 |
| 11 | + * If you did not receive a copy of the license and are unable to |
| 12 | + * obtain it through the world-wide-web, please send an email |
| 13 | + * to license@prestashop.com so we can send you a copy immediately. |
| 14 | + * |
| 15 | + * @author PrestaShop SA <contact@prestashop.com> |
| 16 | + * @copyright Since 2007 PrestaShop SA and Contributors |
| 17 | + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) |
| 18 | + * International Registered Trademark & Property of PrestaShop SA |
| 19 | + */ |
| 20 | + |
| 21 | +namespace PrestaShop\Module\Ps_Googleanalytics\Hooks; |
| 22 | + |
| 23 | +use Context; |
| 24 | +use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsDataHandler; |
| 25 | +use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper; |
| 26 | +use Product; |
| 27 | +use Ps_Googleanalytics; |
| 28 | + |
| 29 | +class HookActionCartUpdateQuantityBefore implements HookInterface |
| 30 | +{ |
| 31 | + private $module; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var Context |
| 35 | + */ |
| 36 | + private $context; |
| 37 | + private $params; |
| 38 | + |
| 39 | + public function __construct(Ps_Googleanalytics $module, Context $context) |
| 40 | + { |
| 41 | + $this->module = $module; |
| 42 | + $this->context = $context; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * run |
| 47 | + * |
| 48 | + * @return void |
| 49 | + */ |
| 50 | + public function run() |
| 51 | + { |
| 52 | + // Get our tag handler |
| 53 | + $ganalyticsDataHandler = new GanalyticsDataHandler( |
| 54 | + $this->context->cart->id, |
| 55 | + $this->context->shop->id |
| 56 | + ); |
| 57 | + |
| 58 | + /* |
| 59 | + * The hook passes a legacy Product object to add, but no attribute information. |
| 60 | + * But thankfully, we can use id_product_attribute for this. |
| 61 | + * |
| 62 | + * Other info is fairly standard: |
| 63 | + * |
| 64 | + * Add to cart from product page + up from cart page |
| 65 | + * $this->params['operator'] == up |
| 66 | + * $this->params['quantity'] to determine quantity |
| 67 | + * |
| 68 | + * Down from cart page |
| 69 | + * $this->params['operator'] == down |
| 70 | + * $this->params['quantity'] to determine quantity |
| 71 | + */ |
| 72 | + |
| 73 | + // Format product and standardize ID |
| 74 | + $product = (array) $this->params['product']; |
| 75 | + $product['id_product'] = $product['id']; |
| 76 | + |
| 77 | + // Get some basic information |
| 78 | + $product = Product::getProductProperties($this->context->language->id, $product); |
| 79 | + |
| 80 | + // Add information about attribute |
| 81 | + if (!empty($this->params['id_product_attribute'])) { |
| 82 | + $product['id_product_attribute'] = (int) $this->params['id_product_attribute']; |
| 83 | + } |
| 84 | + $product['price_amount'] = $product['price']; |
| 85 | + |
| 86 | + // Add informationa about quantity difference |
| 87 | + $product['quantity'] = (int) $this->params['quantity']; |
| 88 | + |
| 89 | + // Prepare it and format it for our purpose |
| 90 | + $productWrapper = new ProductWrapper($this->context); |
| 91 | + $item = $productWrapper->prepareItemFromProduct($product, true); |
| 92 | + |
| 93 | + // Prepare and render event |
| 94 | + $eventData = [ |
| 95 | + 'currency' => $this->context->currency->iso_code, |
| 96 | + 'value' => $product['price_amount'] * $product['quantity'], |
| 97 | + 'items' => [$item], |
| 98 | + ]; |
| 99 | + $jsCode = $this->module->getTools()->renderEvent( |
| 100 | + $this->params['operator'] == 'up' ? 'add_to_cart' : 'remove_from_cart', |
| 101 | + $eventData |
| 102 | + ); |
| 103 | + |
| 104 | + // Store this event |
| 105 | + $ganalyticsDataHandler->persistData($jsCode); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param array $params |
| 110 | + */ |
| 111 | + public function setParams($params) |
| 112 | + { |
| 113 | + $this->params = $params; |
| 114 | + } |
| 115 | +} |
0 commit comments