I would like to add a custom meta_key
value to each WooCommerce product It will be the Discount rate :
_discount_rate = ((_sale_price-_regular_price_)/(_regular_price)*100)
I'm trying to figure out how to add a filter to WooCommerce function woocommerce_process_product_meta
Something like:
add_filter('woocommerce_process_product_meta', 'mytheme_product_save_discountrate');
function mytheme_product_save_discountrate($post_id) {
if (get_post_meta($post_id, "_sale_price")) {
$regular_price = get_post_meta($post_id, "_regular_price");
$sale_price = get_post_meta($post_id, "_sale_price");
$discount_rate = ($sale_price - $regular_price) / $regular_price * 100);
update_post_meta($post_id, '_discount_rate', $discount_rate);
}
}
I'm just not sure how I can retrieve the regular and sale prices?