Last active
October 30, 2023 10:13
-
-
Save SirDarcanos/cd02ce62b2ff9c3fdd51425e18d8b8ef to your computer and use it in GitHub Desktop.
Set a Default Price for Products
This file contains hidden or 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 | |
/** | |
* Sets the product default price to 10. Only works once, if the price is not specified. | |
* | |
* @param int $post_id | |
* @param object $post | |
*/ | |
function set_product_default_price( $post_id, $post ) { | |
$product = wc_get_product( $post_id ); | |
$already_set = get_post_meta( $post_id, '_set_default_price', true ); | |
$price = $product->get_price(); | |
if ( 'yes' !== $already_set && empty( $price ) ) { | |
$product->set_regular_price( '10' ); | |
$product->save(); | |
update_post_meta( $post_id, '_set_default_price', 'yes' ); | |
} | |
} | |
add_action( 'woocommerce_process_product_meta', 'set_product_default_price', 999, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment