Add a Woocommerce fee based on shipping class and item quantity

穿精又带淫゛_ 提交于 2021-02-08 05:47:00

问题


In Woocommerce I am trying to add a shipping fee if a cart item has a specific shipping class assigned to the related product. I would like this shipping fee to be multiplied by the cart item quantity…

I have this working when a product is added to the cart and the quantity is increased and the additional shipping fee is increased also. However if I add another product with the same shipping class and increase the quantity the additional fee does not increase.

This is my code:

// Add additional fees based on shipping class
function woocommerce_fee_based_on_shipping_class( $cart_object ) {

    global $woocommerce;

    // Setup an array of shipping classes which correspond to those created in Woocommerce
    $shippingclass_dry_ice_array = array( 'dry-ice-shipping' );
    $dry_ice_shipping_fee = 70;

    // then we loop through the cart, checking the shipping classes
    foreach ( $cart_object->cart_contents as $key => $value ) {
        $shipping_class = get_the_terms( $value['product_id'], 'product_shipping_class' );
        $quantity = $value['quantity'];

        if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, $shippingclass_dry_ice_array ) ) {
            $woocommerce->cart->add_fee( __('Dry Ice Shipping Fee', 'woocommerce'), $quantity * $dry_ice_shipping_fee ); // each of these adds the appropriate fee
        }
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'woocommerce_fee_based_on_shipping_class' ); // make it all happen when Woocommerce tallies up the fees

How can I make it work for additional cart items too?


回答1:


Your code is a bit outdated and there is some mistakes. To add a fee based on product shipping class and cart item quantity use the following:

// Add a fee based on shipping class and cart item quantity
add_action( 'woocommerce_cart_calculate_fees', 'shipping_class_and_item_quantity_fee', 10, 1 ); 
function shipping_class_and_item_quantity_fee( $cart ) {

    ## -------------- YOUR SETTINGS BELOW ------------ ##
    $shipping_class = 'dry-ice-shipping'; // Targeted Shipping class slug
    $base_fee_rate  = 70; // Base rate for the fee
    ## ----------------------------------------------- ##

    $total_quantity = 0; // Initializing

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ) {
        // Get the instance of the WC_Product Object
        $product = $cart_item['data'];

        // Check for product shipping class
        if( $product->get_shipping_class() == $shipping_class ) {
            $total_quantity += $cart_item['quantity']; // Add item quantity
        }
    }

    if ( $total_quantity > 0 ) {
        $fee_text   = __('Dry Ice Shipping Fee', 'woocommerce');
        $fee_amount = $base_fee_rate * $total_quantity; // Calculate fee amount

        // Add the fee
        $cart->add_fee( $fee_text, $fee_amount );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.



来源:https://stackoverflow.com/questions/54943448/add-a-woocommerce-fee-based-on-shipping-class-and-item-quantity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!