Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AnanthFlycart/2d21228fb2d9a55dab05fa0317dcb43d to your computer and use it in GitHub Desktop.
Save AnanthFlycart/2d21228fb2d9a55dab05fa0317dcb43d to your computer and use it in GitHub Desktop.
UpsellWP - Apply discount via get_price filter event
add_action('woocommerce_before_calculate_totals', function ($cart) {
if (!method_exists('\CUW\App\Helpers\Discount', 'getPrice')) {
return;
}
foreach ($cart->get_cart() as $cart_item) {
if (isset($cart_item['cuw_offer']) && isset($cart_item['data']) && $offer = $cart_item['cuw_offer']) {
if (!empty($offer['discount']) && isset($offer['discount']['type']) && $offer['discount']['type'] != 'no_discount') {
$offer_price = \CUW\App\Helpers\Discount::getPrice($cart_item['data'], $offer['discount']);
if (is_object($cart_item['data'])) {
$cart_item['data']->cuw_price = $offer_price;
}
}
} elseif (isset($cart_item['cuw_product']) && $data = $cart_item['cuw_product']) {
if (!empty($data['discount']) && isset($data['discount']['type']) && $data['discount']['type'] != 'no_discount') {
if (!$data['discount']['is_bundle'] || $data['discount']['type'] != 'fixed_price') {
$discount_price = \CUW\App\Helpers\Discount::getPrice($cart_item['data'], $data['discount']);
if (is_object($cart_item['data'])) {
$cart_item['data']->cuw_price = $discount_price;
}
}
}
}
}
}, 100000);
add_filter('woocommerce_product_get_price', function ($price, $product) {
if (is_object($product) && isset($product->cuw_price)) {
return $product->cuw_price;
}
return $price;
}, 100000, 2);
@AnanthFlycart
Copy link
Author

It helps to resolve upsell product discount not applied correctly due to third-party plugin conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment