/**
  * This will update a Pack quantity and will decrease the quantity of containing Products if needed.
  *
  * @param Product $product A product pack object to update its quantity
  * @param StockAvailable $stock_available the stock of the product to fix with correct quantity
  * @param integer $delta_quantity The movement of the stock (negative for a decrease)
  * @param integer|null $id_shop Opional shop ID
  */
 public function updatePackQuantity($product, $stock_available, $delta_quantity, $id_shop = null)
 {
     $configuration = Adapter_ServiceLocator::get('Core_Business_ConfigurationInterface');
     if ($product->pack_stock_type == 1 || $product->pack_stock_type == 2 || $product->pack_stock_type == 3 && $configuration->get('PS_PACK_STOCK_TYPE') > 0) {
         $packItemsManager = Adapter_ServiceLocator::get('Adapter_PackItemsManager');
         $products_pack = $packItemsManager->getPackItems($product);
         $stockAvailable = new Core_Business_Stock_StockManager();
         $stockManager = Adapter_ServiceLocator::get('Adapter_StockManager');
         $cacheManager = Adapter_ServiceLocator::get('Adapter_CacheManager');
         foreach ($products_pack as $product_pack) {
             $productStockAvailable = $stockManager->getStockAvailableByProduct($product_pack, $product_pack->id_pack_product_attribute, $id_shop);
             $productStockAvailable->quantity = $productStockAvailable->quantity + $delta_quantity * $product_pack->pack_quantity;
             $productStockAvailable->update();
             $cacheManager->clean('StockAvailable::getQuantityAvailableByProduct_' . (int) $product_pack->id . '*');
         }
     }
     $stock_available->quantity = $stock_available->quantity + $delta_quantity;
     if ($product->pack_stock_type == 0 || $product->pack_stock_type == 2 || $product->pack_stock_type == 3 && ($configuration->get('PS_PACK_STOCK_TYPE') == 0 || $configuration->get('PS_PACK_STOCK_TYPE') == 2)) {
         $stock_available->update();
     }
 }
Exemplo 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));
     }
 }
    /**
     * 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.');
            }
        }
    }