protected function update_post_meta(&$product)
{
$updated_props = array();
$changed_props = array_keys($product->get_changes());
$meta_key_to_props = array('_visibility' => 'catalog_visibility', '_sku' => 'sku', '_regular_price' => 'regular_price', '_sale_price' => 'sale_price', '_sale_price_dates_from' => 'date_on_sale_from', '_sale_price_dates_to' => 'date_on_sale_to', 'total_sales' => 'total_sales', '_tax_status' => 'tax_status', '_tax_class' => 'tax_class', '_manage_stock' => 'manage_stock', '_backorders' => 'backorders', '_sold_individually' => 'sold_individually', '_weight' => 'weight', '_length' => 'length', '_width' => 'width', '_height' => 'height', '_upsell_ids' => 'upsell_ids', '_crosssell_ids' => 'cross_sell_ids', '_purchase_note' => 'purchase_note', '_default_attributes' => 'default_attributes', '_virtual' => 'virtual', '_downloadable' => 'downloadable', '_product_image_gallery' => 'gallery_image_ids', '_download_limit' => 'download_limit', '_download_expiry' => 'download_expiry', '_featured' => 'featured', '_thumbnail_id' => 'image_id', '_stock' => 'stock_quantity', '_stock_status' => 'stock_status', '_wc_average_rating' => 'average_rating', '_wc_rating_count' => 'rating_counts', '_wc_review_count' => 'review_count');
foreach ($meta_key_to_props as $meta_key => $prop) {
if (!in_array($prop, $changed_props)) {
continue;
}
$value = $product->{"get_{$prop}"}('edit');
switch ($prop) {
case 'virtual':
case 'downloadable':
case 'manage_stock':
case 'featured':
case 'sold_individually':
$updated = update_post_meta($product->get_id(), $meta_key, wc_bool_to_string($value));
break;
case 'gallery_image_ids':
$updated = update_post_meta($product->get_id(), $meta_key, implode(',', $value));
break;
case 'image_id':
if (!empty($value)) {
set_post_thumbnail($product->get_id(), $value);
} else {
delete_post_meta($product->get_id(), '_thumbnail_id');
}
$updated = true;
break;
default:
$updated = update_post_meta($product->get_id(), $meta_key, $value);
break;
}
if ($updated) {
$updated_props[] = $prop;
}
}
if (in_array('date_on_sale_from', $updated_props) || in_array('date_on_sale_to', $updated_props) || in_array('regular_price', $updated_props) || in_array('sale_price', $updated_props)) {
if ($product->is_on_sale()) {
update_post_meta($product->get_id(), '_price', $product->get_sale_price());
$product->set_price($product->get_sale_price());
} else {
update_post_meta($product->get_id(), '_price', $product->get_regular_price());
$product->set_price($product->get_regular_price());
}
}
if (in_array('featured', $updated_props)) {
delete_transient('wc_featured_products');
}
if (in_array('catalog_visibility', $updated_props)) {
do_action('woocommerce_product_set_visibility', $product->get_id(), $product->get_catalog_visibility());
}
if (in_array('stock_quantity', $updated_props)) {
do_action($product->is_type('variation') ? 'woocommerce_variation_set_stock' : 'woocommerce_product_set_stock', $product);
}
if (in_array('stock_status', $updated_props)) {
do_action($product->is_type('variation') ? 'woocommerce_variation_set_stock_status' : 'woocommerce_product_set_stock_status', $product->get_id(), $product->get_stock_status(), $product);
}
// Update extra data associated with the product.
// Like button text or product URL for external products.
if (!$this->extra_data_saved) {
foreach ($product->get_extra_data_keys() as $key) {
$function = 'get_' . $key;
if (in_array($key, $changed_props) && is_callable(array($product, $function))) {
update_post_meta($product->get_id(), '_' . $key, $product->{$function}('edit'));
}
}
}
}