Last active
June 27, 2022 00:10
-
-
Save mrizwan47/24061263dc47be0d30483c1dd08f78f6 to your computer and use it in GitHub Desktop.
Change woocommerce sales price programmatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); | |
$rp = update_post_meta( $ecd_product->id, '_regular_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
This code worked for me
I had to update and set static price for every single product in woocommerce.