Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(prestashop)如何检测是否存在';目录价格规则绑定到某个类别或其某些父类别?_Prestashop_Categories - Fatal编程技术网

(prestashop)如何检测是否存在';目录价格规则绑定到某个类别或其某些父类别?

(prestashop)如何检测是否存在';目录价格规则绑定到某个类别或其某些父类别?,prestashop,categories,Prestashop,Categories,我有一些类别的目录价格规则。在前端,在category.tpl中,我必须通知该特定类别或其某些父类别是否有特殊价格 现在,我正在控制器上构建一个函数,通过查询找到它。我想知道是否有一些快捷方式可以做到这一点。我编写了一个函数(CategoryController方法)来解决我的问题,我与大家分享: public function get_category_promo(){ //get the active country, since promo are also countr

我有一些类别的目录价格规则。在前端,在category.tpl中,我必须通知该特定类别或其某些父类别是否有特殊价格

现在,我正在控制器上构建一个函数,通过查询找到它。我想知道是否有一些快捷方式可以做到这一点。

我编写了一个函数(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