WooCommerce: Add product to cart with price override?

后端 未结 10 1600
轮回少年
轮回少年 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:23

    For eveeryone that got here from Google. The above is now deprecated as i found out updating to WooCommerce 3.0.1.

    Instead of the above you now need to use set_price instead of price

    Here is an example:

    add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
    function add_custom_price( $cart_object ) {
        $custom_price = 10; // This will be your custome price  
        foreach ( $cart_object->cart_contents as $key => $value ) {
            $value['data']->set_price = $custom_price;
        }
    }
    

    I hope this helps people in the future :)

提交回复
热议问题