3

I am currently developing a webshop where i needed to do a separate price function. So far, with the help of woocommerce hooks, i had managed to manipulate the price in both the shopping cart and the checkout, this works without any problems at all. Hooks i have used woocommerce_cart_item_price, woocommerce_cart_item_subtotals, woocommerce_cart_subtotal and woocommerce_cart_total.

Now we come to my problem that i need to solve in the very near future. The price from my custom function is not included in the woocommerce order. So, is there a hook to manipulate the product prices in the order before the woocommerce creates the order?

I have looked at https://docs.woocommerce.com/wc-apidocs/hook-docs.html but with no success.

Where does Woocommerce get the price from when it creates the order? The _price meta field, woocommerce_get_price hook, the cart or something else. I would be very grateful if someone could explain this to me. I find that woocommerce is not very consistent with where it's getting the price from.

Please ask questions if you don't understand my problem or my relative poor English. Thanks in advance.

Joel
  • 33
  • 1
  • 4

1 Answers1

1

I've use the woocommerce_get_price hook, when you change it, that changed price will be used for the cart to calculate the total (price * qty).

After order is placed, WooCommerce calculates the product based price on the total and qty, if you change one of the 2 values (total or qty) it will change the product price.

In other words, the price is dynamic after order has been created.

Edit:

Added method to change price

function change_price( $cart ) {

    //  Exit function if price is changed at backend
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    foreach ( $cart->get_cart() as $key => $item ) {
        $item['data']->set_price( $custom_price );
    }
 }
 add_action( 'woocommerce_before_calculate_totals', 'change_price', 10, 1);
Skatox
  • 4,237
  • 12
  • 42
  • 47
  • So first, thank you for taking som time to think on my problem. – Joel Jun 11 '17 at 14:29
  • I cannot use `woocommerce_get_price`, and the reason is that my custom price function needs cart related data to calculate the final price. In other words I have parameters in the cart that have to be passed trough some hook to my function, so I can count on them as well. – Joel Jun 11 '17 at 14:42
  • I develop a profile shop where customers can upload their logo and choose 1 color, 2 color 3 color... and so on. I store the customer's choice in the cart together with the product. Then I hook up my price function to these hooks `woocommerce_cart_item_price`, `woocommerce_cart_item_subtotals`, `woocommerce_cart_subtotal` and `woocommerce_cart_total`. Also note that via these hooks I get access to the shopping cart data. So how do I manage to manipulate the final price in the Woocommerce orders? – Joel Jun 11 '17 at 14:42
  • I've updated the answer to explain you how you can change price, it works on WC 3.0, not before – Skatox Jun 12 '17 at 03:04
  • 1
    Thank you so much for your help! it was the ultimate solution for my case. – Joel Jun 12 '17 at 13:52
  • 1
    Thank you. This answer - saved tons of time! – publikz.com Feb 04 '18 at 20:36
  • upvot the answer ;) – Skatox Feb 05 '18 at 22:02
  • please check this question also https://stackoverflow.com/questions/49505056/woocommerce-create-new-discount-functionality – Manik Mar 27 '18 at 06:37