Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zahedkamal87/98cc72e019075fcdbdc729d8bc7ae138 to your computer and use it in GitHub Desktop.
Save zahedkamal87/98cc72e019075fcdbdc729d8bc7ae138 to your computer and use it in GitHub Desktop.
Change woocommerce sales price programmatically
<?php
// Your Product ID
$product_id = 57;
$ecd_product = new WC_Product( $product_id );
/**
* Check if product exists
*/
if( $ecd_product->exists() ){
/**
* Get current (discounted) sales price for calculations etc
*/
$ecd_product->sale_price;
/**
* Get regular price for calculations etc
*/
$ecd_product->regular_price;
/**
* Get price (the actually price that people will pay)
* NOTE: If you set sales price different, this should be same as sales price
*/
$ecd_product->price;
/**
* Update sales price to $10
*/
$sp = update_post_meta( $ecd_product->id, '_sale_price', '10' );
$p = update_post_meta( $ecd_product->id, '_price', '10' );
if( $sp && $p ){
echo 'Sales price is $10 now!';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment