您需要将此 url 变量设置为 WC session 以避免在 Url 更改时丢失它:
// Early enable guest customer WC_Session
add_action( 'init', 'wc_session_enabler' );
function wc_session_enabler() {
if ( is_user_logged_in() || is_admin() )
return;
if ( isset(WC()->session) && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
}
// Get "sale" url variable and set custom WC session variable
add_action( 'init', 'enable_sale_to_wc_session' );
function enable_sale_to_wc_session() {
if ( isset($_GET['sale']) ) {
WC()->session->set('has_sale', '1');
}
}
// Enable sale price if it's set on products when 'sale' is as an URL variable
add_filter( 'woocommerce_product_get_price', 'enabling_sale_price', 1000, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'enabling_sale_price', 1000, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'enabling_sale_price', 1000, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'enabling_sale_price', 1000, 2 );
function enabling_sale_price( $price, $product ) {
if ( isset($_GET['sale']) || WC()->session->get('has_sale') ) {
return $price;
} else {
return $product->get_regular_price();
}
}
// Remove custom WC session variable on thankyou page
add_action( 'woocommerce_thankyou', 'remove_enable_sale_from_wc_session' );
function remove_enable_sale_from_wc_session() {
if ( WC()->session->get('has_sale') ) {
WC()->session->__unset('has_sale');
}
}
代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。