PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 3, 2022

[FIXED] How to update product meta when the woocommerce order status changes

 March 03, 2022     woocommerce, wordpress     No comments   

Issue

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();
}

Solution

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();
    }

}


Answered By - Bhautik
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing