i'm using this method in combination with an ajax call to set a product from a configurator to my cart:

$woocommerce->cart->add_to_cart($p_id, $p_quantity, $p_var_id, $p_var_name);

How can i use this method to set a calculated custom price? Is it possible?



  • I tried last week but I could not. I will follow this question.
    – sakarya
    Nov 2, 2017 at 22:23

1 Answer 1


Currently there is no straight way to add custom price to product which is added through function $woocommerce->cart->add_to_cart (Documentation) but we have a bypass way that i explain in code below

global $woocommerce;
$custom_price = 1000;
// Cart item data to send & save in order
$cart_item_data = array('custom_price' => $custom_price);   
// woocommerce function to add product into cart check its documentation also 
// what we need here is only $product_id & $cart_item_data other can be default.
$woocommerce->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation, $cart_item_data );
// Calculate totals
// Save cart to session
// Maybe set cart cookies

in your functions file you can place below code

 function woocommerce_custom_price_to_cart_item( $cart_object ) {  
    if( !WC()->session->__isset( "reload_checkout" )) {
        foreach ( $cart_object->cart_contents as $key => $value ) {
            if( isset( $value["custom_price"] ) ) {
                //for woocommerce version lower than 3
                //$value['data']->price = $value["custom_price"];
                //for woocommerce version +3
add_action( 'woocommerce_before_calculate_totals', 'woocommerce_custom_price_to_cart_item', 99 );

and you are good to go


Not the answer you're looking for? Browse other questions tagged or ask your own question.