categories - Change woo status automatically

admin2025-01-07  6

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 7 days ago.

Improve this question

I need the change of order status automatically. I try it with the following code but the status change always to "vorbestellung". Please help me to correct my code.

/**
 * Change Order Status
 */
function action_woocommerce_thankyou( $order_id ) {
   // if ( ! $order_id ) return;

    // Get order object
    $order = wc_get_order( $order_id );

    // Specific categories: the term name/term_id/slug. Several could be added, separated by a comma e.g. 240=Langwieder,30855=BIO OÖ,31008 = vorbestellungen,
    // 31070 = VB, 31071=Schusterbauer Rind,31083=Gröberhof Lamm, 31084 = Gröb Manga, 31085 =Gröb Truth, 31127 = Südfrüchte
   
      $categories = array(30735,30855,31008,31071,31083,31084,31085,31127);
    
    // Flag
    $found = false;
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        // Product ID
        $product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();

        // Has term (product category)
        if ( has_term( $categories, 'product_cat', $product_id ) ) {
            $found = true;
            break;
        }
    }
    
    // True
    if ( $found ) {
        // Status Vorbestellung
        $order->update_status( 'vorbestellung' );
    }else{
        $order->update_status( 'bestellung-neu' );
        
    }
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );

Thank you Siegfried

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736257429a437.html

最新回复(0)