Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Last active November 24, 2023 07:36
Show Gist options
  • Save Acephalia/5457406f695ab591aaa00ba2118fa551 to your computer and use it in GitHub Desktop.
Save Acephalia/5457406f695ab591aaa00ba2118fa551 to your computer and use it in GitHub Desktop.
Woocommerce Dynamically adjust price for a specific product based on sales
// Dynamically adjust price for woocommerce product based on sales by u/acehaliax
add_filter('woocommerce_product_get_price', 'adjust_product_price', 10, 2);
function adjust_product_price($price, $product) {
$target_product_id = 827; // Target product ID
// Check if the current product matches the target product
if ($product->get_id() === $target_product_id) {
// Get the quantity sold for the product using get_total_sales()
$quantity_sold = $product->get_total_sales();
// Define the price tiers and corresponding quantity ranges
$price_tiers = array(
5 => 10,
10 => 15, // Add more tiers after this line if neccesary
'15+' => 20, // Price for 15 products and above
);
// Determine the appropriate price based on the quantity sold
foreach ($price_tiers as $range_end => $tier_price) {
if ($quantity_sold <= $range_end || $range_end === '15+') {
$price = $tier_price;
break;
}
}
}
return $price;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment