Created
September 2, 2021 17:05
-
-
Save richerimage/cf551ddc6bb202a3fc5e947497b7bd5b to your computer and use it in GitHub Desktop.
Woo Woes
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
// Utility function to change the prices with a multiplier (number) | |
function get_price_multiplier() { | |
$prod_id = get_the_ID(); | |
$brand = 'acme'; | |
if ( (is_user_logged_in()) && ($brand == 'acme') ) { | |
return 0.8; // 20% off | |
} else { | |
return 1; | |
} | |
} | |
// Simple, grouped and external products | |
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 ); | |
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 ); | |
// Variations | |
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 ); | |
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 ); | |
function custom_price( $price, $product ) { | |
return (float) $price * get_price_multiplier(); | |
} | |
// Variable (price range) | |
add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3 ); | |
add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3 ); | |
function custom_variable_price( $price, $variation, $product ) { | |
// Delete product cached price (if needed) | |
wc_delete_product_transients($variation->get_id()); | |
return (float) $price * get_price_multiplier(); | |
} | |
// Handling price caching (see explanations at the end) | |
add_filter( 'woocommerce_get_variation_prices_hash', 'add_price_multiplier_to_variation_prices_hash', 99, 3 ); | |
function add_price_multiplier_to_variation_prices_hash( $price_hash, $product, $for_display ) { | |
$price_hash[] = get_price_multiplier(); | |
return $price_hash; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment