在WooCommerce中免征计算税的产品

时间:2018-10-11 01:29:55

标签: wordpress woocommerce

此税是小费,是客户选择的购物车总额的百分比,但应仅计算一个类别(食品)的总额,而排除另一类别(活动票)。

add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
function woo_add_cart_fee( $cart ){
   if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {
    return;
   }

   if ( isset( $_POST['post_data'] ) ) {
       parse_str( $_POST['post_data'], $post_data );
   } else {
       $post_data = $_POST;
   }

   if (isset($post_data['propina'])) {
       global $woocommerce;
       $porcentaje = $post_data['propina']  / 100;
       $propina = ( $woocommerce->cart->cart_contents_total ) * $porcentaje;
       WC()->cart->add_fee( 'Propina Sugerida:', $propina );
   }
}

简而言之,($ woocommerce-> cart-> cart_contents_total -活动门票总价)*百分比

编辑:我想我找到了解决方案,但是当产品是变体时,我会遇到问题

add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
function woo_add_cart_fee( $cart ){
    if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {
    return;
    }

    if ( isset( $_POST['post_data'] ) ) {
    parse_str( $_POST['post_data'], $post_data );
    } else {
    $post_data = $_POST;
    }

    if (isset($post_data['propina'])) {
        global $woocommerce;

        $items = $woocommerce->cart->get_cart();
        $product_in_cart = false;
        $ticketpamount = 0;
        foreach($items as $item => $values) { 
            $_product =  $values['data'];
            $terms = get_the_terms( $_product->id, 'product_cat' );

        // second level loop search, in case some items have several categories
        foreach ($terms as $term) {
            $_categoryid = $term->term_id;
            if ( $_categoryid === 122 ) {
                //category is in cart!
                $price = get_post_meta($values['product_id'] , '_price', true);
                $ticketpamount += $price;
                $product_in_cart = true;
            }
        }
    } 

    $porcentaje = $post_data['propina']  / 100;
    if ( $product_in_cart ) {
         $propina = ( $woocommerce->cart->cart_contents_total - $ticketpamount ) * $porcentaje;
    } else {
        $propina = ( $woocommerce->cart->cart_contents_total ) * $porcentaje;
    }
    WC()->cart->add_fee( 'Propina Sugerida:', $propina );
   }
}

2 个答案:

答案 0 :(得分:0)

您需要自己计算小计,而不是$woocommerce->cart->cart_contents_total

$subtotal = 0;
foreach ( WC()->cart->get_cart() as $key => $i ) {
    $product_id = $i["product_id"];

    if ( your_product_should_be_included( $product_id ) ) {
        $subtotal += $i['line_subtotal'];
    }
}

答案 1 :(得分:0)

找到了解决方案,添加了变化并乘以数量

add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
function woo_add_cart_fee( $cart ){
    if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {
    return;
    }

    if ( isset( $_POST['post_data'] ) ) {
    parse_str( $_POST['post_data'], $post_data );
    } else {
    $post_data = $_POST;
    }

    if (isset($post_data['propina'])) {
        global $woocommerce;

        $items = $woocommerce->cart->get_cart();
        $product_in_cart = false;
        $ticketpamount = 0;
        foreach($items as $item => $values) { 
            $_product =  $values['data'];
            $terms = get_the_terms( $_product->id, 'product_cat' );

        // second level loop search, in case some items have several categories
        foreach ($terms as $term) {
            $_categoryid = $term->term_id;
            if ( $_categoryid === 122 ) {
                //category is in cart!
                //check if product is a variation and * quantity
                 if ( $item['variation_id'] ) {
                    $price = get_post_meta($values['variation_id'] , '_price', true) * $values['quantity'];
                } else {
                    $price = get_post_meta($values['product_id'] , '_price', true);
                }
                $ticketpamount += $price;
                $product_in_cart = true;
            }
        }
    } 

    $porcentaje = $post_data['propina']  / 100;
    if ( $product_in_cart ) {
         $propina = ( $woocommerce->cart->cart_contents_total - $ticketpamount ) * $porcentaje;
    } else {
        $propina = ( $woocommerce->cart->cart_contents_total ) * $porcentaje;
    }
    WC()->cart->add_fee( 'Propina Sugerida:', $propina );
   }
}