Home > Enterprise >  How to update product meta when the woocommerce order status changes
How to update product meta when the woocommerce order status changes

Time:02-16

Please help me with the code, I need the product to be updated when the order goes to the status completed... Here is my outline of the code, I have been suffering with it for 5 days...

//Update product meta when the woocommerce order status changes
add_action( 'woocommerce_payment_complete', 'update_product_meta', 20, 2 );
add_action( 'woocommerce_order_status_completed', 'update_product_meta', 20, 2 );
add_action( 'woocommerce_order_status_processing', 'update_product_meta', 20, 2 );

 function update_product_meta ($product, $order) {


 // get product
 $order = get_product( array(
    'post_type'        => 'product', // or ['product','product_variation'],
    'post_status'      => 'publish',
    'fields'           => 'ids',
    'meta_query'       => array( array(
        'key'     => '_sync_updated',
        'compare' => 'NOT EXISTS',
    ) )
) );


    // Get the WC_Product object
    $product = wc_get_product($product_id);

    // Mark product as updated
    $product->update_meta_data( '_sync_updated', true );

    $product->save();
}

CodePudding user response:

You can get all order products from $order obect. use $items = $order->get_items(); to get all items of current order.

for current order product update.

//Update product meta when the woocommerce order status changes
add_action( 'woocommerce_payment_complete', 'update_product_meta', 20, 2 );
add_action( 'woocommerce_order_status_completed', 'update_product_meta', 20, 2 );
add_action( 'woocommerce_order_status_processing', 'update_product_meta', 20, 2 );
function update_product_meta ( $order_id, $order ) {
    $items = $order->get_items(); 
    foreach ( $order->get_items() as $item ) {
        // Compatibility for woocommerce 3 
        $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $item['product_id'] : $item->get_product_id();
        $product = wc_get_product( $product_id );
        $product->update_meta_data( '_sync_updated', 'dsdfdff' );
        $product->save();
    }
}

for all product updates.

//Update product meta when the woocommerce order status changes
add_action( 'woocommerce_payment_complete', 'update_product_meta', 20, 2 );
add_action( 'woocommerce_order_status_completed', 'update_product_meta', 20, 2 );
add_action( 'woocommerce_order_status_processing', 'update_product_meta', 20, 2 );
function update_product_meta ( $order_id, $order ) {
    
    // get product
    $products = get_product( array(
        'post_type'        => 'product', // or ['product','product_variation'],
        'posts_per_page'   => -1,
        'post_status'      => 'publish',
        'fields'           => 'ids',
        'meta_query'       => array( array(
            'key'     => '_sync_updated',
            'compare' => 'NOT EXISTS',
        ) )
    ) );

    foreach ( $products as $product_id ) {
        $product = wc_get_product( $product_id );
        $product->update_meta_data( '_sync_updated', 'dsdfdff' );
        $product->save();
    }

}
  • Related