woocommerce change price while add to cart

后端 未结 4 2138
时光取名叫无心
时光取名叫无心 2021-02-10 05:13

i have a one product 1€ and use e GET parameter to change the price at runtime:

http://url/warenkorb/?add-to-cart=1539&price=18.45

This change not the price

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-10 05:57

    After release of woocommerce version 3.0.0 product price is update on add to cart using set_price($price) function. The example is given as below :

    add_action( 'woocommerce_before_calculate_totals', 'mj_custom_price' );
    
    function mj_custom_price( $cart_object ) {
       $woo_ver = WC()->version; 
       $custom_price = 10;
       foreach ( $cart->cart_contents as $key => $value )
       {
           if($woo_ver < "3.0.0" && $woo_ver < "2.7.0")
           {
               $value['data']->price = $custom_price;
           }
           else
           {
               $value['data']->set_price($custom_price);
           }
       }            
    }
    

    Many Thanks

提交回复
热议问题