540 lines
18 KiB
PHP
540 lines
18 KiB
PHP
<?php
|
|
|
|
/**
|
|
* NOTICE OF LICENSE
|
|
* With the purchase or the installation of the software in your application
|
|
* you accept the license agreement.
|
|
*
|
|
* You can not resell and redistribute this file.
|
|
*
|
|
* @author Dalibor Stojcevski <dal_sto@yahoo.com>
|
|
* @copyright 2019 Dalibor Stojcevski
|
|
* @license Dalibor Stojcevski
|
|
*/
|
|
|
|
class Convertor
|
|
{
|
|
private $settings;
|
|
private $creator;
|
|
private $id_lang;
|
|
|
|
public function __construct($settings, $creator)
|
|
{
|
|
$this->settings = $settings;
|
|
$this->creator = $creator;
|
|
$this->id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
|
|
}
|
|
|
|
public function convertToFrmProduct($original_product)
|
|
{
|
|
$product_category = array();
|
|
if (!empty($original_product['category_path']))
|
|
{
|
|
|
|
foreach ($original_product['category_path'] as $path)
|
|
{
|
|
|
|
if ($path['parent'])
|
|
{
|
|
$parent_id = $this->creator->getCategoryId($path['parent'], $this->settings['top_category_id']);
|
|
}
|
|
else
|
|
{
|
|
$parent_id = $this->settings['top_category_id'];
|
|
}
|
|
|
|
if ($this->settings['import_api_settings']['category_path'])
|
|
{
|
|
$categories = explode('->', $path['value']);
|
|
}
|
|
else
|
|
{
|
|
$categories = array($path['value']);
|
|
}
|
|
|
|
foreach ($categories as $category)
|
|
{
|
|
if (!$category)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
$parent_id = $category_id = $this->creator->getCategoryId($category, $parent_id);
|
|
}
|
|
|
|
$product_category[] = $category_id;
|
|
}
|
|
}
|
|
|
|
$product_category = array_unique($product_category);
|
|
|
|
if (!$product_category && $this->settings['default_category_id'])
|
|
{
|
|
$product_category[] = $this->settings['default_category_id'];
|
|
}
|
|
|
|
if (!$product_category && $this->settings['top_category_id'])
|
|
{
|
|
$product_category[] = $this->settings['top_category_id'];
|
|
}
|
|
|
|
if (!in_array(Configuration::get('PS_HOME_CATEGORY'), $product_category))
|
|
{
|
|
$product_category[] = Configuration::get('PS_HOME_CATEGORY');
|
|
}
|
|
|
|
if (!$product_category)
|
|
{
|
|
$product_category[] = Configuration::get('PS_HOME_CATEGORY');
|
|
}
|
|
|
|
if (!empty($original_product['brand']))
|
|
{
|
|
$manufacturer_id = $this->creator->getManufacturerId($original_product['brand']);
|
|
}
|
|
else
|
|
{
|
|
$manufacturer_id = $this->settings['default_manufacturer_id'];
|
|
}
|
|
|
|
|
|
|
|
$product = new Product();
|
|
|
|
$original_product['description'] = nl2br($original_product['description']);
|
|
|
|
|
|
|
|
foreach (Language::getLanguages(false) as $lang)
|
|
{
|
|
$original_product['name'] = Tools::cleanNonUnicodeSupport($original_product['name']);
|
|
$product->name[$lang['id_lang']] = htmlspecialchars_decode($original_product['name']);
|
|
|
|
$product->description_long[$lang['id_lang']] = htmlspecialchars_decode(htmlspecialchars_decode($original_product['description'], ENT_COMPAT));
|
|
$product->description[$lang['id_lang']] = htmlspecialchars_decode(htmlspecialchars_decode($original_product['description'], ENT_COMPAT));
|
|
$return_str = $original_product['name'];
|
|
$return_str = Tools::replaceAccentedChars($return_str); //AccentedChars
|
|
$return_str = preg_replace('/[^a-zA-Z0-9\s\'\:\/\[\]\-]/', '', $return_str); // accented chars
|
|
$product->link_rewrite[$lang['id_lang']] = Tools::link_rewrite($return_str);
|
|
$product->meta_keywords[$lang['id_lang']] = str_replace(' ', ',', $original_product['name']);
|
|
|
|
if (!empty($original_product['short_description']))
|
|
{
|
|
$product->description_short[$lang['id_lang']] = $original_product['short_description'];
|
|
}
|
|
}
|
|
|
|
$product->reference = $original_product['reference'];
|
|
$product->id_category = $product_category;
|
|
$product->id_manufacturer = $manufacturer_id;
|
|
$product->id_category_default = !empty($category_id) ? $category_id : Configuration::get('PS_HOME_CATEGORY');
|
|
$product->active = isset($original_product['active']) ? $original_product['active'] : 1;
|
|
$product->width = $original_product['width'];
|
|
$product->height = $original_product['height'];
|
|
$product->depth = $original_product['depth'];
|
|
$product->weight = $original_product['weight'];
|
|
//'visibility' => 'both',
|
|
$product->location = $original_product['location'];
|
|
$product->additional_shipping_cost = $original_product['additional_shipping_cost'];
|
|
//'unit_price' => 0,
|
|
|
|
$product->quantity = $original_product['quantity'];
|
|
$product->minimal_quantity = isset($original_product['minimal_quantity']) ? $original_product['minimal_quantity'] : 1;
|
|
$product->price = $original_product['price'];
|
|
$product->wholesale_price = $original_product['wholesale_price'];
|
|
$product->reference = $original_product['reference'];
|
|
$product->ean13 = $original_product['ean13'];
|
|
$product->upc = $original_product['upc'];
|
|
$product->condition = $original_product['condition'];
|
|
|
|
if ($this->settings['id_tax_rules_group'] != -1)
|
|
{
|
|
$product->id_tax_rules_group = $this->settings['id_tax_rules_group'];
|
|
}
|
|
|
|
$product->additional_delivery_times = 2;
|
|
$product->delivery_in_stock[7] = '1-5 dni roboczych';
|
|
|
|
$product->add();
|
|
|
|
// Pobieranie listy wszystkich sklepów
|
|
$shops = Shop::getShops();
|
|
foreach ($shops as $shop)
|
|
{
|
|
// Skojarzenie produktu ze sklepem
|
|
$product->associateTo($shop['id_shop']);
|
|
}
|
|
|
|
// Aktualizacja danych produktu dla każdego sklepu
|
|
foreach ($shops as $shop)
|
|
{
|
|
Shop::setContext(Shop::CONTEXT_SHOP, $shop['id_shop']);
|
|
|
|
// Przykład aktualizacji stanu magazynowego, statusu i zdjęć
|
|
StockAvailable::setQuantity($product->id, 0, $product->quantity, $shop['id_shop']);
|
|
$product->active = 1; // Ustawienie produktu jako aktywnego
|
|
$product->update();
|
|
|
|
// Tutaj możesz dodać logikę do aktualizacji zdjęć produktu, jeśli to konieczne
|
|
// Może to wymagać dodatkowego kodu zależnego od sposobu zarządzania zdjęciami w Twoim sklepie
|
|
}
|
|
|
|
// Resetowanie kontekstu sklepu (opcjonalnie)
|
|
Shop::setContext(Shop::CONTEXT_ALL);
|
|
|
|
foreach ($original_product['images'] as $image_url)
|
|
{
|
|
$this->creator->addImageToProduct($image_url, $product->id);
|
|
}
|
|
|
|
if (!empty($original_product['cover']))
|
|
{
|
|
$this->creator->addImageToProduct($original_product['cover'], $product->id, true);
|
|
}
|
|
|
|
if ( isset( $original_product['feature'] ) and isset( $original_product['feature_value'] ) )
|
|
{
|
|
$original_product['features'] = array(
|
|
array(
|
|
'feature' => $original_product['feature'],
|
|
'feature_value' => $original_product['feature_value']
|
|
)
|
|
);
|
|
}
|
|
|
|
if (!empty($original_product['features']))
|
|
{
|
|
$this->creator->addFeaturesToProduct($original_product['features'], $product->id);
|
|
}
|
|
|
|
StockAvailable::setQuantity($product->id, 0, $original_product['quantity']);
|
|
|
|
if (!empty($original_product['attributes']))
|
|
{
|
|
$attribute_details = !empty($original_product['attribute_details']) ? $original_product['attribute_details'] : array();
|
|
|
|
$this->creator->addAttributesToProduct($original_product['attributes'], $product->id, $attribute_details);
|
|
}
|
|
|
|
$product->addToCategories($product_category);
|
|
|
|
// set default category
|
|
if ( isset( $this -> settings['default_category_id'] ) )
|
|
{
|
|
$updateSql = "UPDATE " . _DB_PREFIX_ . "product SET id_category_default = " . $this -> settings['default_category_id'] . " WHERE id_product = " . $product->id;
|
|
Db::getInstance()->execute($updateSql);
|
|
$updateSql = "UPDATE " . _DB_PREFIX_ . "product_shop SET id_category_default = " . $this -> settings['default_category_id'] . " WHERE id_product = " . $product->id;
|
|
Db::getInstance()->execute($updateSql);
|
|
}
|
|
|
|
return $product;
|
|
}
|
|
|
|
public function unArray($original_product)
|
|
{
|
|
$frm_simple_fields = array('unique' => 'unique_default', 'reference' => '', 'name' => '', 'description' => '', 'brand' => '', 'price' => 0, 'wholesale_price' => 0, 'minimal_quantity' => 1, 'quantity' => '', 'cover' => '', 'ean13' => '', 'upc' => '', 'condition' => 'new', 'additional_shipping_cost' => 0, 'location' => '', 'width' => 0.00000, 'height' => 0.00000, 'depth' => 0.00000, 'weight' => 0.00000, 'active' => 1);
|
|
|
|
foreach ($frm_simple_fields as $field => $default)
|
|
{
|
|
|
|
if (isset($original_product[$field]))
|
|
{
|
|
while (is_array($original_product[$field]))
|
|
{
|
|
$original_product[$field] = current($original_product[$field]);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$original_product[$field] = $default;
|
|
}
|
|
}
|
|
return $original_product;
|
|
}
|
|
|
|
public function clearInput($original_product)
|
|
{
|
|
|
|
|
|
$original_product['price'] = (float)str_replace(',', '.', $original_product['price']);
|
|
$original_product['wholesale_price'] = (float)str_replace(',', '.', $original_product['wholesale_price']);
|
|
$original_product['additional_shipping_cost'] = (float)str_replace(',', '.', $original_product['additional_shipping_cost']);
|
|
$original_product['quantity'] = (int)str_replace(',', '.', $original_product['quantity']);
|
|
$original_product['minimal_quantity'] = (int)str_replace(',', '', $original_product['minimal_quantity']);
|
|
|
|
$original_product['width'] = (float)str_replace(',', '.', $original_product['width']);
|
|
$original_product['height'] = (float)str_replace(',', '.', $original_product['height']);
|
|
$original_product['depth'] = (float)str_replace(',', '.', $original_product['depth']);
|
|
$original_product['weight'] = (float)str_replace(',', '.', $original_product['weight']);
|
|
|
|
$original_product['price'] = number_format($original_product['price'], 2, '.', '');
|
|
$original_product['wholesale_price'] = number_format($original_product['wholesale_price'], 2, '.', '');
|
|
$original_product['additional_shipping_cost'] = number_format($original_product['additional_shipping_cost'], 2, '.', '');
|
|
|
|
|
|
if (isset($original_product['active']))
|
|
{
|
|
$not_active_statuses = array('disabled', 'not active', 'removed'); // lower case, if you add new status
|
|
if (empty($original_product['active']) || in_array(mb_strtolower($original_product['active']), $not_active_statuses))
|
|
{
|
|
$original_product['active'] = 0;
|
|
}
|
|
else
|
|
{
|
|
$original_product['active'] = 1;
|
|
}
|
|
}
|
|
|
|
if ($this->settings['import_api_settings']['price_multiplier'])
|
|
{
|
|
$original_product['price'] *= $this->settings['import_api_settings']['price_multiplier'];
|
|
}
|
|
|
|
|
|
if (empty($original_product['name']))
|
|
{
|
|
$original_product['name'] = $original_product['unique'];
|
|
}
|
|
|
|
$original_product['name'] = str_replace(['>', '<', '=', ';', '{', '}', '#'], ' ', $original_product['name']);
|
|
|
|
$original_product['description'] = htmlspecialchars($original_product['description'], ENT_COMPAT, 'UTF-8'); // This is for to keep html in database after pSQL
|
|
|
|
if (!empty($original_product['images']))
|
|
{
|
|
if (!is_array($original_product['images']))
|
|
{
|
|
$original_product['images'] = array($original_product['images']);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$original_product['images'] = array();
|
|
}
|
|
|
|
/*$existing_images = array();
|
|
|
|
foreach($original_product['images'] as $img) {
|
|
$headers = @get_headers($img, 1);
|
|
|
|
if (!isset($headers['Content-Type'])) {
|
|
$existing_images[] = $img;
|
|
} elseif (strpos($headers['Content-Type'], 'image/') !== FALSE) {
|
|
// regular image (and not 404).
|
|
$existing_images[] = $img;
|
|
}
|
|
}
|
|
|
|
$original_product['images'] = $existing_images;
|
|
|
|
$headers = @get_headers($original_product['cover'], 1);
|
|
|
|
if (isset($headers['Content-Type']) && strpos($headers['Content-Type'], 'image/') === FALSE) {
|
|
unset($original_product['cover']);
|
|
} */
|
|
|
|
if (empty($original_product['cover']) && !empty($original_product['images']))
|
|
{
|
|
$original_product['cover'] = array_shift($original_product['images']);
|
|
}
|
|
|
|
if (!empty($original_product['ean13']) && !Validate::isEan13($original_product['ean13']))
|
|
{
|
|
$original_product['ean13'] = '';
|
|
}
|
|
|
|
if (!empty($original_product['upc']) && !Validate::isUpc($original_product['upc']))
|
|
{
|
|
$original_product['upc'] = '';
|
|
}
|
|
|
|
if (!empty($original_product['width']) && !Validate::isUnsignedFloat($original_product['width']))
|
|
{
|
|
$original_product['width'] = 0;
|
|
}
|
|
|
|
if (!empty($original_product['height']) && !Validate::isUnsignedFloat($original_product['height']))
|
|
{
|
|
$original_product['height'] = 0;
|
|
}
|
|
|
|
if (!empty($original_product['depth']) && !Validate::isUnsignedFloat($original_product['depth']))
|
|
{
|
|
$original_product['depth'] = 0;
|
|
}
|
|
|
|
if (!empty($original_product['weight']) && !Validate::isUnsignedFloat($original_product['weight']))
|
|
{
|
|
$original_product['weight'] = 0;
|
|
}
|
|
|
|
if (!empty($original_product['price']) && !Validate::isPrice($original_product['price']))
|
|
{
|
|
$original_product['price'] = 0;
|
|
}
|
|
|
|
if (!empty($original_product['wholesale_price']) && !Validate::isPrice($original_product['wholesale_price']))
|
|
{
|
|
$original_product['wholesale_price'] = 0;
|
|
}
|
|
|
|
if (!empty($original_product['additional_shipping_cost']) && !Validate::isPrice($original_product['additional_shipping_cost']))
|
|
{
|
|
$original_product['additional_shipping_cost'] = 0;
|
|
}
|
|
|
|
if (!empty($original_product['location']) && !Validate::isReference($original_product['location']))
|
|
{
|
|
$original_product['location'] = '';
|
|
}
|
|
if (!empty($original_product['short_description']) && !Validate::isCleanHtml($original_product['short_description']))
|
|
{
|
|
$original_product['short_description'] = '';
|
|
}
|
|
if (!empty($original_product['description']) && !Validate::isCleanHtml($original_product['description']))
|
|
{
|
|
$original_product['description'] = '';
|
|
}
|
|
|
|
if (!empty($original_product['condition']) && !in_array($original_product['condition'], ['new', 'used', 'refurbished']))
|
|
{
|
|
$original_product['condition'] = '';
|
|
}
|
|
|
|
return $original_product;
|
|
}
|
|
|
|
public function replace($original_product)
|
|
{
|
|
foreach ($this->settings['import_api_replace'] as $product_key => $replaces)
|
|
{
|
|
foreach ($replaces as $replace)
|
|
{
|
|
if (isset($replace[0]) && isset($replace[1]))
|
|
{
|
|
if (isset($original_product[$product_key]) && is_string($original_product[$product_key]))
|
|
{
|
|
$original_product[$product_key] = str_replace($replace[0], $replace[1], $original_product[$product_key]);
|
|
}
|
|
if ($product_key == 'category' && isset($original_product['category_path']))
|
|
{
|
|
foreach ($original_product['category_path'] as &$category_path)
|
|
{
|
|
if ($category_path['value'] == $replace[0])
|
|
{
|
|
$category_path['value'] = $replace[1];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $original_product;
|
|
}
|
|
public function filter($original_product)
|
|
{
|
|
$original_product['belong'] = true;
|
|
foreach ($this->settings['import_api_filter_options'] as $product_key => $filter_option)
|
|
{
|
|
if ($filter_option === 'not_empty' && (!isset($original_product[$product_key]) || $original_product[$product_key] === '' || $original_product[$product_key] === 'false' || $original_product[$product_key] === 'null'))
|
|
{
|
|
$original_product['belong'] = false;
|
|
return $original_product;
|
|
}
|
|
}
|
|
|
|
$product_categories = array();
|
|
if (isset($original_product['category_path']))
|
|
{
|
|
foreach ($original_product['category_path'] as $category_path)
|
|
{
|
|
$product_categories[] = $category_path['value'];
|
|
}
|
|
}
|
|
foreach ($this->settings['import_api_filter'] as $product_key => $filters)
|
|
{
|
|
if (isset($this->settings['import_api_filter_options'][$product_key]))
|
|
{
|
|
$search_type = $this->settings['import_api_filter_options'][$product_key];
|
|
}
|
|
else
|
|
{
|
|
$search_type = 'equal';
|
|
}
|
|
|
|
foreach ($filters as $filter)
|
|
{
|
|
if ($filter != '')
|
|
{
|
|
if (isset($original_product[$product_key]))
|
|
{
|
|
if ($search_type == 'equal')
|
|
{
|
|
if (!is_array($original_product[$product_key]) && $original_product[$product_key] == $filter)
|
|
{
|
|
$original_product['belong'] = true;
|
|
continue 2; // Search next product[key] condition, this is found.// If you need to fulfil only one product key condition, exit here from function with belong=true
|
|
|
|
}
|
|
if ($product_key == 'category' && in_array($filter, $product_categories))
|
|
{
|
|
$original_product['belong'] = true;
|
|
continue 2;
|
|
}
|
|
}
|
|
elseif ($search_type == 'not_equal')
|
|
{
|
|
if (!is_array($original_product[$product_key]) && $original_product[$product_key] != $filter)
|
|
{
|
|
$original_product['belong'] = true;
|
|
continue 2; // Search next product[key] condition, this is found.// If you need to fulfil only one product key condition, exit here from function with belong=true
|
|
|
|
}
|
|
if ($product_key == 'category' && !in_array($filter, $product_categories))
|
|
{
|
|
$original_product['belong'] = true;
|
|
continue 2;
|
|
}
|
|
}
|
|
elseif ($search_type == 'greater')
|
|
{
|
|
if (!is_array($original_product[$product_key]) && $original_product[$product_key] > $filter)
|
|
{
|
|
$original_product['belong'] = true;
|
|
continue 2;
|
|
}
|
|
}
|
|
elseif ($search_type == 'less')
|
|
{
|
|
if (!is_array($original_product[$product_key]) && $original_product[$product_key] < $filter)
|
|
{
|
|
$original_product['belong'] = true;
|
|
continue 2;
|
|
}
|
|
}
|
|
elseif ($search_type == 'regexp')
|
|
{
|
|
if (!is_array($original_product[$product_key]) && preg_match('/' . $filter . '/i', $original_product[$product_key]) !== 0)
|
|
{
|
|
$original_product['belong'] = true;
|
|
continue 2;
|
|
}
|
|
|
|
if ($product_key == 'category' && preg_grep('/' . $filter . '/i', $product_categories))
|
|
{
|
|
$original_product['belong'] = true;
|
|
continue 2;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$original_product['belong'] = false; // If is here, it is not true. Search is not found
|
|
break;
|
|
}
|
|
|
|
return $original_product;
|
|
}
|
|
}
|