问题描述:

I'm in a unique situation with my Woocommerce site.

I need to add a packaging fee, which will be somewhat similar to a handling fee.

Unfortunately, it's not as simple as adding a $5.00 handling fee to every order.

Since I sell wood products based on their measurements (width x height), the packaging fees are based on the total square footage for items. And they can differ depending on the category of the items, as well.

I've done a ton of research and I can't find a plugin to handle this situation.

To add to the complexity, there's a whole table that would need to be created. For example, if items from one category have a total square footage between 1-10, then the packaging fee will be $10. If total sq ftg is between 11-20, then it'll be $20.

How can I do to achieve that?

Thanks

网友答案:

This is possible and easy with the add_fee() method in the woocommerce_cart_calculate_fees hook. Here is a simple usage example for simple products, with 2 categories and based on the product dimensions measurement calculations for each item of the cart. Its also possible for other product types.

Here is the example code (That you will need to customize for your own case):

add_action( 'woocommerce_cart_calculate_fees','custom_applied_fee');
function custom_applied_fee() {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Set HERE your categories (can be an ID, a slug or the name… or an array of this)
    $category1 = 'plain';
    $category2 = 'plywood';

    // variables initialisation
    $fee = 0;
    $coef = 1;

    // Iterating through each cart item
    foreach(WC()->cart->get_cart() as $cart_item){
        // Get the product object
        $product = new WC_Product( $cart_item['product_id'] );
        // Get the dimentions of the product
        $height = $product->get_height();
        $width = $product->get_width();
        // $length = $product->get_length( );

        // Initialising variables (in the loop)
        $cat1 = false; $cat2 = false;

        // Detecting the product category and defining the category coeficient to change price (for example)
        // Set here for each category the modification calculation rules…
        if( has_term( $category1, 'product_cat', $cart_item['product_id']) )
            $coef = 1.15;
        if( has_term( $category2, 'product_cat', $cart_item['product_id']) )
            $coef = 1.3;

        // ## CALCULATIONS ## (Make here your conditional calculations)
        $dimention = $height * $with;
        if($dimention <= 10){
            $fee += 10 * $coef;
        } elseif($dimention > 10 && $dimention <= 20){
            $fee += 20 * $coef;
        } elseif($dimention > 20){
            $fee += 30 * $coef;
        }
    }

    // Set here the displayed fee text
    $fee_text = __( 'Packaging fee', 'woocommerce' );

    // Adding the fee
    if ( $fee != 0 )
        WC()->cart->add_fee( $fee_text, $fee, false );
        // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)

}

You will have to set your own categories with the related changes calculations for each category. You will get a non detailed general output fee calculated taking into account each item in the cart.

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.

The code is tested and fully functional.

Related Answers:

  • Discount for Certain Category Based on Total Number of Products
  • Cart discount for a product category based on quantity calculations
相关阅读:
Top