/** * {@inheritdoc} */ public function getCatalogProductList($offset, $limit, $orderBy, $sortOrder, $post = array(), $avoidPersistence = false, $formatCldr = true) { $filterParams = $this->combinePersistentCatalogProductFilter(array_merge($post, ['last_offset' => $offset, 'last_limit' => $limit, 'last_orderBy' => $orderBy, 'last_sortOrder' => $sortOrder]), $avoidPersistence); $filterParams = AdminFilter::sanitizeFilterParameters($filterParams); $showPositionColumn = $this->isCategoryFiltered(); if ($orderBy == 'position_ordering' && $showPositionColumn) { foreach ($filterParams as $key => $param) { if (strpos($key, 'filter_column_') === 0) { $filterParams[$key] = ''; } } } if ($orderBy == 'position_ordering') { $orderBy = 'position'; } $idShop = \ContextCore::getContext()->shop->id; $idLang = \ContextCore::getContext()->language->id; $sqlSelect = array('id_product' => array('table' => 'p', 'field' => 'id_product', 'filtering' => ' %s '), 'reference' => array('table' => 'p', 'field' => 'reference', 'filtering' => self::FILTERING_LIKE_BOTH), 'price' => array('table' => 'p', 'field' => 'price', 'filtering' => ' %s '), 'id_shop_default' => array('table' => 'p', 'field' => 'id_shop_default'), 'is_virtual' => array('table' => 'p', 'field' => 'is_virtual'), 'name' => array('table' => 'pl', 'field' => 'name', 'filtering' => self::FILTERING_LIKE_BOTH), 'active' => array('table' => 'sa', 'field' => 'active', 'filtering' => self::FILTERING_EQUAL_NUMERIC), 'shopname' => array('table' => 'shop', 'field' => 'name'), 'id_image' => array('table' => 'image_shop', 'field' => 'id_image'), 'name_category' => array('table' => 'cl', 'field' => 'name', 'filtering' => self::FILTERING_LIKE_BOTH), 'price_final' => '0', 'nb_downloadable' => array('table' => 'pd', 'field' => 'nb_downloadable'), 'sav_quantity' => array('table' => 'sav', 'field' => 'quantity', 'filtering' => ' %s '), 'badge_danger' => array('select' => 'IF(sav.`quantity`<=0, 1, 0)', 'filtering' => 'IF(sav.`quantity`<=0, 1, 0) = %s')); $sqlTable = array('p' => 'product', 'pl' => array('table' => 'product_lang', 'join' => 'LEFT JOIN', 'on' => 'pl.`id_product` = p.`id_product` AND pl.`id_lang` = ' . $idLang . ' AND pl.`id_shop` = ' . $idShop), 'sav' => array('table' => 'stock_available', 'join' => 'LEFT JOIN', 'on' => 'sav.`id_product` = p.`id_product` AND sav.`id_product_attribute` = 0 AND sav.id_shop = ' . $idShop), 'sa' => array('table' => 'product_shop', 'join' => 'JOIN', 'on' => 'p.`id_product` = sa.`id_product` AND sa.id_shop = ' . $idShop), 'cl' => array('table' => 'category_lang', 'join' => 'LEFT JOIN', 'on' => 'sa.`id_category_default` = cl.`id_category` AND cl.`id_lang` = ' . $idLang . ' AND cl.id_shop = ' . $idShop), 'c' => array('table' => 'category', 'join' => 'LEFT JOIN', 'on' => 'c.`id_category` = cl.`id_category`'), 'shop' => array('table' => 'shop', 'join' => 'LEFT JOIN', 'on' => 'shop.id_shop = ' . $idShop), 'image_shop' => array('table' => 'image_shop', 'join' => 'LEFT JOIN', 'on' => 'image_shop.`id_product` = p.`id_product` AND image_shop.`cover` = 1 AND image_shop.id_shop = ' . $idShop), 'i' => array('table' => 'image', 'join' => 'LEFT JOIN', 'on' => 'i.`id_image` = image_shop.`id_image`'), 'pd' => array('table' => 'product_download', 'join' => 'LEFT JOIN', 'on' => 'pd.`id_product` = p.`id_product`')); $sqlWhere = array('AND', 1); foreach ($filterParams as $filterParam => $filterValue) { if (!$filterValue && $filterValue !== '0') { continue; } if (strpos($filterParam, 'filter_column_') === 0) { $filterValue = \Db::getInstance()->escape($filterValue, in_array($filterParam, ['filter_column_id_product', 'filter_column_sav_quantity', 'filter_column_price']), true); $field = substr($filterParam, 14); // 'filter_column_' takes 14 chars if (isset($sqlSelect[$field]['table'])) { $sqlWhere[] = $sqlSelect[$field]['table'] . '.`' . $sqlSelect[$field]['field'] . '` ' . sprintf($sqlSelect[$field]['filtering'], $filterValue); } else { $sqlWhere[] = '(' . sprintf($sqlSelect[$field]['filtering'], $filterValue) . ')'; } } // for 'filter_category', see next if($showPositionColumn) block. } $sqlWhere[] = 'state = ' . \Product::STATE_SAVED; $sqlOrder = array($orderBy . ' ' . $sortOrder); if ($orderBy != 'id_product') { $sqlOrder[] = 'id_product asc'; // secondary order by (useful when ordering by active, quantity, price, etc...) } $sqlLimit = $offset . ', ' . $limit; // Column 'position' added if filtering by category if ($showPositionColumn) { $filteredCategoryId = (int) $filterParams['filter_category']; $sqlSelect['position'] = array('table' => 'cp', 'field' => 'position'); $sqlTable['cp'] = array('table' => 'category_product', 'join' => 'INNER JOIN', 'on' => 'cp.`id_product` = p.`id_product` AND cp.`id_category` = ' . $filteredCategoryId); } elseif ($orderBy == 'position') { // We do not show position column, so we do not join the table, so we do not order by position! $sqlOrder = array('id_product ASC'); } // exec legacy hook but with different parameters (retro-compat < 1.7 is broken here) \HookCore::exec('actionAdminProductsListingFieldsModifier', array('_ps_version' => _PS_VERSION_, 'sql_select' => &$sqlSelect, 'sql_table' => &$sqlTable, 'sql_where' => &$sqlWhere, 'sql_order' => &$sqlOrder, 'sql_limit' => &$sqlLimit)); $sql = $this->compileSqlQuery($sqlSelect, $sqlTable, $sqlWhere, $sqlOrder, $sqlLimit); $products = \Db::getInstance()->executeS($sql, true, false); $total = \Db::getInstance()->executeS('SELECT FOUND_ROWS();', true, false); $total = $total[0]['FOUND_ROWS()']; // post treatment $currency = new \CurrencyCore(\Configuration::get('PS_CURRENCY_DEFAULT')); foreach ($products as &$product) { $product['total'] = $total; // total product count (filtered) $product['price_final'] = \ProductCore::getPriceStatic($product['id_product'], true, null, (int) \Configuration::get('PS_PRICE_DISPLAY_PRECISION'), null, false, true, 1, true, null, null, null, $nothing, true, true); if ($formatCldr) { $product['price'] = \ToolsCore::displayPrice($product['price'], $currency); $product['price_final'] = \ToolsCore::displayPrice($product['price_final'], $currency); } $product['image'] = $this->imageManager->getThumbnailForListing($product['id_image']); } // post treatment by hooks // exec legacy hook but with different parameters (retro-compat < 1.7 is broken here) \HookCore::exec('actionAdminProductsListingResultsModifier', array('_ps_version' => _PS_VERSION_, 'products' => &$products, 'total' => $total)); return $products; }
/** * Get specific prices list for a product * * @param object $product * @param object $defaultCurrency * @param array $shops Available shops * @param array $currencies Available currencies * @param array $countries Available countries * @param array $groups Available users groups * * @return array */ public function getSpecificPricesList($product, $defaultCurrency, $shops, $currencies, $countries, $groups) { $content = []; $specific_prices = \SpecificPriceCore::getByProductId((int) $product->id); $tmp = array(); foreach ($shops as $shop) { $tmp[$shop['id_shop']] = $shop; } $shops = $tmp; $tmp = array(); foreach ($currencies as $currency) { $tmp[$currency['id_currency']] = $currency; } $currencies = $tmp; $tmp = array(); foreach ($countries as $country) { $tmp[$country['id_country']] = $country; } $countries = $tmp; $tmp = array(); foreach ($groups as $group) { $tmp[$group['id_group']] = $group; } $groups = $tmp; if (is_array($specific_prices) && count($specific_prices)) { foreach ($specific_prices as $specific_price) { $id_currency = $specific_price['id_currency'] ? $specific_price['id_currency'] : $defaultCurrency->id; if (!isset($currencies[$id_currency])) { continue; } $current_specific_currency = $currencies[$id_currency]; if ($specific_price['reduction_type'] == 'percentage') { $impact = '- ' . $specific_price['reduction'] * 100 . ' %'; } elseif ($specific_price['reduction'] > 0) { $impact = '- ' . \ToolsCore::displayPrice(\Tools::ps_round($specific_price['reduction'], 2), $current_specific_currency) . ' '; if ($specific_price['reduction_tax']) { $impact .= '(' . $this->translator->trans('Tax incl.', [], 'Admin.Global') . ')'; } else { $impact .= '(' . $this->translator->trans('Tax excl.', [], 'Admin.Global') . ')'; } } else { $impact = '--'; } if ($specific_price['from'] == '0000-00-00 00:00:00' && $specific_price['to'] == '0000-00-00 00:00:00') { $period = $this->translator->trans('Unlimited', [], 'Admin.Global'); } else { $period = $this->translator->trans('From', [], 'Admin.Global') . ' ' . ($specific_price['from'] != '0000-00-00 00:00:00' ? $specific_price['from'] : '0000-00-00 00:00:00') . '<br />' . $this->translator->trans('to', [], 'Admin.Global') . ' ' . ($specific_price['to'] != '0000-00-00 00:00:00' ? $specific_price['to'] : '0000-00-00 00:00:00'); } if ($specific_price['id_product_attribute']) { $combination = new \CombinationCore((int) $specific_price['id_product_attribute']); $attributes = $combination->getAttributesName(1); $attributes_name = ''; foreach ($attributes as $attribute) { $attributes_name .= $attribute['name'] . ' - '; } $attributes_name = rtrim($attributes_name, ' - '); } else { $attributes_name = $this->translator->trans('All combinations', [], 'Admin.Catalog.Feature'); } $rule = new \SpecificPriceRuleCore((int) $specific_price['id_specific_price_rule']); $rule_name = $rule->id ? $rule->name : '--'; if ($specific_price['id_customer']) { $customer = new \CustomerCore((int) $specific_price['id_customer']); if (\ValidateCore::isLoadedObject($customer)) { $customer_full_name = $customer->firstname . ' ' . $customer->lastname; } unset($customer); } if (!$specific_price['id_shop'] || in_array($specific_price['id_shop'], \ShopCore::getContextListShopID())) { $can_delete_specific_prices = true; if (\ShopCore::isFeatureActive()) { $can_delete_specific_prices = count($this->legacyContext->employee->getAssociatedShops()) > 1 && !$specific_price['id_shop'] || $specific_price['id_shop']; } $price = \ToolsCore::ps_round($specific_price['price'], 2); $fixed_price = $price == \ToolsCore::ps_round($product->price, 2) || $specific_price['price'] == -1 ? '--' : \ToolsCore::displayPrice($price, $current_specific_currency); $content[] = ['id_specific_price' => $specific_price['id_specific_price'], 'id_product' => $product->id, 'rule_name' => $rule_name, 'attributes_name' => $attributes_name, 'shop' => $specific_price['id_shop'] ? $shops[$specific_price['id_shop']]['name'] : $this->translator->trans('All shops', [], 'Admin.Global'), 'currency' => $specific_price['id_currency'] ? $currencies[$specific_price['id_currency']]['name'] : $this->translator->trans('All currencies', [], 'Admin.Global'), 'country' => $specific_price['id_country'] ? $countries[$specific_price['id_country']]['name'] : $this->translator->trans('All countries', [], 'Admin.Global'), 'group' => $specific_price['id_group'] ? $groups[$specific_price['id_group']]['name'] : $this->translator->trans('All groups', [], 'Admin.Global'), 'customer' => isset($customer_full_name) ? $customer_full_name : $this->translator->trans('All customers', [], 'Admin.Global'), 'fixed_price' => $fixed_price, 'impact' => $impact, 'period' => $period, 'from_quantity' => $specific_price['from_quantity'], 'can_delete' => !$rule->id && $can_delete_specific_prices ? true : false]; unset($customer_full_name); } } } return $content; }