自定义Woocommerce优惠券价格

时间:2018-06-05 05:37:38

标签: woocommerce coupon

我正在为woocommerce优惠券开发自定义功能。我已经创建了优惠券和固定优惠券价格0的组。我想设置自定义价格优惠券自定义价格将从单个产品设置管理员将选择组和金额。如果应用的优惠券属于该组,则金额将被设置为优惠券价格。功能将适用于优惠券。谢谢

add_action(' woocommerce_applied_coupon',' option2_additional_discount',10,1); function option2_additional_discount($ coupon_code){

global $woocommerce;
// echo '<pre>';
// print_r($woocommerce->cart);
// echo '</pre>';

foreach ( $woocommerce->cart->get_cart() as $item_values ) {
    $product_id = $item_values['product_id']; 

    if( have_rows('select_coupon', $product_id) ){ 
        while( have_rows('select_coupon', $product_id) ){
        the_row(); 

            $term = get_sub_field('coupon_group'); 
            if (is_array($term)) {
             $term = $terms[0];
            }

            $coupon_group =  $term->name;
            $coupon_group_id =  $term->term_id;

            $discount_type = get_sub_field('discount_type');
            $coupon_amount = get_sub_field('coupon_amount'); 


            // check the discount code in discount group
            $args = array(
            'post_type' => 'shop_coupon',
            'tax_query' => array(
                array(
                'taxonomy' => 'coupon_group',
                'field' => 'term_id',
                'terms' => $coupon_group_id
                 )
              )
            );
            $query = new WP_Query( $args );


            if( $query->have_posts() ) :

              while( $query->have_posts() ) :
                $query->the_post();
                $coupon_name = get_the_title();

                $applied_coupons = $woocommerce->cart->get_applied_coupons();
                //print_r($applied_coupons);

                if (in_array($coupon_name, $applied_coupons)) {


                    //$wc_coupon->set_amount( floatval(10) );
                    //echo "Match found";
                    //$woocommerce->cart->add_discount( sanitize_text_field( $coupon_name ));
                    //echo $coupon_name;
                    //$discount = $woocommerce->cart->subtotal - $coupon_amount;

                    //$woocommerce->cart->add_fee( __( 'fee', 'yourtext-domain' ) , 10 );

                    //$array =  $woocommerce->cart->get_coupons( $coupon_code );

                    //print_r($array);

                        $amount = 10;
                        $obj = new WC_Coupon($coupon_code);
                        $coupons_amount1 = $obj->get_amount();
                        $obj->set_amount(10);
                        $coupons_amount1 = $obj->get_amount();

                        $woocommerce->cart->add_discount($coupon_name);






                }

              endwhile;
              wp_reset_postdata();


            endif;


        } 
    }

}

}

0 个答案:

没有答案