/** * Return current category products * * @param integer $id_lang Language ID * @param integer $p Page number * @param integer $n Number of products per page * @param boolean $get_total return the number of results instead of the results themself * @param boolean $active return only active products * @param boolean $random active a random filter for returned products * @param int $random_number_products number of products to return if random is activated * @param boolean $check_access set to false to return all products (even if customer hasn't access) * @return mixed Products or number of products * This edit function for right sorting by price */ public function getProducts($id_lang, $p, $n, $order_by = null, $order_way = null, $get_total = false, $active = true, $random = false, $random_number_products = 1, $check_access = true, Context $context = null) { $manufacturer = ToolsCore::getValue('manufacturer', 0); if (!$context) { $context = Context::getContext(); } if ($check_access && !$this->checkAccess($context->customer->id)) { return false; } $front = true; if (!in_array($context->controller->controller_type, array('front', 'modulefront'))) { $front = false; } if ($p < 1) { $p = 1; } if (empty($order_by)) { $order_by = 'position'; } else { /* Fix for all modules which are now using lowercase values for 'orderBy' parameter */ $order_by = strtolower($order_by); } if (empty($order_way)) { $order_way = 'ASC'; } if ($order_by == 'id_product' || $order_by == 'date_add' || $order_by == 'date_upd') { $order_by_prefix = 'p'; } elseif ($order_by == 'name') { $order_by_prefix = 'pl'; } elseif ($order_by == 'manufacturer') { $order_by_prefix = 'm'; $order_by = 'name'; } elseif ($order_by == 'position') { $order_by_prefix = 'cp'; } if ($order_by == 'price') { $order_by = 'orderprice'; } if (!Validate::isBool($active) || !Validate::isOrderBy($order_by) || !Validate::isOrderWay($order_way)) { die(Tools::displayError()); } $id_supplier = (int) Tools::getValue('id_supplier'); /* Return only the number of products */ if ($get_total) { $sql = 'SELECT COUNT(cp.`id_product`) AS total FROM `' . _DB_PREFIX_ . 'product` p ' . Shop::addSqlAssociation('product', 'p') . ' LEFT JOIN `' . _DB_PREFIX_ . 'category_product` cp ON p.`id_product` = cp.`id_product` WHERE cp.`id_category` = ' . (int) $this->id . ($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '') . ($active ? ' AND product_shop.`active` = 1' : '') . ($id_supplier ? 'AND p.id_supplier = ' . (int) $id_supplier : ''); return (int) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql); } $sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity, MAX(product_attribute_shop.id_product_attribute) id_product_attribute, product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity, pl.`description`, pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, MAX(image_shop.`id_image`) id_image, il.`legend`, m.`name` AS manufacturer_name, cl.`name` AS category_default, DATEDIFF(product_shop.`date_add`, DATE_SUB(NOW(), INTERVAL ' . (Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20) . ' DAY)) > 0 AS new, product_shop.price AS orderprice FROM `' . _DB_PREFIX_ . 'category_product` cp LEFT JOIN `' . _DB_PREFIX_ . 'product` p ON p.`id_product` = cp.`id_product` ' . Shop::addSqlAssociation('product', 'p') . ' LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa ON (p.`id_product` = pa.`id_product`) ' . Shop::addSqlAssociation('product_attribute', 'pa', false, 'product_attribute_shop.`default_on` = 1') . ' ' . Product::sqlStock('p', 'product_attribute_shop', false, $context->shop) . ' LEFT JOIN `' . _DB_PREFIX_ . 'category_lang` cl ON (product_shop.`id_category_default` = cl.`id_category` AND cl.`id_lang` = ' . (int) $id_lang . Shop::addSqlRestrictionOnLang('cl') . ') LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = ' . (int) $id_lang . Shop::addSqlRestrictionOnLang('pl') . ') LEFT JOIN `' . _DB_PREFIX_ . 'image` i ON (i.`id_product` = p.`id_product`)' . Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1') . ' LEFT JOIN `' . _DB_PREFIX_ . 'image_lang` il ON (image_shop.`id_image` = il.`id_image` AND il.`id_lang` = ' . (int) $id_lang . ') LEFT JOIN `' . _DB_PREFIX_ . 'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer` WHERE product_shop.`id_shop` = ' . (int) $context->shop->id . ' AND cp.`id_category` = ' . (int) $this->id . ($active ? ' AND product_shop.`active` = 1' : '') . ($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '') . ($id_supplier ? ' AND p.id_supplier = ' . (int) $id_supplier : '') . ($manufacturer ? ' AND p.id_manufacturer = ' . (int) $manufacturer : '') . ' GROUP BY product_shop.id_product'; if ($random === true) { $sql .= ' ORDER BY RAND()'; $sql .= ' LIMIT 0, ' . (int) $random_number_products; } else { $sql .= ' ORDER BY ' . (isset($order_by_prefix) ? $order_by_prefix . '.' : '') . '`' . pSQL($order_by) . '` ' . pSQL($order_way); //custom for right sort by price// //LIMIT '.(((int)$p - 1) * (int)$n).','.(int)$n; //-------------------------------// } $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); if ($order_by == 'orderprice') { Tools::orderbyPrice($result, $order_way); } //-------- custom for right sort by price -----------------// $result = array_slice($result, ((int) $p - 1) * (int) $n, (int) $n); //--------------------------------------------------------// if (!$result) { return array(); } /* Modify SQL result */ return Product::getProductsProperties($id_lang, $result); }
/** * Check and add a Trusted Shops certificate in shop. * * @uses TSBuyerProtection::_getProtectionItems() * to get all buyer protection products from Trusted Shops * @uses TSBuyerProtection::_saveProtectionItems() * to save buyer protection products in shop * @return boolean true if certificate is added successfully, false otherwise */ private function _submitAddCertificate() { $checked_certificate = false; try { $checked_certificate = $this->_checkCertificate(ToolsCore::getValue('new_certificate')); } catch (TSBPException $e) { $this->errors[] = $e->getMessage(); } if ($checked_certificate) { TSBuyerProtection::$CERTIFICATE[strtoupper($checked_certificate->certificationLanguage)] = array('stateEnum' => $checked_certificate->stateEnum, 'typeEnum' => $checked_certificate->typeEnum, 'url' => $checked_certificate->url, 'tsID' => $checked_certificate->tsID, 'user' => '', 'password' => ''); // update the configuration var Configuration::updateValue(TSBuyerProtection::PREFIX_TABLE . 'CERTIFICATE_' . strtoupper($checked_certificate->certificationLanguage), Tools::htmlentitiesUTF8(Tools::jsonEncode(TSBuyerProtection::$CERTIFICATE[strtoupper($checked_certificate->certificationLanguage)]))); $this->confirmations[] = $this->l('Certificate has been added successfully.'); if ($checked_certificate->typeEnum === 'EXCELLENCE') { try { $protection_items = $this->_getProtectionItems($checked_certificate->tsID); if ($protection_items) { $this->_saveProtectionItems($protection_items, $checked_certificate->tsID); } } catch (TSBPException $e) { $this->errors[] = $e->getMessage(); } } return true; } return false; }
/** * Check, confirm and add a Trusted Shops certificate in shop. * * @uses TSCommon::getProtectionItems() * to get all buyer protection products from Trusted Shops * @uses TSCommon::saveProtectionItems() * to save buyer protection products in shop * @return boolean true if certificate is added successfully, false otherwise */ private function submitConfirmCertificate() { $checked_certificate = false; try { $checked_certificate = $this->checkCertificate(ToolsCore::getValue('new_certificate'), Tools::getValue('lang')); } catch (TSBPException $e) { $this->errors[] = $e->getMessage(); } if ($checked_certificate) { TSCommon::$certificates[Tools::strtoupper($checked_certificate->certificationLanguage)] = array('stateEnum' => $checked_certificate->stateEnum, 'typeEnum' => $checked_certificate->typeEnum, 'tsID' => $checked_certificate->tsID, 'url' => $checked_certificate->url, 'user' => '', 'password' => '', 'variant' => 'default', 'yoffset' => '0', 'jscode' => '', 'display_rating_front_end' => '1', 'display_rating_oc' => '0', 'send_separate_mail' => '0', 'send_seperate_mail_delay' => '0', 'send_seperate_mail_order_state' => Configuration::get('PS_OS_SHIPPING')); // update the configuration var Configuration::updateValue(TSCommon::PREFIX_TABLE . 'CERTIFICATE_' . Tools::strtoupper($checked_certificate->certificationLanguage), Tools::htmlentitiesUTF8(Tools::jsonEncode(TSCommon::$certificates[Tools::strtoupper($checked_certificate->certificationLanguage)]))); unset(self::$available_languages_for_adding[Tools::strtoupper($checked_certificate->certificationLanguage)]); $this->confirmations[] = $this->l('Your Trusted Shops membership is valid.'); if ($checked_certificate->typeEnum !== 'UNKNOWN') { self::registerCertificate($checked_certificate->tsID); } if ($checked_certificate->typeEnum === 'EXCELLENCE') { try { $protection_items = $this->getProtectionItems($checked_certificate->tsID); if ($protection_items) { $this->saveProtectionItems($protection_items, $checked_certificate->tsID); } } catch (TSBPException $e) { $this->errors[] = $e->getMessage(); } } } return (bool) $checked_certificate; }