admin管理员组

文章数量:1320661

I'm building a plugin that needs to update data after order creation! I have succeeded in doing that on the website but not in the REST API

What I have done is using woocommerce_thankyou that working fine after checkout through the website but not the REST API too!!

So how to do the same function below after Creating an Order through WooCommerce REST API!!

add_action('woocommerce_thankyou', 'edit_stock_metadata', 10, 1);
function edit_stock_metadata($order_id)
{

    if (!$order_id)
        return;

    // Getting an instance of the order object
    $order = wc_get_order($order_id);
    
    // iterating through each order items (getting product ID and the product object) 
    // (work for simple and variable products)
    foreach ($order->get_items() as $item_id => $item) {

        if ($item['variation_id'] > 0) {
            $product_id = $item['variation_id']; // variable product
            $variation = new WC_Product_Variation($product_id);
            $stock_qty = intval($variation->get_meta('_stock_multiplier'));
            $item_quantity  = $item->get_quantity(); // Get the item quantity
            $variation->update_meta_data('_stock_multiplier', ($stock_qty - $item_quantity));
            $variation->save();
        }
    }
}

本文标签: phpWoocommerce hook run after an Order been created through REST API