mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-30 11:42:22 +08:00
39 lines
1.2 KiB
Text
39 lines
1.2 KiB
Text
add_filter( 'woocommerce_get_price_html', 'wc_alter_price_display', 9999, 2 );
|
|
|
|
function wc_alter_price_display( $price_html, $product ) {
|
|
|
|
// ONLY ON FRONTEND
|
|
if ( is_admin() ) return $price_html;
|
|
|
|
// ONLY IF PRICE NOT NULL
|
|
if ( '' === $product->get_price() ) return $price_html;
|
|
|
|
// IF CUSTOMER LOGGED IN, APPLY 20% DISCOUNT
|
|
if ( wc_current_user_has_role( 'customer' ) ) {
|
|
$orig_price = wc_get_price_to_display( $product );
|
|
$price_html = wc_price( $orig_price * 0.80 );
|
|
}
|
|
|
|
return $price_html;
|
|
|
|
}
|
|
|
|
add_action( 'woocommerce_before_calculate_totals', 'wc_alter_price_cart', 9999 );
|
|
|
|
function wc_alter_price_cart( $cart ) {
|
|
|
|
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
|
|
|
|
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
|
|
|
|
// IF CUSTOMER NOT LOGGED IN, DONT APPLY DISCOUNT
|
|
if ( ! wc_current_user_has_role( 'customer' ) ) return;
|
|
|
|
// LOOP THROUGH CART ITEMS & APPLY 20% DISCOUNT
|
|
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
|
|
$product = $cart_item['data'];
|
|
$price = $product->get_price();
|
|
$cart_item['data']->set_price( $price * 0.80 );
|
|
}
|
|
|
|
}
|