WooCommerce: Add product to cart with price override?

后端 未结 10 1447
轮回少年
轮回少年 2020-11-27 13:53
$replace_order = new WC_Cart();
$replace_order->empty_cart( true );
$replace_order->add_to_cart( \"256\", \"1\");

The above code add product

10条回答
  •  有刺的猬
    2020-11-27 14:09

    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_object->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

提交回复
热议问题