64, 'supplier_reference' => 32, 'location' => 64, 'ean13' => 13, 'upc' => 12, 'unity' => 10); protected $fieldsValidateLang = array( 'meta_description' => 'isGenericName', 'meta_keywords' => 'isGenericName', 'meta_title' => 'isGenericName', 'link_rewrite' => 'isLinkRewrite', 'name' => 'isCatalogName', 'description' => 'isString', 'description_short' => 'isString', 'available_now' => 'isGenericName', 'available_later' => 'IsGenericName', 'description_more' => 'isString', 'description_comment' => 'isString', 'videos' => 'isString', 'description_delivery' => 'isString'); /** * @Override * Check then return multilingual fields for database interaction * * @return array Multilingual fields */ public function getTranslationsFieldsChild() { self::validateFieldsLang(); $fieldsArray = array('meta_description', 'meta_keywords', 'meta_title', 'link_rewrite', 'name', 'available_now', 'available_later'); $fields = array(); $languages = Language::getLanguages(false); $defaultLanguage = Configuration::get('PS_LANG_DEFAULT'); foreach ($languages as $language) { $fields[$language['id_lang']]['id_lang'] = $language['id_lang']; $fields[$language['id_lang']][$this->identifier] = (int)($this->id); $fields[$language['id_lang']]['description'] = (isset($this->description[$language['id_lang']])) ? pSQL($this->description[$language['id_lang']], true) : ''; $fields[$language['id_lang']]['description_short'] = (isset($this->description_short[$language['id_lang']])) ? pSQL($this->description_short[$language['id_lang']], true) : ''; $fields[$language['id_lang']]['description_more'] = (isset($this->description_more[$language['id_lang']])) ? pSQL($this->description_more[$language['id_lang']], true) : ''; $fields[$language['id_lang']]['description_comment'] = (isset($this->description_comment[$language['id_lang']])) ? pSQL($this->description_comment[$language['id_lang']], true) : ''; $fields[$language['id_lang']]['videos'] = (isset($this->videos[$language['id_lang']])) ? pSQL($this->videos[$language['id_lang']], true) : ''; $fields[$language['id_lang']]['description_delivery'] = (isset($this->description_delivery[$language['id_lang']])) ? pSQL($this->description_delivery[$language['id_lang']], true) : ''; foreach ($fieldsArray as $field) { if (!Validate::isTableOrIdentifier($field)) die(Tools::displayError()); /* Check fields validity */ if (isset($this->{$field}[$language['id_lang']]) AND !empty($this->{$field}[$language['id_lang']])) $fields[$language['id_lang']][$field] = pSQL($this->{$field}[$language['id_lang']]); elseif (in_array($field, $this->fieldsRequiredLang)) { if ($this->{$field} != '') $fields[$language['id_lang']][$field] = pSQL($this->{$field}[$defaultLanguage]); } else $fields[$language['id_lang']][$field] = ''; } } return $fields; } public function getCustomFields() { $customs_data = Db::getInstance()->getRow(' SELECT `nc8`, `id_country` FROM `'._DB_PREFIX_.'product_customs` WHERE id_product = '.(int)$this->id ); return $customs_data; } /** * @Override * Admin panel product search * * @param integer $id_lang Language id * @param string $query Search query * @return array Matching products */ public static function searchByName($id_lang, $query, $orderBy = 'name', $order = 'ASC') { $result = Db::getInstance()->ExecuteS(' SELECT p.`id_product`, pl.`name`, p.`active`, p.`reference`, m.`name` AS manufacturer_name, MIN(cp.`id_category`) AS `main_category_id` -- (SELECT name FROM ps_category_lang WHERE `id_category` = MIN(cp.`id_category`) AND `id_lang` = 1) AS `main_category` FROM `'._DB_PREFIX_.'category_product` cp LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = cp.`id_product` LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)($id_lang).') LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer` LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON pa.`id_product` = p.`id_product` WHERE pl.`name` LIKE \'%'.pSQL($query).'%\' OR p.`reference` LIKE \'%'.pSQL($query).'%\' OR p.`supplier_reference` LIKE \'%'.pSQL($query).'%\' OR pa.`reference` LIKE \'%'.pSQL($query).'%\' GROUP BY `id_product` ORDER BY pl.`'.$orderBy.'` '.$order); if (!$result) return false; $resultsArray = array(); foreach ($result AS $row) { $row['price'] = Product::getPriceStatic($row['id_product'], true, NULL, 2); $row['quantity'] = Product::getQuantity($row['id_product']); $resultsArray[] = $row; } return $resultsArray; } public static function getProductsProperties($id_lang, $query_result, $cached = true) { $resultsArray = array(); if (is_array($query_result)) foreach ($query_result AS $row) if ($row2 = Product::getProductProperties($id_lang, $row, $cached)) $resultsArray[] = $row2; return $resultsArray; } public static function getProductProperties($id_lang, $row, $cached = true) { if (!$row['id_product']) return false; // Product::getDefaultAttribute is only called if id_product_attribute is missing from the SQL query at the origin of it: consider adding it in order to avoid unnecessary queries $row['allow_oosp'] = Product::isAvailableWhenOutOfStock($row['out_of_stock']); if ((!isset($row['id_product_attribute']) OR !$row['id_product_attribute']) AND ((isset($row['cache_default_attribute']) AND ($ipa_default = $row['cache_default_attribute']) !== NULL) OR ($ipa_default = Product::getDefaultAttribute($row['id_product'], !$row['allow_oosp']))) ) $row['id_product_attribute'] = $ipa_default; if (!isset($row['id_product_attribute'])) $row['id_product_attribute'] = 0; // Tax $usetax = Tax::excludeTaxeOption(); if ($cached) { $cacheKey = $row['id_product'].'-'.$row['id_product_attribute'].'-'.$id_lang.'-'.(int)($usetax); if (array_key_exists($cacheKey, self::$producPropertiesCache)) return self::$producPropertiesCache[$cacheKey]; } // Datas $link = new Link(); $row['category'] = Category::getLinkRewrite((int)$row['id_category_default'], (int)($id_lang)); $row['link'] = $link->getProductLink((int)$row['id_product'], $row['link_rewrite'], $row['category'], $row['ean13']); $row['attribute_price'] = (isset($row['id_product_attribute']) AND $row['id_product_attribute']) ? (float)(Product::getProductAttributePrice($row['id_product_attribute'])) : 0; $row['price_tax_exc'] = Product::getPriceStatic((int)$row['id_product'], false, ((isset($row['id_product_attribute']) AND !empty($row['id_product_attribute'])) ? (int)($row['id_product_attribute']) : NULL), (self::$_taxCalculationMethod == PS_TAX_EXC ? 2 : 6)); if (self::$_taxCalculationMethod == PS_TAX_EXC) { $row['price_tax_exc'] = Tools::ps_round($row['price_tax_exc'], 2); $row['price'] = Product::getPriceStatic((int)$row['id_product'], true, ((isset($row['id_product_attribute']) AND !empty($row['id_product_attribute'])) ? (int)($row['id_product_attribute']) : NULL), 6); $row['price_without_reduction'] = Product::getPriceStatic((int)$row['id_product'], false, ((isset($row['id_product_attribute']) AND !empty($row['id_product_attribute'])) ? (int)($row['id_product_attribute']) : NULL), 2, NULL, false, false); } else { $row['price'] = Tools::ps_round(Product::getPriceStatic((int)$row['id_product'], true, ((isset($row['id_product_attribute']) AND !empty($row['id_product_attribute'])) ? (int)($row['id_product_attribute']) : NULL), 2), 2); $row['price_without_reduction'] = Product::getPriceStatic((int)$row['id_product'], true, ((isset($row['id_product_attribute']) AND !empty($row['id_product_attribute'])) ? (int)($row['id_product_attribute']) : NULL), 6, NULL, false, false); } $row['reduction'] = Product::getPriceStatic((int)($row['id_product']), (bool)$usetax, (int)($row['id_product_attribute']), 6, NULL, true, true, 1, true, NULL, NULL, NULL, $specific_prices); $row['specific_prices'] = $specific_prices; if ($row['id_product_attribute']) { $row['quantity_all_versions'] = Product::getTotalAttributeQuantity((int)$row['id_product']); $row['quantity'] = Product::getQuantity((int)$row['id_product'], $row['id_product_attribute'], isset($row['cache_is_pack']) ? $row['cache_is_pack'] : NULL); } $row['id_image'] = Product::defineProductImage($row, $id_lang); $row['features'] = Product::getFrontFeaturesStatic((int)$id_lang, $row['id_product']); $row['attachments'] = ((!isset($row['cache_has_attachments']) OR $row['cache_has_attachments']) ? Product::getAttachmentsStatic((int)($id_lang), $row['id_product']) : array()); // Pack management $row['pack'] = (!isset($row['cache_is_pack']) ? Pack::isPack($row['id_product']) : (int)$row['cache_is_pack']); $row['packItems'] = $row['pack'] ? Pack::getItemTable($row['id_product'], $id_lang) : array(); $row['nopackprice'] = $row['pack'] ? Pack::noPackPrice($row['id_product']) : 0; if ($row['pack'] AND !Pack::isInStock($row['id_product'])) $row['quantity'] = 0; // is random product $is_random = Db::getInstance()->getValue(' SELECT `random` FROM `'._DB_PREFIX_.'product_customs` WHERE id_product = '.(int)$row['id_product'] ); $row['is_random'] = (int)$is_random; if ($cached) { self::$producPropertiesCache[$cacheKey] = $row; return self::$producPropertiesCache[$cacheKey]; } else { return $row; } } public function toggleCoupCoeur(){ if($this->online_only == 1){ $this->online_only = 0; $this->save(); }else{ $this->online_only = 1; $this->save(); } } public static function getDetailsCombination($id_product_attribute, $id_lang) { return Db::getInstance()->ExecuteS(' SELECT pa.*, ag.`id_attribute_group`, ag.`is_color_group`, agl.`name` AS group_name, al.`name` AS attribute_name, a.`id_attribute`, pa.`unit_price_impact` FROM `'._DB_PREFIX_.'product_attribute` pa LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON pac.`id_product_attribute` = pa.`id_product_attribute` LEFT JOIN `'._DB_PREFIX_.'attribute` a ON a.`id_attribute` = pac.`id_attribute` LEFT JOIN `'._DB_PREFIX_.'attribute_group` ag ON ag.`id_attribute_group` = a.`id_attribute_group` LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = '.(int)($id_lang).') LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` agl ON (ag.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = '.(int)($id_lang).') WHERE pa.`id_product_attribute` = '.(int)($id_product_attribute).' ORDER BY pa.`id_product_attribute`'); } public static function duplicateOneAttribute($id_product_old, $id_product_new, $old_attribute_id, $quantity = 0) { $return = true; $combinationImages = array(); $result = Db::getInstance()->ExecuteS(' SELECT * FROM `'._DB_PREFIX_.'product_attribute` WHERE `id_product` = '.(int)($id_product_old).' AND `id_product_attribute` = '.(int)($old_attribute_id)); foreach ($result as $row) { $id_product_attribute_old = (int)($row['id_product_attribute']); $result2 = Db::getInstance()->ExecuteS(' SELECT * FROM `'._DB_PREFIX_.'product_attribute_combination` WHERE `id_product_attribute` = '.$id_product_attribute_old); if ($quantity>0) { $row['quantity'] = $quantity; } $row['id_product'] = $id_product_new; unset($row['id_product_attribute']); $return &= Db::getInstance()->AutoExecute(_DB_PREFIX_.'product_attribute', $row, 'INSERT'); $id_product_attribute_new = (int)(Db::getInstance()->Insert_ID()); if ($resultImages = self::_getAttributeImageAssociations($id_product_attribute_old)) { $combinationImages['old'][$id_product_attribute_old] = $resultImages; $combinationImages['new'][$id_product_attribute_new] = $resultImages; } foreach ($result2 AS $row2) { $row2['id_product_attribute'] = $id_product_attribute_new; $return &= Db::getInstance()->AutoExecute(_DB_PREFIX_.'product_attribute_combination', $row2, 'INSERT'); } } return !$return ? false : $combinationImages; } /* * @Override * Ajout d'une quantité choisie */ public static function duplicateAttributes($id_product_old, $id_product_new, $quantity = 0) { $return = true; $combinationImages = array(); $result = Db::getInstance()->ExecuteS(' SELECT * FROM `'._DB_PREFIX_.'product_attribute` WHERE `id_product` = '.(int)($id_product_old)); foreach ($result as $row) { $id_product_attribute_old = (int)($row['id_product_attribute']); $result2 = Db::getInstance()->ExecuteS(' SELECT * FROM `'._DB_PREFIX_.'product_attribute_combination` WHERE `id_product_attribute` = '.$id_product_attribute_old); if ($quantity>0) { $row['quantity'] = $quantity; } $row['id_product'] = $id_product_new; unset($row['id_product_attribute']); $return &= Db::getInstance()->AutoExecute(_DB_PREFIX_.'product_attribute', $row, 'INSERT'); $id_product_attribute_new = (int)(Db::getInstance()->Insert_ID()); if ($resultImages = self::_getAttributeImageAssociations($id_product_attribute_old)) { $combinationImages['old'][$id_product_attribute_old] = $resultImages; $combinationImages['new'][$id_product_attribute_new] = $resultImages; } foreach ($result2 AS $row2) { $row2['id_product_attribute'] = $id_product_attribute_new; $return &= Db::getInstance()->AutoExecute(_DB_PREFIX_.'product_attribute_combination', $row2, 'INSERT'); } } return !$return ? false : $combinationImages; } public static function getTotalAttributeQuantity($id_product) { $quantity = Db::getInstance()->getValue(' SELECT SUM(`quantity`) FROM `'._DB_PREFIX_.'product_attribute` WHERE `id_product` = '.(int)($id_product) ); return $quantity; } /** * Returns quantity sold during the last 5 sales for a single product * ordered by best to last * @param array $params array which contains an array with either ean or ref as key ['ean':'012345678901245'] or ['ref':'my-ref'] * @param array $excluded_ids array which contains product exclusions * @param bool $use_cache set if the function uses the cache * @return array containing information about the best sales * output structure : * [ { "quantity_sold": "12", "id_sale": "662", "product_id": "28624", "name": "Gigoteuse 70 cm - Jungle Pistache" }, { [...] } ] */ public static function getBestSales($params, $excluded_ids, $use_cache = true) { $returnedArray = array(); $ean = false; $ref = false; if (isset($params['ean'])) { $ean = $params['ean']; } if (isset($params['ref'])) { $ref = $params['ref']; } if (empty($excluded_ids)) { $excluded_ids = getBraderieProducts(); } $order_states = explode(',', Configuration::get('PS_IT_OF_ORDER_STATES')); if ($ref) { //get related products $sql = 'SELECT p.id_product, p.reference FROM `' . _DB_PREFIX_ . 'product` p WHERE p.`supplier_reference` = \'' . pSQL($ref) . '\' ORDER BY id_product'; testonecho('sql ' . $sql); } if ($ean) { //get related products $sql = 'SELECT id_product, reference FROM `' . _DB_PREFIX_ . 'product` WHERE ean13 = \'' . pSQL($ean) . '\' ORDER BY id_product'; } $result = Db::getInstance()->ExecuteS($sql, true, $use_cache, __METHOD__); $product_id_array = array(); if ($result) { foreach ($result as $row) { if (!in_array((int)$row['id_product'], $excluded_ids, true)) { $product_id_array[] = (int)$row['id_product']; } } //get related sales $sql = 'SELECT SUM(d.`product_quantity` - d.`product_quantity_reinjected`) as quantity_sold, ppc.`id_sale`, d.`product_id`, pl.name FROM `' . _DB_PREFIX_ . 'order_detail` d LEFT JOIN `' . _DB_PREFIX_ . 'orders` o ON o.`id_order` = d.`id_order` lEFT JOIN `' . _DB_PREFIX_ . 'order_state_current` sc ON sc.`id_order` = d.`id_order` LEFT JOIN `' . _DB_PREFIX_ . 'product_ps_cache` ppc ON ppc.`id_product` = d.`product_id` LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON pl.`id_product` = d.`product_id` AND pl.id_lang = 2 WHERE d.`product_id` IN (' . implode(',', $product_id_array) . ') AND d.`product_attribute_id` = 0 AND sc.`id_order_state` IN ("' . implode('","', $order_states) . '") GROUP BY ppc.`id_sale`, d.`product_id` ORDER BY quantity_sold DESC LIMIT 5'; $returnedArray = Db::getInstance()->ExecuteS($sql, true, $use_cache, __METHOD__); } return $returnedArray; } }