WooCommerce: Add product to cart with price override?

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

    I have tried all above code samples and latest woocommerce 3.0 is not support any of the above example. Use below code and working perfectly for me.

    add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
    
    function add_custom_price( $cart_object ) {
        $custom_price = 10; // This will be your custom price  
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            $cart_item['data']->set_price($custom_price);   
        }
    }
    

提交回复
热议问题