WooCommerce: Add product to cart with price override?

$replace_order = new WC_Cart();
$replace_order->empty_cart( true );
$replace_order->add_to_cart( "256", "1");

The above code add product 256 to the Cart 1 time. But the issue I'm having is that I want to be able to completely override the product price... as far as I can tell, the only thing I can do it apply a coupon to the Cart.

Is there a way to completely override the price to something totally custom?

Answers


Here is the code for overriding price of product in cart

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']->price = $custom_price;
    }
}

Hope it will be useful...


Need Your Help

Global variable in a module not accessible after the module being imported

rascal

I found that in the Rascal console, after I import a module which contains global variable definitions, these global variables are not accessible. Only global function names are accessible. For

Why such OpenAL code gives such Erros in Visual studio 2008?

c++ c visual-studio visual-studio-2008 openal

So in general my code is super simple - I am triing to write Microphone Echo programm and this is its start (as it seems to me - I am wary new to OpenAL)

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.