首页 > 解决方案 > 在 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 );
   }
}

标签: wordpresswoocommerce

解决方案


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

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 );
   }
}

推荐阅读