/**
     * 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)
    {
        $query = '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`="' . pSQL($ts_id) . '"';
        Db::getInstance()->Execute($query);
        foreach ($protection_items as $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 = Tools::convertPrice($item->grossFee, Currency::getIdByIsoCode($item->currency));
            $product->id_category_default = TSBuyerProtection::$CAT_ID;
            $product->active = true;
            $product->visibility = 'none';
            $product->id_tax = 0;
            $product->add();
            if ($product->id) {
                $query = '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($query);
                if (class_exists('StockAvailable')) {
                    $id_stock_available = Db::getInstance()->getValue('
						SELECT s.`id_stock_available` FROM `' . _DB_PREFIX_ . 'stock_available` s
						WHERE s.`id_product` = ' . (int) $product->id);
                    $stock = new StockAvailable($id_stock_available);
                    $stock->id_product = $product->id;
                    $stock->out_of_stock = 1;
                    $stock->id_product_attribute = 0;
                    $stock->quantity = 1000000;
                    $stock->id_shop = Context::getContext()->shop->id;
                    if ($stock->id) {
                        $stock->update();
                    } else {
                        $stock->add();
                    }
                }
            } else {
                $this->errors['products'] = $this->l('Product wasn\'t saved.');
            }
        }
    }
Ejemplo n.º 2
0
 /**
  * For a given id_product and id_product_attribute sets the quantity available
  *
  * @param int $id_product
  * @param int $id_product_attribute Optional
  * @param int $delta_quantity The delta quantity to update
  * @param int $id_shop Optional
  */
 public static function setQuantity($id_product, $id_product_attribute, $quantity, $id_shop = null)
 {
     if (!Validate::isUnsignedId($id_product)) {
         return false;
     }
     $context = Context::getContext();
     // if there is no $id_shop, gets the context one
     if ($id_shop === null && Shop::getContext() != Shop::CONTEXT_GROUP) {
         $id_shop = (int) $context->shop->id;
     }
     $depends_on_stock = StockAvailable::dependsOnStock($id_product);
     //Try to set available quantity if product does not depend on physical stock
     if (!$depends_on_stock) {
         $id_stock_available = (int) StockAvailable::getStockAvailableIdByProductId($id_product, $id_product_attribute, $id_shop);
         if ($id_stock_available) {
             $stock_available = new StockAvailable($id_stock_available);
             $stock_available->quantity = (int) $quantity;
             $stock_available->update();
         } else {
             $out_of_stock = StockAvailable::outOfStock($id_product, $id_shop);
             $stock_available = new StockAvailable();
             $stock_available->out_of_stock = (int) $out_of_stock;
             $stock_available->id_product = (int) $id_product;
             $stock_available->id_product_attribute = (int) $id_product_attribute;
             $stock_available->quantity = (int) $quantity;
             $shop_group = new ShopGroup((int) Shop::getContextShopGroupID());
             // if quantities are shared between shops of the group
             if ($shop_group->share_stock) {
                 $stock_available->id_shop = 0;
                 $stock_available->id_shop_group = (int) $shop_group->id;
             } else {
                 $stock_available->id_shop = $id_shop;
                 $stock_available->id_shop_group = Shop::getGroupFromShop($id_shop);
             }
             $stock_available->add();
         }
         Hook::exec('actionUpdateQuantity', array('id_product' => $id_product, 'id_product_attribute' => $id_product_attribute, 'quantity' => $stock_available->quantity));
     }
 }
 protected function importStockAvailable()
 {
     $this->truncateTables(array('stock', 'stock_available', 'stock_mvt'));
     $handle = $this->openCsvFile('stock_available.csv');
     for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, ';'); $current_line++) {
         $res = false;
         $fields = $this->filterFields('StockAvailable', $this->stock_available_fields, $line);
         if (!isset($fields['id'])) {
             $sa = new StockAvailable($line[0]);
             $sa->id = $line[0];
         } else {
             $sa = new StockAvailable($fields['id']);
         }
         foreach ($fields as $key => $field) {
             $sa->{$key} = $field;
         }
         $sa->force_id = true;
         if (!$res) {
             $res = $sa->add();
         }
     }
     $this->closeCsvFile($handle);
     return true;
 }