You are currently viewing WooCommerce set custom product price when adding to cart

WooCommerce set custom product price when adding to cart

Woocommerce product cart price with custom price when adding to cart. Using override price you can update product price on cart by flat, percentage or specific products. Below code will update the product price to 100 when adding to cart. Just add below code in your theme’s function.php file.

function add_custom_price( $cart_object ) {    
    foreach ( $cart_object->cart_contents as $key => $value ) {
	$value['data']->set_price(100);
    }
}

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

Let’s take an example, add 50 to every product cart price when adding to cart. Below code will add 50 to cart price on cart page.

function add_custom_price( $cart_object ) {    
    foreach ( $cart_object->get_cart() as $key => $value ) {
        $price = $value['data']->get_regular_price();
	$newprice = $price + 50;
        $value['data']->set_price( $newprice );
    }
}

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

Leave a Reply