• Resolved lituoklis13

    (@lituoklis13)


    I have some old orders which are still processing and want to change their shipping method to the new one which I’m using now. Some processing orders uses another shipping method so in this case I should check not only the order status but shipping method ID as well. I try to do this but unfortunatelly no luck.

    Based on answer code there, this is my attempt with no luck:

    function change_shipping($order, $calculate_tax_for, $shipping_methods ) {
        // Here set your shipping method ID replacement
        $old_method_id ='multiparcels_lp_express_courier';
        $new_method_id ='dpd_home_delivery';
        
        // Get the the WC_Order Object from an order ID (optional)
        $order = wc_get_order( $order_id );
        
        // Array for tax calculations
        $calculate_tax_for = array(
            'country'  => $order->get_shipping_country(),
            'state'    => $order->get_shipping_state(), // (optional value)
            'postcode' => $order->get_shipping_postcode(), // (optional value)
            'city'     => $order->get_shipping_city(), // (optional value)
        );
        
        if ($order->data['status'] == 'processing' && $shipping_method->id == $old_method_id ) {
        
        $changed = false; // Initializing
        
        // Loop through order shipping items
        foreach( $order->get_items( 'shipping' ) as $item_id => $item ){
        
            // Retrieve the customer shipping zone
            $shipping_zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $item->get_instance_id() );
        
            // Get an array of available shipping methods for the current shipping zone
            $shipping_methods = $shipping_zone->get_shipping_methods();
        
            // Loop through available shipping methods
            foreach ( $shipping_methods as $instance_id => $shipping_method ) {
        
                // Targeting specific shipping method
                if( $shipping_method->is_enabled() && $shipping_method->id === $new_method_id ) {
        
                    // Set an existing shipping method for customer zone
                    $item->set_method_title( $shipping_method->get_title() );
                    $item->set_method_id( $shipping_method->get_rate_id() ); // set an existing Shipping method rate ID
                    $item->set_total( $shipping_method->cost );
        
                    $item->calculate_taxes( $calculate_tax_for );
                    $item->save();
        
                    $changed = true;
                    break; // stop the loop
                }
            }
        }
        
        if ( $changed ) {
            // Calculate totals and save
            $order->calculate_totals(); // the save() method is included
        }
        
        }
        
        }
    add_action ('change_shipping', true);

    I guess I should use a specific hook to add_action to this code run and somehow change the code to select not one but multiple orders ID’s. How I should do that?

    • This topic was modified 2 years, 6 months ago by lituoklis13.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Change order shipping method if order status is processing and has specific id’ is closed to new replies.