Skip to content

Instantly share code, notes, and snippets.

@bob-moore
Created October 12, 2021 18:09
Show Gist options
  • Save bob-moore/192e0b729d869897521963a5b6cbed04 to your computer and use it in GitHub Desktop.
Save bob-moore/192e0b729d869897521963a5b6cbed04 to your computer and use it in GitHub Desktop.
<?php
function generate_price_metadata( $product_id ) {
/**
* Get the product based on ID
*/
$product = wc_get_product( $product_id );
/**
* This should be set during import to false
*
* Correcting this setting here for variable products so stock is managed
* at the variation level, not at the product level
*/
update_post_meta( $product_id, '_manage_stock', false );
/**
* Method only exists for variable products, so double check in case of
* simple product
*
* Runs the sync function to generate the '_price' metadata
*/
if ( method_exists( $product, 'sync' ) ) {
$product::sync( $product );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment