Skip to content

Instantly share code, notes, and snippets.

@SiR-DanieL
Last active October 30, 2023 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SiR-DanieL/cd02ce62b2ff9c3fdd51425e18d8b8ef to your computer and use it in GitHub Desktop.
Save SiR-DanieL/cd02ce62b2ff9c3fdd51425e18d8b8ef to your computer and use it in GitHub Desktop.
Set a Default Price for Products
<?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