• Resolved javasworks

    (@javasworks)


    So, i trigger update_checkout via javascript. Then, i have function to be called in woocommerce_review_order_before_shipping hook. The function is to add/remove free_shipping shipping method (it’s based on Cash on Delivery availability).

    There are two radio buttons of payment method:

    – Direct Transfer
    – Cash on Delivery

    The script already works. So when i choose COD (Cash on Delivery), the free shipping method is selected automatically.

    But, the problem is the total price did not changed, this indicates that the cost is still available. Well, it changed, but it is late. I said late because the total price is canged when i change the payment method by choosing Direct Bank Transfer radio button.

    The question is,

    1. why the total price doesn’t update realtime when the payment method is changed? Ok, it changed but in the next change event.
    2. what is WooCommerce API to update the total price OR to update the cost? Where in the hook should i place this?

    ?? Thank you very much for answering

Viewing 3 replies - 1 through 3 (of 3 total)
  • jessepearson

    (@jessepearson)

    Automattic Happiness Engineer

    Hey @javasworks,

    Here’s the solution I provided before with some edits:

    function before_shipping_html_filter() {
    
    	// if it's not cod, leave
    	if ( ! isset( $_POST['payment_method'] ) || 'cod' !== $_POST['payment_method'] ) {
    		return;
    	}
    
    	// Set the shipping method to local pickup
    	WC()->session->set( 'chosen_shipping_methods', array( 'local_pickup' ) );
    
    	// get the packages
    	$packages = WC()->shipping->get_packages();
    
    	// go through each
    	foreach ( $packages as $key => $package ) {
    
    		// if flat rate shipping is set, remove it
    		if ( isset( $package['rates']['flat_rate'] ) ) {
    			unset( $packages[ $key ]['rates']['flat_rate'] );
    		}
    	}
    
    	// recalculate totals
    	WC()->cart->calculate_totals();
    
    	// update the packages in the object
    	WC()->shipping->packages = $packages;
    
    }
    add_action( 'woocommerce_review_order_before_shipping', 'before_shipping_html_filter', 99 );

    The changes are setting the shipping method to local pickup in the user’s session, and recalculating the totals before updating the packages. So if COD is chosen, the shipping method is set to Local Pickup, then Flat Rate is removed as an option, the Total is updated, and the packages are modified.

    This is very basic, but can be used to create more extensive code with more shipping and/or payment options.

    Thread Starter javasworks

    (@javasworks)

    ?? Wow thanks again @jesse Pearson for answering again. Filter them, recalculate, then update the package

    jessepearson

    (@jessepearson)

    Automattic Happiness Engineer

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Woocommerce checkout: Update cost OR update total price via “update_checkout” aj’ is closed to new replies.