Search
 
SCRIPT & CODE EXAMPLE
 

PHP

order item add hook WooCommerce admin panel

add_action( 'woocommerce_order_before_calculate_totals', "custom_order_before_calculate_totals", 10, 3);
function custom_order_before_calculate_totals($and_taxes, $order ) {
    // The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
    // loop all products and calculate total deposit
    $total_deposit = 0;
    foreach( $order->get_items() as $item_id => $item ) {
        // get the WC_Product object
        $product = $item->get_product();

        // get the quantity
        $product_quantity = $item->get_quantity();

        // get the deposit amount
        $deposit = $product->get_attribute('deposit') * $product_quantity;

        // sum of deposits from all products
        $total_deposit += $deposit;
    }


    // update the Deposit fee if it exists
    $deposit_fee_exists = false;
    foreach( $order->get_fees() as $item_id => $item_fee ) {
        $fee_name = $item_fee->get_name();

        if ( $fee_name == 'Deposit' ) {
            $item_fee->set_tax_status('none'); // no tax on deposit
            $item_fee->set_total($total_deposit);
            $deposit_fee_exists = true;
            break;
        }
    }

    // if there isn't an existing deposit fee then add it
    if ( $total_deposit > 0 && !$deposit_fee_exists ) {
        // Get a new instance of the WC_Order_Item_Fee Object
        $item_fee = new WC_Order_Item_Fee();

        $item_fee->set_name( "Deposit" ); // Generic fee name
        $item_fee->set_amount( $total_deposit ); // Fee amount
        $item_fee->set_tax_status( 'none' ); // or 'none'
        $item_fee->set_total( $total_deposit ); // Fee amount

        // Add Fee item to the order
        $order->add_item( $item_fee );
    }
}
Comment

PREVIOUS NEXT
Code Example
Php :: str_word_count() 
Php :: php include file from file included before 
Php :: crrate model in laravel 
Php :: wordpress header.php 
Php :: carbon check sunday 
Php :: time dropdown in html 
Php :: tackel discount in javascript 
Php :: db($twoRandomPhotosOfSomePeoples); 
Java :: Cannot resolve class android.support.design.widget.CoordinatorLayout 
Java :: how to print hello world in java 
Java :: list java versions mac 
Java :: java sleep in code 
Java :: java read file to string 
Java :: string to date android 
Java :: spring application properties mysql jpa 
Java :: java reflection get field value 
Java :: full screen android java 
Java :: change visibility of textview andoird 
Java :: java remove List null element 
Java :: beans tag definition for spring frame work 
Java :: java how to find length of int 
Java :: Could not initialize class org.codehaus.groovy.vmplugin.VMPluginFactory 
Java :: jframe add label java 
Java :: how to set event priority in spigot 
Java :: android ancestral navigation 
Java :: How to add negative random numbers in java 
Java :: format localdate java 
Java :: bundletool aab to apk 
Java :: android hide textview 
Java :: java swing button on click 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =