Deposit based on a percentage of total cart amount

后端 未结 1 1305
长发绾君心
长发绾君心 2021-01-26 21:29

I\'ve taken this code from another post and basically from my understanding, this code is trying to force the cart price to change to a fixed amount of $40 and charge it as a bo

1条回答
  •  太阳男子
    2021-01-26 22:10

    Your code is not really going to do what you expect, as you are adding a fee of o.2 to total cart amount. So if the cart amount is 100, the grand total is going to be 100.2

    What you want to do instead, is to remove 80% of total cart amount to get a cart amount of 20%. This is possible using a negative fee of 80% from total cart amount. If you don't need to set targeted products IDs as in your original code, use the 2nd function below.

    Here is the first function that will remove 80% of total cart amount when a cart item match with the targeted product IDs set in the function:

    add_action( 'woocommerce_cart_calculate_fees', 'booking_deposit_calculation' );
    function booking_deposit_calculation( $cart_object ) {
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        ## Set HERE your targeted products IDs
        $target_product_ids = array( 2434 );
    
        ## Set HERE your negative percentage (to remove an amount from cart total)
        $percent = -.80; // 80% off (negative)
    
        $matching = false;
    
        // Iterating through each cart items
        foreach ( $cart_object->get_cart() as $item_values )
        {
            if( in_array( $item_values['product_id'], $target_product_ids ) )
            { // If a cart item match with a targeted product ID
    
                // Get cart subtotal excluding taxes
                $cart_subtotal = $cart_object->subtotal_ex_tax;
                // or for subtotal including taxes use instead:
                // $cart_subtotal = $cart_object->subtotal;
    
                ## ## CALCULATION ## ##
                $calculated_amount = $cart_subtotal * $percent;
    
                // Adding a negative fee to cart amount (Including taxes)
                $cart_object->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, true );
    
                break; // We stop the loop
            }
        }
    }
    

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


    But may be you don't need to have some targeted products as in the original code.

    If it's the case this simplify the code:

    add_action( 'woocommerce_cart_calculate_fees', 'booking_deposit_calculation' );
    function booking_deposit_calculation( $cart_object ) {
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        ## Set HERE your negative percentage (to remove an amount from cart total)
        $percent = -.80; // 80% off (negative)
    
        // Get cart subtotal excluding taxes
        $cart_subtotal = $cart_object->subtotal_ex_tax;
        // or for subtotal including taxes use instead:
        // $cart_subtotal = $cart_object->subtotal;
    
        ## ## CALCULATION ## ##
        $calculated_amount = $cart_subtotal * $percent;
    
        // Adding a negative fee to cart amount (Including taxes)
        $cart_object->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, true );
    
    }
    

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

    Both functions are tested and works

    0 讨论(0)
提交回复
热议问题