Answer the question
In order to leave comments, you need to log in
Changing price type based on Woocommerce cart total?
If the total amount of the basket is more than 2000r. then the items in the cart are considered at the wholesale price. It is added custom via functions.php.
I roughly imagine the logic of the algorithm, but I'm not very good at php. I will be grateful in advance
Answer the question
In order to leave comments, you need to log in
On the Internet, I found a code that changes the price of a product to a fixed one without any conditions.
And something like this came out:
// Adding a wholesale price for items
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 20, 1);
function add_custom_price( $cart ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Avoiding hook repetition (when using price calculations for example)
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Get order subtotal
$order_subtotal = WC()->cart->get_cart_subtotal();
if ( $order_subtotal >= 2000 ) {
foreach ( $cart->get_cart() as $item ) {
// Значение set_price нужно будет заменить на id кастомной цены, если всё заработает
$item['data']->set_price( 150.6 );
}
}
}
$order_subtotal
. $order_subtotal
through echo
, but I was constantly given that it is equal to 0 (zero or zero?). Not understanding what was happening, I fell a little desperate. But. BUT. After I noticed that it is add_action
writtenwoocommerce_before_calculate_totals
. Hahahah. Everything was so easy. It says before. This is BEFORE. That is, before calculating the amount. With light pressing with my fingers, I replaced this worthless "before" with the "after" I needed. And gods, it worked. I began to see the correct basket subtotal for me. get_cart_subtotal()
, then I get the amount as a string with a currency sign, and if we use it, get_subtotal()
we get the float type, which can be used for the if construct. // Adding a wholesale price for items
add_action( 'woocommerce_after_calculate_totals', 'add_custom_price', 20, 1);
function add_custom_price( $cart ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Avoiding hook repetition (when using price calculations for example)
if ( did_action( 'woocommerce_after_calculate_totals' ) >= 2 )
return;
if ( WC()->cart->get_subtotal() >= 2000 ) {
foreach ( $cart->get_cart() as $item ) {
$item['data']->set_price( 150.6 );
}
}
}
// Adding a wholesale price for items
add_action( 'woocommerce_after_calculate_totals', 'add_custom_price', 20, 1);
function add_custom_price( $cart ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Avoiding hook repetition (when using price calculations for example)
if ( did_action( 'woocommerce_after_calculate_totals' ) >= 2 )
return;
// Adding a custom price for items
if ( WC()->cart->get_subtotal() >= 2000 ) {
foreach ( $cart->get_cart() as $item ) {
$item['data']->set_price( 150.6 );
}
}
// Recount cart total
WC()->cart->calculate_totals();
// Wrong minimum amount protection
if ( WC()->cart->get_subtotal() < 2000 ) {
foreach ( $cart->get_cart() as $item ) {
$regular_price = $item['data']->get_regular_price();
$item['data']->set_price( $regular_price );
}
}
// Recount cart total
WC()->cart->calculate_totals();
}
$ca = WC()->cart->calculate_totals()
. In an attempt to check if the site hangs from such logic when reloading, I did not see a message stating that "An error has occurred. Try another time." (This is already very pleasing). I decided to try changing the quantity of goods. And it was here that I noticed that the basket recalculates the total amount at the new price set by me. Ooooooooooo. I crossed myself and said thank you to myself for being so cool. After that, I left only WC()->cart->calculate_totals()
that also worked properly.$regular_price = $item['data']->get_regular_price()
and also added a recalculation of the cart amount after. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question