0

一部のカテゴリにはカタログ価格ルールがあります。フロントエンドでは、category.tpl で、その特定のカテゴリまたはその親のいくつかにバインドされた特別価格があるかどうかを通知する必要があります。

今のところ、クエリでそれを見つけるコントローラーに関数を作成しています。これを行うための近道があるかどうか、私はさまよっていました。

4

1 に答える 1

0

私は自分の問題を解決する関数 (CategoryController メソッド) を作成し、共有します。

    public function get_category_promo(){

    //get the active country, since promo are also country-based
    if($this->context->customer->isLogged()){
        $current_country = Customer::getCurrentCountry($this->context->customer->id);
    } else {
        $current_country = $this->context->country->id;
    }

    $db = Db::getInstance();

    $sql = '
        SELECT 
            truncate(`reduction`,0) as reduction,
            `reduction_type`,
            `from`, 
            `to`, 
            `type` as category_type, 
            `value` as id_category,
            `id_parent` as category_parent,
            `level_depth` as depth
        FROM 
            `'._DB_PREFIX_.'specific_price_rule_condition` rule_condition 
        INNER JOIN 
            `'._DB_PREFIX_.'specific_price_rule_condition_group` rule_group 
            on rule_condition.`id_specific_price_rule_condition_group` = rule_group.`id_specific_price_rule_condition_group`
        INNER JOIN 
            `'._DB_PREFIX_.'specific_price_rule` price_rule 
            on price_rule.`id_specific_price_rule` = rule_group.`id_specific_price_rule`
        INNER JOIN 
            `'._DB_PREFIX_.'category` category
            on rule_condition.`value` = category.`id_category`
        WHERE rule_condition.`type` = "category"';

    $parents = $this->category->getParentsCategories();

    array_shift($parents);//first is == this->category so I shift it out

    $sql_aux = ' and (rule_condition.`value` = ' . $this->category->id_category;
    foreach($parents as $parent){
        $sql_aux .= ' or rule_condition.`value` = ' . $parent["id_category"];
    }
    $sql_aux .= ')';
    $sql_aux .= ' and price_rule.`id_country` = ' . $current_country;
    $sql_aux .= ' order by level_depth desc';

    $sql .= $sql_aux;

    $promo_data = $db->executeS($sql);
    $promo_data = count($promo_data) > 0 ? $promo_data : null;
    if(!$promo_data) return false;//stop

    function validate_date($promo){
        //if there are no dates
        if((int)$promo['to'] == 0 && (int)$promo['from'] == 0) return true;

        //if there is end promo date
        if((int)$promo['to'] != 0){
            $to = new DateTime($promo['to']);
            //...and is still valid
            if($to >= new DateTime('NOW')) return true;
        }
    }//end validate

    //refine query results
    $filtered = array_values(array_filter($promo_data,'validate_date'));

    $promo_data = $filtered[0];//if there are more than one promo on the same category, the promo on the higher depth is used form the system, so I need only that promo

    //promo without dates. Only refine to/from to better use in smarty
    if((int)$promo_data['to'] == 0 && (int)$promo_data['from'] == 0){
        $promo_data['to'] = 0;
        $promo_data['from'] = 0;
        $this->context->smarty->assign('promo_data', $promo_data);
        return true;
    }

    if((int)$promo_data['to'] != 0){//promo ha send date
        $to = new DateTime($promo_data['to']);
        if($to >= new DateTime('NOW')){
            $promo_data['to'] = $to->format('d-m-Y');
        } else {
            return false;//not a valid date
        }
    }

    if((int)$promo_data['from'] != 0){
        $from = new DateTime($promo_data['from']);
        $promo_data['from'] = $from->format('d-m-Y');
    } else {
        $promo_data['from'] = 0;
    }

    $this->context->smarty->assign('promo_data', $promo_data);

}//end get_category_promo
于 2013-09-26T06:24:03.817 に答える