Untitled

mail@pastecode.io avatar
unknown
php
2 years ago
4.0 kB
3
Indexable
Never
    public function getVouchersForProductAndAddToNewProduct($id_product){
        
        $getCartRulesQuery = "SELECT * FROM `". _DB_PREFIX_ ."cart_rule` WHERE `code`= '' ";
        $getCartRules = DB::getInstance()->executeS($getCartRulesQuery);
        
        if( count($getCartRules) == 0 ){
            return [];
        }

        $vouchers = [];

        $use_reduction = [];

        foreach ($getCartRules as $cartVoucher) {

            //$use_reduction[$cartVoucher['id_cart_rule']] = false;
            
            $CartRule = new CartRule((int)$cartVoucher['id_cart_rule']);

            $cart_rule_product_rule_groups_query = "SELECT * FROM `". _DB_PREFIX_ ."cart_rule_product_rule_group` WHERE id_cart_rule =".(int)$cartVoucher['id_cart_rule'];
            $cart_rule_product_rule_groups = \DB::getInstance()->executeS($cart_rule_product_rule_groups_query);
            
            $lists = [];

            if( count($cart_rule_product_rule_groups) > 0){
                foreach ($cart_rule_product_rule_groups as $cart_rule_product_rule_group) {
                    $cart_rule_product_rules_query = "SELECT * FROM `". _DB_PREFIX_ ."cart_rule_product_rule` WHERE id_product_rule_group =".$cart_rule_product_rule_group['id_product_rule_group'];
                    $cart_rule_product_rules = \DB::getInstance()->executeS($cart_rule_product_rules_query);
                      
                    if(count($cart_rule_product_rules) > 0){
                        foreach ($cart_rule_product_rules as $cart_rule_product_rule) {
                            $cart_rule_product_rule_values_query = "SELECT DISTINCT id_item FROM `". _DB_PREFIX_ ."cart_rule_product_rule_value` WHERE id_product_rule=". (int)$cart_rule_product_rule['id_product_rule'];
                            $cart_rule_product_rule_values = \DB::getInstance()->executeS($cart_rule_product_rule_values_query);
                            $selected = [];
                            if( count($cart_rule_product_rule_values) > 0){
                                foreach ($cart_rule_product_rule_values as $cart_rule_product_rule_value) {
                                    $lists[$cart_rule_product_rule['type']][] =  (int)$cart_rule_product_rule_value['id_item'];
                                }
                            }
                        }
                    }       
                }
            }

            if( count($lists) > 0 ){
                foreach ($lists as $type => $value) {
                    switch ($type) {
                        case 'attributes':
                            //TODO

                            break;
                        case 'products':
                        
                            if(in_array($id_product, $lists[$type])){
                                $use_reduction[] = $cartVoucher['id_cart_rule'];
                            }

                            break;
                        case 'manufacturers':
                            //TODO

                            break;
                        case 'suppliers':
                            //TODO

                            break;
                        case 'categories':
                            /*$oProduct = new Product($id_product);
                            $aCategory = $oProduct->getCategories();

                            if(count($aCategory) > 0 ){
                            
                                foreach ($aCategory as $category) {
                            
                                    if(in_array($category, $lists[$type])){
                                        $use_reduction[] = $cartVoucher['id_cart_rule'];
                                    }
                                }
                            }*/

                            break;
                        default:
                            //TODO
                            break;
                    }
                }
            }
        }
        
        return $use_reduction;
    }