繁体   English   中英

根据产品类别和运输方式添加费用 WooCommerce

[英]Add fee based on product category and shipping method WooCommerce

如果购物篮中的任何产品的类别 ID 为“160”,我将尝试为每件产品添加 3 英镑的费用。

这很好用,但我也只希望在选择某些运输方式的情况下收取此费用。

到目前为止我所拥有的:

function df_add_ticket_surcharge( $cart_object ) {

    global $woocommerce;
    $specialfeecat = 160; // category id for the special fee
    $spfee = 3.00; // initialize special fee
    $spfeeperprod = 3.00; //special fee per product

    foreach ( $cart_object->cart_contents as $key => $value ) {

        $proid = $value['product_id']; //get the product id from cart
        $quantiy = $value['quantity']; //get quantity from cart
        $itmprice = $value['data']->price; //get product price

        $terms = get_the_terms( $proid, 'product_cat' ); //get taxonamy of the prducts
        if ( $terms && ! is_wp_error( $terms ) ) :
            foreach ( $terms as $term ) {

                $catid = $term->term_id;

                if($specialfeecat == $catid ) {
                    $spfee = $spfee * $quantiy;
                }
            }
        endif;  
    }

    if($spfee > 0 ) {
        $woocommerce->cart->add_fee( 'Ticket Concierge Charge', $spfee, true, 'standard' );
    }

}

add_action( 'woocommerce_cart_calculate_fees', 'df_add_ticket_surcharge' );

我已经尝试从这篇文章中集成 function: https://stackoverflow.com/a/47732732/12019310但似乎无法在没有关键通知的情况下使其运行。

任何建议将不胜感激!

以下代码

  • 如果购物篮中的商品属于类别 ID“160”,则每件商品加收 3 美元的费用
  • 如果选择“local_pickup”
function df_add_ticket_surcharge( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
    $chosen_shipping_method    = explode(':', $chosen_shipping_method_id)[0];

    /* SETTINGS */

    $special_fee_cat = 160; // category id for the special fee
    $fee_per_prod = 3; //special fee per product

    /* END SETTINGS */      

    // Total
    $total = 0;

    // Only for Local pickup chosen shipping method
    if ( strpos( $chosen_shipping_method_id, 'local_pickup' ) !== false ) {
        // Loop though each cart items and set prices in an array
        foreach ( $cart->get_cart() as $cart_item ) {
            // Get product id
            $product_id = $cart_item['product_id'];

            // Quantity
            $product_quantity = $cart_item['quantity'];

            // Check for category
            if ( has_term( $special_fee_cat, 'product_cat', $product_id ) ) {
                $total += $fee_per_prod * $product_quantity;
            }
        }

        // Add the discount
        $cart->add_fee( __('Ticket Concierge Charge', 'woocommerce'), $total );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'df_add_ticket_surcharge', 10, 1 );

这篇文章完全为我服务,但如果我有其他城市有固定的运费,这个脚本对我不起作用。

所以这对我有用!

add_action( 'woocommerce_cart_calculate_fees','custom_pcat_fee');
function custom_pcat_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    
    $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
    $chosen_shipping_method    = explode(':', $chosen_shipping_method_id)[0];

    // Set HERE your categories (can be term IDs, slugs or names) in a coma separated array
    $categories = array('181');
    $fee_amount = 0;

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ){
        if( has_term( $categories, 'product_cat', $cart_item['product_id']) )
            $fee_amount = 35000;
    }
    
    // Only for Local pickup chosen shipping method
    if ( strpos( $chosen_shipping_method_id, 'flat_rate' ) !== false || strpos( $chosen_shipping_method_id, 'filters_by_cities_shipping_method' ) !== false ) {
        // Loop though each cart items and set prices in an array
        foreach ( $cart->get_cart() as $cart_item ) {
            // Get product id
            $product_id = $cart_item['product_id'];

            // Quantity
            $product_quantity = $cart_item['quantity'];

            // Check for category
            if ( has_term( $special_fee_cat, 'product_cat', $product_id ) ) {
                $total += $fee_per_prod * $product_quantity;
            }
        }

    // Adding the fee
    if ( $fee_amount > 0 ){
        // Last argument is related to enable tax (true or false)
        WC()->cart->add_fee( __( "Costo de trasteo de mobiliario", "woocommerce" ), $fee_amount, false );
    }
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM