2

I need to change the price of my products in my store, with a 10% discount, if my customer is from some specific place, so, I wrote this code:

add_filter('woocommerce_price_html', 'my_price_edit');
function my_price_edit() {
     $product = new WC_Product( get_the_ID() );
     $price = $product->price;
     echo $price * 0.9;
}

Ok, it works! But when in the checkout the price are normals without the 10% discount!

Does have some hook for change the price of the products in the checkout area or some different code to change correctly in the both (in the product page and checkout)?

Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127

1 Answers1

0

New in Woocommerce too. Your question looks really similiar to this one. Adding Custom price with woocomerce product price in Cart & Checkout

I think you need to use the woocommerce_cart_item_subtotal hook to change the price in the cart directly (not exactly as a parameter)

I made a slightly modification to the code (changed price formula). I think that may help you.

    add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
    // Display the line total price
    add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );

    function calculate_discounted_price( $price, $values ) {
        // You have all your data on $values;
        $price = $price*.09;
        return $price;
    }

    // wc_price => format the price with your own currency
    function display_discounted_price( $values, $item ) {
        return wc_price( $item[ 'line_total' ] );
    }
Community
  • 1
  • 1
Syntax_Error
  • 760
  • 3
  • 10
  • 15