Last active
November 18, 2023 19:11
-
-
Save kimcoleman/8b4e6f9ec6cc0b969676588d5b2e7371 to your computer and use it in GitHub Desktop.
Sitewide Sales compatibility with RightPress Product Prices - Live Update.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Sitewide Sales compatibility with RightPress Product Prices - Live Update. | |
*/ | |
function my_sws_apply_sitewide_sales_discount_to_live_price($price, $product) { | |
// Return early if Sitewide Sales is not active. | |
if ( ! defined ( 'SWSALES_VERSION' ) ) { | |
return $price; | |
} | |
// Check if Sitewide Sales has a sale is going on. | |
$active_sitewide_sale = Sitewide_Sales\classes\SWSales_Sitewide_Sale::get_active_sitewide_sale(); | |
// Make sure there is a sale and it's for WC. | |
if ( null === $active_sitewide_sale || 'wc' !== $active_sitewide_sale->get_sale_type() ) { | |
return $price; | |
} | |
// We have a WooCommerce sale. Get the coupon ID associated with the sale | |
$coupon_id = $active_sitewide_sale->get_meta_value( 'swsales_wc_coupon_id', null ); | |
// Our returned price. | |
$return_price = $price; | |
if ( $coupon_id ) { | |
$coupon = new WC_Coupon( $coupon_id ); | |
if ( $coupon->is_valid() && $coupon->is_valid_for_product( $product ) ) { | |
// Calculate the discounted price. | |
// Use this line to base the discount off of the product's regular price. | |
$discount_amount = $coupon->get_discount_amount( $product->get_price('edit') ); | |
// Use this line to base the discount off of the product current live update price. | |
// $return_price = $coupon->get_discount_amount( $price ); | |
$return_price = $price - $discount_amount; | |
} | |
} | |
// Return the price. | |
return $return_price; | |
} | |
add_filter( 'rightpress_product_price_live_update_price', 'my_sws_apply_sitewide_sales_discount_to_live_price', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment