|
| 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\Wrapper\ProductWrapper; |
| 25 | +use Product; |
| 26 | +use Ps_Googleanalytics; |
| 27 | +use Validate; |
| 28 | + |
| 29 | +class HookActionObjectProductInCartDeleteBefore 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 | + // Format product and standardize ID |
| 53 | + $product = new Product((int) $this->params['id_product'], false, (int) $this->context->language->id); |
| 54 | + if (!Validate::isLoadedObject($product)) { |
| 55 | + return; |
| 56 | + } |
| 57 | + $product = (array) $product; |
| 58 | + $product['id_product'] = $product['id']; |
| 59 | + |
| 60 | + // Get some basic information |
| 61 | + $product = Product::getProductProperties($this->context->language->id, $product); |
| 62 | + |
| 63 | + // Add information about attribute |
| 64 | + if (!empty($this->params['id_product_attribute'])) { |
| 65 | + $product['id_product_attribute'] = (int) $this->params['id_product_attribute']; |
| 66 | + } |
| 67 | + |
| 68 | + // Prepare it and format it for our purpose |
| 69 | + $productWrapper = new ProductWrapper($this->context); |
| 70 | + $item = $productWrapper->prepareItemFromProduct($product, false); |
| 71 | + |
| 72 | + // Prepare and render event |
| 73 | + $eventData = [ |
| 74 | + 'currency' => $this->context->currency->iso_code, |
| 75 | + 'value' => $item['price'] * $item['quantity'], |
| 76 | + 'items' => [$item], |
| 77 | + ]; |
| 78 | + $jsCode = $this->module->getTools()->renderEvent( |
| 79 | + 'remove_from_cart', |
| 80 | + $eventData |
| 81 | + ); |
| 82 | + |
| 83 | + // Store this event |
| 84 | + $this->module->getDataHandler()->persistData($jsCode); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param array $params |
| 89 | + */ |
| 90 | + public function setParams($params) |
| 91 | + { |
| 92 | + $this->params = $params; |
| 93 | + } |
| 94 | +} |
0 commit comments