最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Woocommerce hook run after an Order been created through REST API

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论