Skip to content

Commit

Permalink
Merge pull request #50 from dparker1005/wc_discount_on_sale_page
Browse files Browse the repository at this point in the history
Fixed WC sale landing page not always showing discounts if applying discount code automatically
  • Loading branch information
ideadude authored Sep 22, 2020
2 parents dc006b3 + e18d791 commit 2aded63
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions modules/class-swsales-module-wc.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,26 @@ public static function strike_prices( $price, $product ) {
if ( null === $active_sitewide_sale || 'wc' !== $active_sitewide_sale->get_sale_type() || is_admin() ) {
return $price;
}

$coupon_id = $active_sitewide_sale->get_meta_value( 'swsales_wc_coupon_id', null );
if ( null !== $coupon_id && WC()->cart->has_discount( wc_get_coupon_code_by_id( $coupon_id ) ) ) {
if ( null === $coupon_id ) {
return $price;
}
// Check if we are on the landing page
$landing_page_post_id = intval( $active_sitewide_sale->get_landing_page_post_id() );
$on_landing_page = false;
if (
( ! empty( $_SERVER['REQUEST_URI'] ) && intval( url_to_postid( $_SERVER['REQUEST_URI'] ) ) === $landing_page_post_id ) ||
( ! empty( $_SERVER['HTTP_REFERER'] ) && intval( url_to_postid( $_SERVER['HTTP_REFERER'] ) ) === $landing_page_post_id )
) {
$on_landing_page = true;
}
$should_apply_discount_on_landing = ( 'none' !== $active_sitewide_sale->get_automatic_discount() );

// If discount code is already applied or we are on the landing page and should apply discount...
if (
( ! empty( WC()->cart ) && WC()->cart->has_discount( wc_get_coupon_code_by_id( $coupon_id ) ) ) ||
( $on_landing_page && $should_apply_discount_on_landing )
) {
$coupon = new \WC_Coupon( wc_get_coupon_code_by_id( $coupon_id ) );
if ( $coupon->is_valid() && $coupon->is_valid_for_product( $product ) ) {
// Get pricing for simple products.
Expand Down

0 comments on commit 2aded63

Please sign in to comment.