获得产品的功能

时间:2014-05-28 21:03:33

标签: php prestashop prestashop-1.5

如何销售产品? 类Product没有此功能。

我尝试在Product覆盖类中添加一个新方法,但不返回任何产品(我是prestashop代码中的noob)。

    /**
    * Get new products
    *
    * @param integer $id_lang Language id
    * @param integer $pageNumber Start from (optional)
    * @param integer $nbProducts Number of products to return (optional)
    * @return array New products
    */
    public static function getOnSaleProducts($id_lang, $page_number = 0, $nb_products = 10,
        $count = false, $order_by = null, $order_way = null, Context $context = null)
    {
        if (!$context)
            $context = Context::getContext();

        $front = true;
        if (!in_array($context->controller->controller_type, array('front', 'modulefront')))
            $front = false;

        if ($page_number < 0) $page_number = 0;
        if ($nb_products < 1) $nb_products = 10;
        if (empty($order_by) || $order_by == 'position') $order_by = 'date_add';
        if (empty($order_way)) $order_way = 'DESC';
        if ($order_by == 'id_product' || $order_by == 'price' || $order_by == 'date_add'  || $order_by == 'date_upd')
            $order_by_prefix = 'p';
        else if ($order_by == 'name')
            $order_by_prefix = 'pl';
        if (!Validate::isOrderBy($order_by) || !Validate::isOrderWay($order_way))
            die(Tools::displayError());

        $groups = FrontController::getCurrentCustomerGroups();
        $sql_groups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
        if (strpos($order_by, '.') > 0)
        {
            $order_by = explode('.', $order_by);
            $order_by_prefix = $order_by[0];
            $order_by = $order_by[1];
        }
        if ($count)
        {
            $sql = 'SELECT COUNT(DISTINCT p.`id_product`) AS nb
                    FROM `'._DB_PREFIX_.'product` p
                    '.Shop::addSqlAssociation('product', 'p').'
                    WHERE product_shop.`active` = 1
                    AND p.`on_sale` = true
                    AND 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
                    '.($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '').'
                    AND p.`id_product` IN (
                        SELECT cp.`id_product`
                        FROM `'._DB_PREFIX_.'category_group` cg
                        LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
                        WHERE cg.`id_group` '.$sql_groups.'
                    )';
            return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
        }

        $sql = new DbQuery();
        $sql->select(
            'p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity, pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`,
            pl.`meta_keywords`, pl.`meta_title`, pl.`name`, image_shop.`id_image`, il.`legend`, m.`name` AS manufacturer_name,
            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'
        );

        $sql->from('product', 'p');
        $sql->join(Shop::addSqlAssociation('product', 'p'));
        $sql->leftJoin('product_lang', 'pl', '
            p.`id_product` = pl.`id_product`
            AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl')
        );
        $sql->leftJoin('image', 'i', 'i.`id_product` = p.`id_product`');
        $sql->join(Shop::addSqlAssociation('image', 'i'));
        $sql->leftJoin('image_lang', 'il', 'i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang);
        $sql->leftJoin('manufacturer', 'm', 'm.`id_manufacturer` = p.`id_manufacturer`');

        $sql->where('p.`on_sale` = true');
        $sql->where('product_shop.`active` = 1');
        $sql->where('(image_shop.id_image IS NOT NULL OR i.id_image IS NULL) OR (image_shop.id_image IS NULL AND i.cover=1)');
        if ($front)
            $sql->where('product_shop.`visibility` IN ("both", "catalog")');

        $sql->where('p.`id_product` IN (
            SELECT cp.`id_product`
            FROM `'._DB_PREFIX_.'category_group` cg
            LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
            WHERE cg.`id_group` '.$sql_groups.')'
        );

        $sql->orderBy((isset($order_by_prefix) ? pSQL($order_by_prefix).'.' : '').'`'.pSQL($order_by).'` '.pSQL($order_way));
        $sql->limit($nb_products, $page_number * $nb_products);

        $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);

        if ($order_by == 'price')
            Tools::orderbyPrice($result, $order_way);
        if (!$result)
            return false;

        $products_ids = array();
        foreach ($result as $row)
            $products_ids[] = $row['id_product'];
        // Thus you can avoid one query per product, because there will be only one query for all the products of the cart
        Product::cacheFrontFeatures($products_ids, $id_lang);

        return Product::getProductsProperties((int)$id_lang, $result);
    }

}

2 个答案:

答案 0 :(得分:1)

试试这个:

/**
* Get products 'on sale'
*
* @param integer $id_lang Language id
* @param integer $pageNumber Start from (optional)
* @param integer $nbProducts Number of products to return (optional)
* @return array New products
*/
public static function getOnSaleProducts($id_lang, $page_number = 0, $nb_products = 10,
    $count = false, $order_by = null, $order_way = null, Context $context = null)
{
    if (!$context)
        $context = Context::getContext();

    $front = true;
    if (!in_array($context->controller->controller_type, array('front', 'modulefront')))
        $front = false;

    if ($page_number < 0) $page_number = 0;
    if ($nb_products < 1) $nb_products = 10;
    if (empty($order_by) || $order_by == 'position') $order_by = 'date_add';
    if (empty($order_way)) $order_way = 'DESC';
    if ($order_by == 'id_product' || $order_by == 'price' || $order_by == 'date_add'  || $order_by == 'date_upd')
        $order_by_prefix = 'p';
    else if ($order_by == 'name')
        $order_by_prefix = 'pl';
    if (!Validate::isOrderBy($order_by) || !Validate::isOrderWay($order_way))
        die(Tools::displayError());

    $groups = FrontController::getCurrentCustomerGroups();
    $sql_groups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
    if (strpos($order_by, '.') > 0)
    {
        $order_by = explode('.', $order_by);
        $order_by_prefix = $order_by[0];
        $order_by = $order_by[1];
    }
    if ($count)
    {
        $sql = 'SELECT COUNT(DISTINCT p.`id_product`) AS nb
                FROM `'._DB_PREFIX_.'product` p
                '.Shop::addSqlAssociation('product', 'p').'
                WHERE product_shop.`active` = 1
                AND p.`on_sale` = true
                AND 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
                '.($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '').'
                AND p.`id_product` IN (
                    SELECT cp.`id_product`
                    FROM `'._DB_PREFIX_.'category_group` cg
                    LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
                    WHERE cg.`id_group` '.$sql_groups.'
                )';
        return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
    }

    $sql = new DbQuery();
    $sql->select(
        'p.*, product_shop.*, pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`,
        pl.`meta_keywords`, pl.`meta_title`, pl.`name`, image_shop.`id_image`, il.`legend`, m.`name` AS manufacturer_name,
        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'
    );

    $sql->from('product', 'p');
    $sql->join(Shop::addSqlAssociation('product', 'p'));
    $sql->leftJoin('product_lang', 'pl', '
        p.`id_product` = pl.`id_product`
        AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl')
    );
    $sql->leftJoin('image', 'i', 'i.`id_product` = p.`id_product`');
    $sql->join(Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1'));
    $sql->leftJoin('image_lang', 'il', 'i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang);
    $sql->leftJoin('manufacturer', 'm', 'm.`id_manufacturer` = p.`id_manufacturer`');

    $sql->where('p.on_sale = true');
    $sql->where('product_shop.`active` = 1');
    $sql->where('(image_shop.id_image IS NOT NULL OR i.id_image IS NULL) OR (image_shop.id_image IS NULL AND i.cover=1)');
    if ($front)
        $sql->where('product_shop.`visibility` IN ("both", "catalog")');

    $sql->where('p.`id_product` IN (
        SELECT cp.`id_product`
        FROM `'._DB_PREFIX_.'category_group` cg
        LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
        WHERE cg.`id_group` '.$sql_groups.')'
    );

    $sql->orderBy((isset($order_by_prefix) ? pSQL($order_by_prefix).'.' : '').'`'.pSQL($order_by).'` '.pSQL($order_way));
    $sql->limit($nb_products, $page_number * $nb_products);

    $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);

    // return $sql->build(); die();
    if ($order_by == 'price')
        Tools::orderbyPrice($result, $order_way);
    if (!$result)
        return false;


    $products_ids = array();
    foreach ($result as $row)
        $products_ids[] = $row['id_product'];
    // Thus you can avoid one query per product, because there will be only one query for all the products of the cart
    Product::cacheFrontFeatures($products_ids, $id_lang);

    return Product::getProductsProperties((int)$id_lang, $result);
}

答案 1 :(得分:0)

我相信您应该在ProductSale.php中搜索课程ProductSaleCore。 它看起来就像你在寻找的东西。