skip to Main Content

I’m using custom order statusses on a few product categories on our webshop.

Now that we are live I see that all orders that are "Processing" are payed with Mollie (payment GW).

However the orders that have a custom order status, and where the customer also selected Mollie, the payment GW didn’t work. The customer just gets forwarded to a next page where they see the message "Order is submitted" without the payment GW beging triggered.

The code I used:

add_action( 'woocommerce_checkout_order_processed', 'custom_order_status_by_cat', 10, 3 );
function custom_order_status_by_cat( $order_id, $posted_data, $order ){
    $items = $order->get_items(); 
    foreach ( $items as $item ) {      
      $product_id = $item->get_product_id();  
      if ( has_term( 'met-gravure', 'product_cat', $product_id ) ) { //Categorie selecteren
         $order->update_status( 'ord-maatwerk' ); // Aangepaste status
         break;
      } else if ( has_term( 'moedermelk', 'product_cat', $product_id ) ) { //Categorie selecteren
         $order->update_status( 'ord-moedermelk' ); // Aangepaste status
         break;
      }
else if ( has_term( 'last-minute-gepersonaliseerd-juweel', 'product_cat', $product_id ) ) { //Categorie selecteren
         $order->update_status( 'ord-lastminute' ); // Aangepaste status
         break;
      }
    }
}

Find out what the code is to call the payment gateway but didn’t find it.

2

Answers


  1. Chosen as BEST ANSWER

    Thank you so much for your clear answer. Im starting to understand now (I think :-)).

    If im correct the only thing I need to do is change the 'payment_id' with my Mollie ID? Do I need to include the code "// For other payment gateways than Mollie" ? Because im not using any other payment gateway. I think it will look like this?

        // Utility function: Get the custom order status by product category
    function get_custom_order_status_by_cat( $order, $status = false ) {
        // Loop through order items
        foreach ( $order->get_items() as $item ) {      
            $product_id = $item->get_product_id(); // Get product ID
    
            if ( has_term( 'met-gravure', 'product_cat', $product_id ) ) {
                $status = 'ord-maatwerk'; // Aangepaste status
                break;
            } elseif ( has_term( 'moedermelk', 'product_cat', $product_id ) ) {
                $status = 'ord-moedermelk'; // Aangepaste status
                break;
            } elseif ( has_term( 'last-minute-gepersonaliseerd-juweel', 'product_cat', $product_id ) ) {
                $status = 'ord-lastminute'; // Aangepaste status
                break;
            }
        }
        return $status;
    }
    
    
    // For Mollie Payments
    add_filter( 'woocommerce_payment_complete_order_status', 'update_order_status_by_category_for_mollie', 10, 3 );
    function update_order_status_by_category_for_mollie(  $status, $order_id, $order  ){
        // HERE bellow, define Mollie payment method ID
        $targeted_payment_id = '*******';
    
        $custom_status = get_custom_order_status_by_cat( $order ); // Get custom order status
        
        // If a custom order status is found and if it's a Mollie payment
        if ( $custom_status && $order->get_payment_method() === $targeted_payment_id ) { 
            return $custom_status;
        }
        return $status;
    }
    

    Again thank you so much!


  2. You should not use woocommerce_checkout_order_processed hook that is triggered before the payment has been processed, for orders with Mollie payment.

    Instead, for orders with Mollie payment, you should use woocommerce_payment_complete_order_status filter hook, to set the custom order statuses.

    Also, to avoid an error in PHP, note that the "ELSE IF" statement doesn’t include a space between else and if. Instead, simply use elseif.

    Try the following, defining in the both last functions the correct Mollie payment ID:

    // Utility function: Get the custom order status by product category
    function get_custom_order_status_by_cat( $order, $status = false ) {
        // Loop through order items
        foreach ( $order->get_items() as $item ) {      
            $product_id = $item->get_product_id(); // Get product ID
    
            if ( has_term( 'met-gravure', 'product_cat', $product_id ) ) {
                $status = 'ord-maatwerk'; // Aangepaste status
                break;
            } elseif ( has_term( 'moedermelk', 'product_cat', $product_id ) ) {
                $status = 'ord-moedermelk'; // Aangepaste status
                break;
            } elseif ( has_term( 'last-minute-gepersonaliseerd-juweel', 'product_cat', $product_id ) ) {
                $status = 'ord-lastminute'; // Aangepaste status
                break;
            }
        }
        return $status;
    }
    
    // For other payment gateways than Mollie
    add_action( 'woocommerce_checkout_order_processed', 'update_order_status_by_category', 10, 3 );
    function update_order_status_by_category( $order_id, $posted_data, $order ){
        // HERE bellow, define Mollie payment method ID
        $targeted_payment_id = 'payment_id';
    
        $custom_status = get_custom_order_status_by_cat( $order ); // Get custom order status
        
        // If a custom order status is found and if it's NOT a Mollie payment
        if ( $custom_status && $order->get_payment_method() !== $targeted_payment_id ) { 
            $order->update_status( $custom_status );
        }
    }
    
    // For Mollie Payments
    add_filter( 'woocommerce_payment_complete_order_status', 'update_order_status_by_category_for_mollie', 10, 3 );
    function update_order_status_by_category_for_mollie(  $status, $order_id, $order  ){
        // HERE bellow, define Mollie payment method ID
        $targeted_payment_id = 'payment_id';
    
        $custom_status = get_custom_order_status_by_cat( $order ); // Get custom order status
        
        // If a custom order status is found and if it's a Mollie payment
        if ( $custom_status && $order->get_payment_method() === $targeted_payment_id ) { 
            return $custom_status;
        }
        return $status;
    }
    

    It should work, if you set in the code the correct payment ID slug for Mollie.

    Some related threads using woocommerce_payment_complete_order_status filter hook.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search