nirab

WooCommerce change price programmatically

Sep 25, 2022
#WooCommerce

To change existing product price on WooCommerce via programmatically , You need to use these filter Hooks :

<?php

add_filter( 'woocommerce_product_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
add_filter( 'woocommerce_product_get_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'custom_dynamic_sale_price', 10, 2 );

Simple Product

If product type is simple or it’s based on WC_Product_Simple class , You might be use these simple product related filter hooks only :

<?php

add_filter( 'woocommerce_product_get_regular_price', 'function_for_logic', 10, 2 );
add_filter( 'woocommerce_product_get_price', 'function_for_logic', 10, 2 );

EXAMPLE :

update regular price

<?php

add_filter('woocommerce_product_get_regular_price', 'update_regular_price', 10, 2);

function update_regular_price($regular_price, $product) {
   // some logic happen here

   return $regular_price;
}

update sale price

<?php

add_filter('woocommerce_product_get_price', 'update_sale_price', 10, 2);

function update_sale_price($price, $product) {
   // some logic happen here

   return $price;
}

Variable Product

To update variation price you need to use these hooks :

<?php

add_filter( 'woocommerce_product_variation_get_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_variation_prices_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_variation_prices_sale_price', 'custom_dynamic_sale_price', 10, 2 );

EXAMPLE :

update variation price

<?php

add_filter('woocommerce_product_variation_get_price', 'update_variation_price', 10, 2);

function update_variation_price($price, $variation) {
  // some logic happen here

  return $price;
}

Thanks.


me

Istiaq Nirab

Full stack web developer

I’m a WordPress Plugin developer & have some experienced with WooCommerce . Last couple of years, I developed 10+ WooCommerce extensions & also did some custom solutions . I'm ready for served any WordPress & WooCommerce related custom solutions.

Follow me on twitter