WooCommerce add product to cart with custom price

WooCommerce add product to cart with custom price

Today, We are going to learn WooCommerce add product to cart with custom price. In one of my project client want to set custom price without changing the actual product price. Then After, Allow user to purchase the product with that custom product price instead of the actual product price.

WooCommerce allows us to set a custom price for all product or any specific product or a specific user. We are going to add a condition on the code for a set custom price.

WooCommerce add product to cart with custom price

We can allow a user to WooCommerce add product to cart with custom price. Sometimes, User want to set the special price for a product in festival days. Without changing the actual product price.

Let’s see we are going to set the custom price on the product. For example, You want to set the custom product on the first product. So, We have to add the condition on the product id.

First of all, Open the currently active theme functions.php file. Then after, Write the custom price condition like.

add_filter('woocommerce_get_price','change_price', 10, 2);
add_filter('woocommerce_get_regular_price','change_price', 10, 2);
add_filter('woocommerce_get_sale_price','change_price', 10, 2);

function change_price($price, $productd){ 
	if($productd->id == '1'){ 
		$price = "150"; 
	} 
	return $price; 
}

Right now, I set the product ID to 1 You should change the product ID. You will find the product ID on the page URL when you edit the product from the WordPress admin panel.

Just replace the product ID then check the WooCommerce will show the custom product price instead of the actual price.

You want to set the custom product price only for a specific user. Then, We should add the user condition with the product ID condition like.

add_filter('woocommerce_get_price','change_price', 10, 2);
add_filter('woocommerce_get_regular_price','change_price', 10, 2);
add_filter('woocommerce_get_sale_price','change_price', 10, 2);

function change_price($price, $productd){ 
	$current_user_id = get_current_user_id(); 
	if( $current_user_id == 15 && $productd->id == 1){ 
		$price = "150"; 
	} 
	return $price; 
}

As per, The above condition I want to set the custom product price only for the user that ID is 15. Just change the user and product IDs as you want.

Now, Check you can add a product to cart with custom price without changing the actual product price.

In conclusion, Today we learn How to WooCommerce add product to cart with custom price. I hope this article is helpful to you. If you have any question please write the comment. We are always ready to help you.

Related Posts

  1. How to Remove WooCommerce tabs from product page
  2. Add to cart variable products WooCommerce with variation button URL
  3. WooCommerce add multiple products to cart using URL
  4. WordPress do_shortcode in template PHP file
  5. MySQL value BETWEEN Two Columns in Laravel
  6. PHP tree structure for categories and sub categories without looping a MySQL query
  7. Email unique validation in Laravel ignore id on update
  8. WordPress Button Shortcode – Add button to WordPress editor
  9. WordPress shortcode – How to create shortcode in WordPress
  10. WordPress change URL in database using MySQL Query

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Search Posts

Subscribe To Newsletter

Get Early Access To New Articles, Plugins, Discount Codes And Quickly Brief Updates about tips and tricks

Join 357 other subscribers

%d bloggers like this: