Files
wyczarujprezent.pl/modules/ecsgtmpro/classes/gtm/EcsGtmProProduct.php
2024-10-28 22:14:22 +01:00

162 lines
5.7 KiB
PHP

<?php
/**
* 2022 ECSoft
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to dev.ecsoft@gmail.com so we can send you a copy immediately.
*
* @author ECSoft <dev.ecsoft@gmail.com>
* @copyright 2022 ECSoft
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of ECSoft
*/
/**
* @property EcsGtmPro $module
*/
class EcsGtmProProduct
{
private static $instance = null;
protected $module;
private function __construct()
{
$this->module = Module::getInstanceByName('ecsgtmpro');
}
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function getProductId($product, $combination)
{
return $this->getProductIdentifier($product, $combination, $this->module->gtmConfig->dataConfig->product_id);
}
public function remarketingGetProductIdentifier($product)
{
$id_product_attribute = EcsGtmProTools::getProductAttributeId($product, $this->module->gtmConfig->dataConfig->display_variant_id);
$combination = null;
if ($id_product_attribute) {
$combination = new Combination($id_product_attribute);
}
return $this->getProductIdentifier($product, $combination, $this->module->gtmConfig->remarketingConfig->product_id, $this->module->gtmConfig->remarketingConfig->product_prefix);
}
public function getProductIdentifier($product, $combination = null, $id_type = null, $product_id_prefix = null)
{
$product = is_object($product) ? $product : (object)$product;
$product_identifier = null;
if (empty($id_type) || !in_array($id_type, array('id', 'reference', 'ean13', 'upc'))) {
$id_type = 'id';
}
if (!is_null($combination) && Validate::isLoadedObject($combination)) {
$id_product_attribute = $combination->id;
} else {
$combination = null;
$id_product_attribute = null;
}
if (empty($product->id) && !empty($product->id_product)) {
$product->id = $product->id_product;
}
switch ($id_type) {
case 'id':
$product_id = !empty($product->id) ? $product->id : 0;
$product_identifier = null;
if ($this->module->gtmConfig->dataConfig->display_variant_id != 'never') {
if (!$id_product_attribute && !empty($product->id_product_attribute)) {
$id_product_attribute = $product->id_product_attribute;
}
if ($id_product_attribute) {
$product_identifier = $product_id.'-'.$id_product_attribute;
} elseif ($this->module->gtmConfig->dataConfig->display_variant_id == 'always') {
$product_identifier = $product_id.'-'.Product::getDefaultAttribute($product_id);
}
}
$product_identifier = $product_identifier ? $product_identifier : ''.$product_id;
break;
case 'reference':
case 'ean13':
case 'upc':
if ($combination && !empty($combination->$id_type)) {
$product_identifier = $combination->$id_type;
} elseif (!empty($product->$id_type)) {
$product_identifier = $product->$id_type;
}
break;
default:
$product_identifier = 'ERROR';
break;
}
if (!empty($product_id_prefix)) {
$product_id_prefix = (string) $product_id_prefix;
$product_id_prefix = str_replace('{lang}', Context::getContext()->language->iso_code, $product_id_prefix);
$product_id_prefix = str_replace('{LANG}', Tools::strtoupper(Context::getContext()->language->iso_code), $product_id_prefix);
$product_identifier = $product_id_prefix.$product_identifier;
}
return $product_identifier;
}
public function getProductName($product, $product_name_field = null)
{
$base_product = new Product($product->id);
$product_name_field = empty($product_name_field) ? $this->module->gtmConfig->dataConfig->product_name : $product_name_field;
$id_lang = $this->module->dlManager->getDataLanguage();
switch ($product_name_field) {
case 'link_rewrite':
if (!empty($base_product->link_rewrite)) {
if (is_array($base_product->link_rewrite)) {
$product_name = !empty($base_product->link_rewrite[$id_lang]) ? $base_product->link_rewrite[$id_lang] : $base_product->link_rewrite[0];
} else {
$product_name = $base_product->link_rewrite;
}
}
break;
case 'id':
$product_name = $base_product->id;
break;
default:
$product_name = EcsGtmProTools::cleanString(Product::getProductName($base_product->id, 0, $id_lang));
break;
}
$product_name = trim($product_name);
if (empty($product_name)) {
$product_name = 'Unknown product name';
}
return $product_name;
}
}