V
V
vanechka_ivan2021-03-31 12:02:03
PHP
vanechka_ivan, 2021-03-31 12:02:03

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

1 answer(s)
V
vanechka_ivan, 2021-04-01
@vanechka_ivan

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 );
        }
    }
}

Here, to test roboticity, I simply change the price of goods in the basket to a fixed one.
But when saving functions.php with this code, the site throws an error when trying to add a product to the cart. This happens if I compare the subtotal of the basket with the amount I need (assuming 1 == 1, everything works). Also, I'm not sure if I'm getting the value correctly $order_subtotal.
Even if the code works as it should, will a certain product be assigned its specific custom price.
UPD:
I started to check the value $order_subtotalthrough 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_actionwrittenwoocommerce_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.
After that, I realized that if I get the value I need through using 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.
Now the code looks like this:
// 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 );
        }
    }
}

PS While editing the text, I noticed that when the subtotal becomes more than necessary (that is, the condition is met) and the product acquires the right price, the final result is calculated at the old price. I will think further
PS 2 I understood why this is happening. I set a condition under which the price of the product is set after the basket amount has been calculated, and therefore it calculates according to the standard price.
UPS 2
Yyyy. It works as it should. Because that's exactly what I wanted.
The code:
// 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();
}

And now in order. Firstly, I did not know how to recalculate the total cost of the basket with changed prices. I had an idea that I stuck to. I don’t even want to think about how I would do it, because my head already hurts from this. But the point is exactly how I started doing it. I have added a variable $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.
Realizing that the price change only works in one direction, I added a second if construct with only the opposite condition. Replaced fixed price with
$regular_price = $item['data']->get_regular_price()
and also added a recalculation of the cart amount after.
This is my personal little victory over which I am very proud, because up to this point I have not dealt with php from the word at all. It remains only to add the wholesale price, specify it in my small code and that's it. This is the end of my suffering.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question