public static function find($id_lang, $expr, $page_number = 1, $page_size = 1, $order_by = 'position', $order_way = 'desc', $ajax = false, $use_cookie = true, Context $context = null) { // Call parent method $find = parent::find($id_lang, $expr, $page_number, $page_size, $order_by, $order_way, $ajax, $use_cookie, $context); if (isset($find['result']) && !empty($find['result']) && Module::isInstalled('mymodcomments')) { // List id product $products = $find['result']; $id_product_list = array(); foreach ($products as $p) { $id_product_list[] = (int) $p['id_product']; } // Get grade average and nb comments for products in list require_once dirname(__FILE__) . '/../../modules/mymodcomments/classes/MyModComment.php'; $grades_comments = MyModComment::getInfosOnProductsList($id_product_list); // Associate grade and nb comments with product foreach ($products as $kp => $p) { foreach ($grades_comments as $gc) { if ($gc['id_product'] == $p['id_product']) { $products[$kp]['mymodcomments']['grade_avg'] = round($gc['grade_avg']); $products[$kp]['mymodcomments']['nb_comments'] = $gc['nb_comments']; } } } $find['result'] = $products; } // Return products return $find; }
public static function searchTag($id_lang, $tag, $count = false, $pageNumber = 0, $pageSize = 10, $orderBy = false, $orderWay = false, $useCookie = true, Context $context = null) { if (!$context) { $context = Context::getContext(); } if (!Module::isInstalled('agilemultipleseller') and !Module::isInstalled('agilesellerlistoptions')) { return parent::searchTag($id_lang, $expr, $pageNumber, $pageSize, $orderBy, $orderWay, $ajax, $useCookie, $context); } $agile_sql_parts = AgileSellerManager::getAdditionalSqlForProducts("p"); if ($useCookie) { $id_customer = (int) $context->customer->id; } else { $id_customer = 0; } if (!is_numeric($pageNumber) || !is_numeric($pageSize) || !Validate::isBool($count) || !Validate::isValidSearch($tag) || $orderBy && !$orderWay || $orderBy && !Validate::isOrderBy($orderBy) || $orderWay && !Validate::isOrderBy($orderWay)) { return false; } if ($pageNumber < 1) { $pageNumber = 1; } if ($pageSize < 1) { $pageSize = 10; } $id = Context::getContext()->shop->id; $id_shop = $id ? $id : Configuration::get('PS_SHOP_DEFAULT'); if ($count) { $sql = 'SELECT COUNT(DISTINCT pt.`id_product`) nb FROM `' . _DB_PREFIX_ . 'product` p ' . $agile_sql_parts['joins'] . ' ' . Shop::addSqlAssociation('product', 'p') . ' LEFT JOIN `' . _DB_PREFIX_ . 'product_tag` pt ON (p.`id_product` = pt.`id_product`) LEFT JOIN `' . _DB_PREFIX_ . 'tag` t ON (pt.`id_tag` = t.`id_tag` AND t.`id_lang` = ' . (int) $id_lang . ') LEFT JOIN `' . _DB_PREFIX_ . 'category_product` cp ON (cp.`id_product` = p.`id_product`) LEFT JOIN `' . _DB_PREFIX_ . 'category_shop` cs ON (cp.`id_category` = cs.`id_category` AND cs.`id_shop` = ' . (int) $id_shop . ') LEFT JOIN `' . _DB_PREFIX_ . 'category_group` cg ON (cg.`id_category` = cp.`id_category`) WHERE product_shop.`active` = 1 ' . $agile_sql_parts['wheres'] . ' AND cs.`id_shop` = ' . (int) Context::getContext()->shop->id . ' AND cg.`id_group` ' . (!$id_customer ? '= ' . (int) Configuration::get('PS_UNIDENTIFIED_GROUP') : 'IN ( SELECT id_group FROM ' . _DB_PREFIX_ . 'customer_group WHERE id_customer = ' . (int) $id_customer . ')') . ' AND t.`name` LIKE \'%' . pSQL($tag) . '%\''; return (int) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql); } $sql = 'SELECT DISTINCT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity, pl.`description_short`, pl.`link_rewrite`, pl.`name`, MAX(image_shop.`id_image`) id_image, il.`legend`, m.`name` manufacturer_name, 1 position, DATEDIFF( p.`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 new ' . $agile_sql_parts['selects'] . ' FROM `' . _DB_PREFIX_ . 'product` p ' . $agile_sql_parts['joins'] . ' INNER JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON ( p.`id_product` = pl.`id_product` AND pl.`id_lang` = ' . (int) $id_lang . Shop::addSqlRestrictionOnLang('pl') . ' ) ' . Shop::addSqlAssociation('product', 'p', false) . ' 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 (i.`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`) LEFT JOIN `' . _DB_PREFIX_ . 'product_tag` pt ON (p.`id_product` = pt.`id_product`) LEFT JOIN `' . _DB_PREFIX_ . 'tag` t ON (pt.`id_tag` = t.`id_tag` AND t.`id_lang` = ' . (int) $id_lang . ') LEFT JOIN `' . _DB_PREFIX_ . 'category_product` cp ON (cp.`id_product` = p.`id_product`) LEFT JOIN `' . _DB_PREFIX_ . 'category_group` cg ON (cg.`id_category` = cp.`id_category`) LEFT JOIN `' . _DB_PREFIX_ . 'category_shop` cs ON (cg.`id_category` = cs.`id_category` AND cs.`id_shop` = ' . (int) $id_shop . ') ' . Product::sqlStock('p', 0) . ' WHERE product_shop.`active` = 1 ' . $agile_sql_parts['wheres'] . ' AND cs.`id_shop` = ' . (int) Context::getContext()->shop->id . ' AND cg.`id_group` ' . (!$id_customer ? '= ' . (int) Configuration::get('PS_UNIDENTIFIED_GROUP') : 'IN ( SELECT id_group FROM ' . _DB_PREFIX_ . 'customer_group WHERE id_customer = ' . (int) $id_customer . ')') . ' AND t.`name` LIKE \'%' . pSQL($tag) . '%\' GROUP BY product_shop.id_product ORDER BY position DESC' . ($orderBy ? ', ' . $orderBy : '') . ($orderWay ? ' ' . $orderWay : '') . ' LIMIT ' . (int) (($pageNumber - 1) * $pageSize) . ',' . (int) $pageSize; if (!($result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql))) { return false; } $results = Product::getProductsProperties((int) $id_lang, $result); $results = AgileSellerManager::prepareSellerRattingInfo($results); return $results; }
public function duplicateProduct($productId, $namePattern = 'copy of %s') { //TODO : use the $namePattern var to input translated version of 'copy of %s', if translation requested. $product = new \ProductCore($productId); if (!\ValidateCore::isLoadedObject($product)) { throw new \Exception('AdminProductDataUpdater->duplicateProduct() received an unknown ID.', 5005); } $id_product_old = $product->id; if (empty($product->price) && \ShopCore::getContext() == \ShopCore::CONTEXT_GROUP) { $shops = \ShopGroupCore::getShopsFromGroup(\ShopCore::getContextShopGroupID()); foreach ($shops as $shop) { if ($product->isAssociatedToShop($shop['id_shop'])) { $product_price = new \ProductCore($id_product_old, false, null, $shop['id_shop']); $product->price = $product_price->price; } } } unset($product->id); unset($product->id_product); $product->indexed = 0; $product->active = 0; // change product name to prefix it foreach ($product->name as $langKey => $oldName) { if (!preg_match('/^' . str_replace('%s', '.*', preg_quote($namePattern, '/') . '$/'), $oldName)) { $newName = sprintf($namePattern, $oldName); if (mb_strlen($newName, 'UTF-8') <= 127) { $product->name[$langKey] = $newName; } } } if ($product->add() && \CategoryCore::duplicateProductCategories($id_product_old, $product->id) && \ProductCore::duplicateSuppliers($id_product_old, $product->id) && ($combination_images = \ProductCore::duplicateAttributes($id_product_old, $product->id)) !== false && \GroupReductionCore::duplicateReduction($id_product_old, $product->id) && \ProductCore::duplicateAccessories($id_product_old, $product->id) && \ProductCore::duplicateFeatures($id_product_old, $product->id) && \ProductCore::duplicateSpecificPrices($id_product_old, $product->id) && \PackCore::duplicate($id_product_old, $product->id) && \ProductCore::duplicateCustomizationFields($id_product_old, $product->id) && \ProductCore::duplicateTags($id_product_old, $product->id) && \ProductCore::duplicateDownload($id_product_old, $product->id)) { if ($product->hasAttributes()) { \ProductCore::updateDefaultAttribute($product->id); } if (!\Image::duplicateProductImages($id_product_old, $product->id, $combination_images)) { throw new DataUpdateException('product', $id_product_old, 'An error occurred while copying images.', 5008); } else { $this->hookDispatcher->dispatchForParameters('actionProductAdd', array('id_product' => (int) $product->id, 'product' => $product)); if (in_array($product->visibility, array('both', 'search')) && \Configuration::get('PS_SEARCH_INDEXATION')) { \SearchCore::indexation(false, $product->id); } return $product->id; } } else { throw new \Exception('An error occurred while creating an object.', 5009); } }