mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
https://stackoverflow.com/questions/75942371/give-site-wide-discounts-for-current-active-woocommerce-subscriptions-subscriber
22 lines
1.1 KiB
Text
22 lines
1.1 KiB
Text
add_filter( 'woocommerce_product_get_price', 'apply_half_price_for_active_subscribers', 10, 2 );
|
|
add_filter( 'woocommerce_product_get_regular_price', 'apply_half_price_for_active_subscribers', 10, 2 );
|
|
function apply_half_price_for_active_subscribers( $price, $product ) {
|
|
if ( has_active_subscription() && WC()->cart->cart_contents_count > 0 ) {
|
|
// Get the total quantity of products in the cart for the current user
|
|
$user_id = get_current_user_id();
|
|
$product_count = 0;
|
|
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
|
|
if ( $cart_item['data']->get_id() != $product->get_id() && $cart_item['data']->is_purchasable() ) {
|
|
$product_user_id = $cart_item['data']->get_meta( '_customer_user', true );
|
|
if ( empty( $product_user_id ) || $product_user_id == $user_id ) {
|
|
$product_count += $cart_item['quantity'];
|
|
}
|
|
}
|
|
}
|
|
// Apply half price discount for additional products purchased
|
|
if ( $product_count > 0 ) {
|
|
$price = $price / 2;
|
|
}
|
|
}
|
|
return $price;
|
|
}
|