This code alters the price per product once a specific number of stock have been purchased or inventory level reached in WooCommerce.
The code includes variables you can use to set :
- The specific product id
- Sales or stock inventory threshold
- New regular price
This solution works based on total sales of the product once the order status has completed.
add_action('woocommerce_order_status_completed', 'update_product_price_based_on_sales');
function update_product_price_based_on_sales( $order_id ) {
$product_id = 888;
$sales_threshold = 100;
$new_price = 50;
$product = wc_get_product($product_id);
$total_sales = (int) get_post_meta( $product_id, 'total_sales', true );
if ( $total_sales >= $sales_threshold ) {
$product->set_regular_price($new_price);
$product->save();
}
}
You can also change the price based on inventory levels rather than sales using this code :
add_action('woocommerce_product_set_stock', 'update_product_price_based_on_stock');
function update_product_price_based_on_stock($product) {
$product_id = 888;
$stock_threshold = 100;
$new_price = 50;
if ($product->get_id() === $product_id ) {
$current_stock = $product->get_stock_quantity();
if ( $current_stock <= $stock_threshold ) {
$product->set_regular_price($new_price);
$product->save();
}
}
}
All custom function should be added to a child theme functions file or custom code snippets plugin.






Leave a Reply