Skip to content

Instantly share code, notes, and snippets.

@goranefbl
Created January 24, 2025 19:43
Show Gist options
  • Save goranefbl/ac127c7c21b012830bf32b792fcf9160 to your computer and use it in GitHub Desktop.
Save goranefbl/ac127c7c21b012830bf32b792fcf9160 to your computer and use it in GitHub Desktop.
different amount per product
<?php
add_filter('gens_raf_coupon_amount', 'modify_raf_coupon_amount', 10, 2);
function modify_raf_coupon_amount($amount, $order_id) {
// Get the order object
$order = wc_get_order($order_id);
if (!$order) {
return $amount; // Return default amount if order not found
}
// Define the product ID you want to check for
$target_product_id = 123; // Replace with your actual product ID
// Flag to check if the target product is in the order
$target_product_found = false;
// Loop through order items
foreach ($order->get_items() as $item) {
$product_id = $item->get_product_id();
if ($product_id == $target_product_id) {
$target_product_found = true;
break;
}
}
// Modify the coupon amount if the target product is found
if ($target_product_found) {
// Set your custom amount here
$new_amount = 20; // For example, set to 20
return $new_amount;
}
// Return the default amount if the target product is not in the order
return $amount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment