A Detailed Tutorial to WooCommerce Set Product Price Programmatically

Sometimes, you want to showcase different prices to different customers. For instance, showing a discounted price to logged in customers and vice-a-versa. However, the edit product page settings are not sufficient all the time. So, in such scenarios, you need to learn about WooCommerce set product price programmatically. 

You require such coding operations whenever you are running a special promotion offer. The reason being you do not wish to waste time. It takes a great deal in manually setting up discounted prices for thousands of products. 

So, you need to implement the two things while you set/override product price programmatically. Firstly, you should change the ‘display’ of product pricing on single & loop pages. Secondly, you are required to set a ‘cart item’ price to alter the price values in a right way. 

Furthermore, let’s see how you can code it on your website.

Image showing how the product price will appear after learning WooCommerce Set Product Price Programmatically

PHP Snippet: WooCommerce Set Product Price Programmatically at WooCommerce Frontend

In the below snippet, we will alter the product prices for logged in and registered customers. Also, this WooCommerce set product price programmatically strategy can be implemented in other scenarios. For instance, changing prices for a specific category, apply a flat discount to all products priced under $200. 

/**
* @snippet Alter Product Pricing Part 1 - WooCommerce Product
* @author PHP - Stackoverflow
* @compatible WooCommerce 4.1
*/

add_filter( 'woocommerce_get_price_html',
'phpsof_alter_price_display', 9999, 2 );

function phpsof_alter_price_display( $price_html, $product ) {

// ONLY ON FRONTEND
if ( is_admin() ) return $price_html;

// ONLY IF PRICE NOT NULL
if ( '' === $product->get_price() ) return $price_html;

// IF CUSTOMER LOGGED IN, APPLY 20% DISCOUNT
if ( wc_current_user_has_role( 'customer' ) ) {
    $orig_price = wc_get_price_to_display( $product );
    $price_html = wc_price( $orig_price * 0.80 );
}

return $price_html;

}

/**
 * @snippet Alter Product Pricing Part 2 - WooCommerce
Cart/Checkout
* @author PHP - Stackoverflow
* @compatible WooCommerce 4.1
*/

add_action( 'woocommerce_before_calculate_totals',
'phpsof_alter_price_cart', 9999 );

function phpsof_alter_price_cart( $cart ) {

if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;

// IF CUSTOMER NOT LOGGED IN, DONT APPLY DISCOUNT
if ( ! wc_current_user_has_role( 'customer' ) ) return;

// LOOP THROUGH CART ITEMS & APPLY 20% DISCOUNT
  foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
      $product = $cart_item['data'];
      $price = $product->get_price();
      $cart_item['data']->set_price( $price * 0.80 );
  }

}

Furthermore, we provide another screenshot for the above product page. This is how the price will appear for non-logged in & logged-in but non-customer users. As shown in the screenshot, the original product price is $34. 

Image showing original price to the non-logged in users on a product page

Where to add this snippet?

You can place this PHP snippet at the bottom of your child theme functions.php file. You are supposed to place it before “?>” if you have any. Apart from this, CSS goes in the child theme style.css file. 

Is this snippet still valid?

I have checked this snippet with the Storefront theme & the WooCommerce version stated above. Also, this WooCommerce set product price programmatically strategy is tested with WordPress-friendly hosting on PHP 7+. 

Also Read, WooCommerce Get Product Info (ID SKU $) from $product object

Guide on Storefront theme Edit or Remove Footer Credits