/** * Get Product object by code * @param string $code Code * @return Product Product object */ public static function getProductByCode($code, $id_lang) { $powatag_sku = Configuration::get('POWATAG_SKU'); switch ($powatag_sku) { case Powatag::EAN: $id_product = (int) self::getProductIdByEan13($code); break; case Powatag::UPC: $id_product = (int) self::getProductIdByUPC($code); break; case Powatag::REFERENCE: $id_product = (int) self::getProductIdByReference($code); break; default: $id_product = (int) self::getProductIdByIdProduct($code); break; } $product = new Product($id_product, true, (int) $id_lang); //Check if multishop is enabled if (Shop::isFeatureActive() && $product) { //Check that product exists in current shop $id_shops = Product::getShopsByProduct($product->id); $product_exists = false; foreach ($id_shops as $id_shop) { if ($id_shop['id_shop'] == Context::getContext()->shop->id) { $product_exists = true; break; } } if (!$product_exists) { $product = false; } } return $product; }
/** * Add a product attribute * @since 1.5.0.1 * * @param float $price Additional price * @param float $weight Additional weight * @param float $ecotax Additional ecotax * @param integer $id_images Image ids * @param string $reference Reference * @param string $location Location * @param string $ean13 Ean-13 barcode * @param boolean $default Is default attribute for product * @param integer $minimal_quantity Minimal quantity to add to cart * @return mixed $id_product_attribute or false */ public function addAttribute($price, $weight, $unit_impact, $ecotax, $id_images, $reference, $ean13, $default, $location = null, $upc = null, $minimal_quantity = 1, array $id_shop_list = array()) { if (!$this->id) { return; } $price = str_replace(',', '.', $price); $weight = str_replace(',', '.', $weight); $combination = new Combination(); $combination->id_product = (int) $this->id; $combination->price = (double) $price; $combination->ecotax = (double) $ecotax; $combination->quantity = 0; $combination->weight = (double) $weight; $combination->unit_price_impact = (double) $unit_impact; $combination->reference = pSQL($reference); $combination->location = pSQL($location); $combination->ean13 = pSQL($ean13); $combination->upc = pSQL($upc); $combination->default_on = (int) $default; $combination->minimal_quantity = (int) $minimal_quantity; // if we add a combination for this shop and this product does not use the combination feature in other shop, // we clone the default combination in every shop linked to this product if ($default && !$this->hasAttributesInOtherShops()) { $id_shop_list_array = Product::getShopsByProduct($this->id); foreach ($id_shop_list_array as $array_shop) { $id_shop_list[] = $array_shop['id_shop']; } } if (count($id_shop_list)) { $combination->id_shop_list = $id_shop_list; } $combination->add(); if (!$combination->id) { return false; } Product::updateDefaultAttribute($this->id); if (!empty($id_images)) { $combination->setImages($id_images); } return (int) $combination->id; }
/** * @deprecated 1.5.5.0 * @param $attributes * @param bool $set_default * @return array */ public function addProductAttributeMultiple($attributes, $set_default = true) { Tools::displayAsDeprecated(); $return = array(); $default_value = 1; foreach ($attributes as &$attribute) { $obj = new Combination(); foreach ($attribute as $key => $value) { $obj->{$key} = $value; } if ($set_default) { $obj->default_on = $default_value; $default_value = 0; // if we add a combination for this shop and this product does not use the combination feature in other shop, // we clone the default combination in every shop linked to this product if (!$this->hasAttributesInOtherShops()) { $id_shop_list_array = Product::getShopsByProduct($this->id); $id_shop_list = array(); foreach ($id_shop_list_array as $array_shop) { $id_shop_list[] = $array_shop['id_shop']; } $obj->id_shop_list = $id_shop_list; } } $obj->add(); $return[] = $obj->id; } return $return; }