/** * Save in special database each buyer protection product for a certificate, * Each Trusted Shops particular characteristics is saved. * Create a product in Prestashop database to allow added each of them in cart. * * @param array|stdClass $protection_items * @param string $ts_id */ private function _saveProtectionItems($protection_items, $ts_id) { $sql = ' DELETE ts, p, pl FROM `' . _DB_PREFIX_ . TSBuyerProtection::DB_ITEMS . '` AS ts LEFT JOIN `' . _DB_PREFIX_ . 'product` AS p ON ts.`id_product` = p.`id_product` LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` AS pl ON ts.`id_product` = pl.`id_product` WHERE ts.`ts_id`="' . $ts_id . '"'; Db::getInstance()->Execute($sql); foreach ($protection_items as $key => $item) { //add hidden product $product = new Product(); foreach ($this->available_languages as $iso => $lang) { $language = Language::getIdByIso(strtolower($iso)); if ((int) $language !== 0) { $product->name[$language] = 'TrustedShops guarantee'; $product->link_rewrite[$language] = 'trustedshops_guarantee'; } } // If the default lang is different than available languages : // (Bug occurred otherwise) if (!array_key_exists(Language::getIsoById((int) Configuration::get('PS_LANG_DEFAULT')), $this->available_languages)) { $product->name[(int) Configuration::get('PS_LANG_DEFAULT')] = 'Trustedshops'; $product->link_rewrite[(int) Configuration::get('PS_LANG_DEFAULT')] = 'trustedshops'; } // Add specifics translations $id_lang = Language::getIdByIso('de'); if ((int) $id_lang > 0) { $product->name[$id_lang] = 'Trusted Shops Käuferschutz'; } $id_lang = Language::getIdByIso('en'); if ((int) $id_lang > 0) { $product->name[$id_lang] = 'Trusted Shops buyer protection'; } $id_lang = Language::getIdByIso('fr'); if ((int) $id_lang > 0) { $product->name[$id_lang] = 'Trusted Shops protection acheteur'; } $product->quantity = 1000; $product->price = ToolsCore::convertPrice($item->grossFee, Currency::getIdByIsoCode($item->currency)); $product->id_category_default = TSBuyerProtection::$CAT_ID; $product->active = true; $product->id_tax = 0; $product->add(); if ($product->id) { $sql = ' INSERT INTO `' . _DB_PREFIX_ . TSBuyerProtection::DB_ITEMS . '` ( `creation_date`, `id_product`, `ts_id`, `id`, `currency`, `gross_fee`, `net_fee`, `protected_amount_decimal`, `protection_duration_int`, `ts_product_id` ) VALUES ( "' . pSQL($item->creationDate) . '", "' . pSQL($product->id) . '", "' . pSQL($ts_id) . '", "' . (int) $item->id . '", "' . pSQL($item->currency) . '", "' . pSQL($item->grossFee) . '", "' . pSQL($item->netFee) . '", "' . pSQL($item->protectedAmountDecimal) . '", "' . pSQL($item->protectionDurationInt) . '", "' . pSQL($item->tsProductID) . '" )'; Db::getInstance()->Execute($sql); } else { $this->errors['products'] = $this->l('Product wasn\'t saved.'); } } }