0

I'm having some issues when a customer select a certain delivery option and a certain payment. At the moment we are using bacs and local payment gateways methods to collect the money, and we have local pick up and express delivery options. also, we have custom statuses for woocommerce for each delivery option. The problem occurs when someone asks for express delivery, and he wants to pay with bacs. we tried to assign custom status for each combination but the code is not working, due its selecting the same custom status to every option in the code.

add_action( 'woocommerce_thankyou', 'express_shipping_update_order_status', 10, 1 );
function express_shipping_update_order_status( $order_id ) {
    if ( ! $order_id ) return;
    
    $search_rm = 'Despacho Express Todo Santiago (Excluye Padre Hurtado. Recibe al siguiente día hábil)'; 
    $search_estoril = 'Retiro en Tienda Estoril';
    $search_vina = 'Retiro en Tienda Reñaca';
    
    $order = wc_get_order( $order_id );
    $payment_method=$order->get_payment_method();

    foreach($order->get_shipping_methods() as $shipping_item ){
    if( strpos( $shipping_item->get_method_title(), $search_rm ) !== false && $payment_method == "bacs"){
            $order->update_status('check-payment');
            break;
        } else {
            $order->update_status('express-rm');
            break;
        }

if( strpos( $shipping_item->get_method_title(), $search_estoril ) !== false && $payment_method == "bacs"){
            $order->update_status('check-payment');
            break;
        } else {
            $order->update_status('retiro-rm');
            break;
        }

        if( strpos( $shipping_item->get_method_title(), $search_vina ) !== false && $payment_method == "bacs"){
            $order->update_status('check-payment');
            break;
        } else {
            $order->update_status('retiro-vina');
            break;
        }
    }
}

this is the result:

express-rm

is there any way to fix this? thanks!

  • You're using `foreach($order->get_shipping_methods()`. How many different shipping methods can an order contain? – 7uc1f3r Aug 26 '21 at 19:05
  • we have 3 different shipping methods, and 2 local pick ups( for different stores) but they are all in the shipping method list. – EzzeOnursito Aug 26 '21 at 19:17

1 Answers1

1

Because you use custom shipping methods and custom order statuses, it is difficult to give an appropriate answer.

However, if you run the following code and apply the debug information in the if condition, you should get the desired result.

A hint. Build your code step by step and test it in the meantime, see debugging in WooCommerce versus multiple if and else conditions without you really being able to determine where things are going wrong

Explanation via comment tags added in the code:

function action_woocommerce_thankyou( $order_id ) {
    // Get $order object
    $order = wc_get_order( $order_id );

    // Get payment method
    $payment_method = $order->get_payment_method();

    // Get shipping method
    $shipping_method = $order->get_shipping_method();
    
    // DEBUGGING PURPOSES. Delete after testing
    echo 'DEBUG: Shipping method = ' . $shipping_method;

    // Compare payment method
    if ( $payment_method == 'bacs' ) {
        // Compare shipping method
        if ( $shipping_method == 'my_shipping_method_copy_pasted_from_the_debug_information' ) {
            $order->update_status( 'my-custom-order-status' );
        } else {
            // etc..
        }
    }
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50