Force meta to be written on creation

#12567
This commit is contained in:
Mike Jolley 2016-12-12 13:56:35 +00:00
parent ecbf72632d
commit 1e80db6021
8 changed files with 31 additions and 23 deletions

View File

@ -59,7 +59,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
'tax_class' => '',
'manage_stock' => false,
'stock_quantity' => null,
'stock_status' => '',
'stock_status' => 'instock',
'backorders' => 'no',
'sold_individually' => false,
'weight' => '',

View File

@ -68,7 +68,7 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
if ( $id && ! is_wp_error( $id ) ) {
$order->set_id( $id );
$this->update_post_meta( $order );
$this->update_post_meta( $order, true );
$order->save_meta_data();
$order->apply_changes();
$this->clear_caches( $order );
@ -204,9 +204,10 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
* Helper method that updates all the post meta for an order based on it's settings in the WC_Order class.
*
* @param WC_Order
* @param bool $force Force all props to be written even if not changed. This is used during creation.
* @since 2.7.0
*/
protected function update_post_meta( &$order ) {
protected function update_post_meta( &$order, $force = false ) {
$updated_props = array();
$changed_props = array_keys( $order->get_changes() );
$meta_key_to_props = array(
@ -221,7 +222,7 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
'_prices_include_tax' => 'prices_include_tax',
);
foreach ( $meta_key_to_props as $meta_key => $prop ) {
if ( ! in_array( $prop, $changed_props ) ) {
if ( ! in_array( $prop, $changed_props ) && ! $force ) {
continue;
}
$value = $order->{"get_$prop"}( 'edit' );

View File

@ -69,7 +69,7 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
if ( $coupon_id ) {
$coupon->set_id( $coupon_id );
$this->update_post_meta( $coupon );
$this->update_post_meta( $coupon, true );
$coupon->save_meta_data();
$coupon->apply_changes();
do_action( 'woocommerce_new_coupon', $coupon_id );
@ -164,9 +164,11 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
/**
* Helper method that updates all the post meta for a coupon based on it's settings in the WC_Coupon class.
*
* @param WC_Coupon
* @param bool $force Force all props to be written even if not changed. This is used during creation.
* @since 2.7.0
*/
private function update_post_meta( $coupon ) {
private function update_post_meta( &$coupon, $force = false ) {
$updated_props = array();
$changed_props = array_keys( $coupon->get_changes() );
@ -191,7 +193,7 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
);
foreach ( $meta_key_to_props as $meta_key => $prop ) {
if ( ! in_array( $prop, $changed_props ) ) {
if ( ! in_array( $prop, $changed_props ) && ! $force ) {
continue;
}
$value = $coupon->{"get_$prop"}( 'edit' );

View File

@ -127,9 +127,10 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
* Helper method that updates all the post meta for an order based on it's settings in the WC_Order class.
*
* @param WC_Order
* @param bool $force Force all props to be written even if not changed. This is used during creation.
* @since 2.7.0
*/
protected function update_post_meta( &$order ) {
protected function update_post_meta( &$order, $force = false ) {
$updated_props = array();
$changed_props = $order->get_changes();
$meta_key_to_props = array(
@ -147,7 +148,7 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
);
foreach ( $meta_key_to_props as $meta_key => $prop ) {
if ( ! array_key_exists( $prop, $changed_props ) ) {
if ( ! array_key_exists( $prop, $changed_props ) && ! $force ) {
continue;
}
$value = $order->{"get_$prop"}( 'edit' );
@ -187,7 +188,7 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
foreach ( $address_props as $props_key => $props ) {
foreach ( $props as $meta_key => $prop ) {
$prop_key = substr( $prop, 8 );
if ( ! isset( $changed_props[ $props_key ] ) || ! array_key_exists( $prop_key, $changed_props[ $props_key ] ) ) {
if ( ! $force && ( ! isset( $changed_props[ $props_key ] ) || ! array_key_exists( $prop_key, $changed_props[ $props_key ] ) ) ) {
continue;
}
$value = $order->{"get_$prop"}( 'edit' );
@ -199,7 +200,7 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
}
}
parent::update_post_meta( $order );
parent::update_post_meta( $order, $force );
// If address changed, store concatinated version to make searches faster.
if ( in_array( 'billing', $updated_props ) || ! metadata_exists( 'post', $order->get_id(), '_billing_address_index' ) ) {

View File

@ -54,10 +54,11 @@ class WC_Order_Refund_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT im
* Helper method that updates all the post meta for an order based on it's settings in the WC_Order class.
*
* @param WC_Order
* @param bool $force Force all props to be written even if not changed. This is used during creation.
* @since 2.7.0
*/
protected function update_post_meta( &$refund ) {
parent::update_post_meta( $refund );
protected function update_post_meta( &$refund, $force = false ) {
parent::update_post_meta( $refund, $force );
$updated_props = array();
$changed_props = $refund->get_changes();
@ -68,7 +69,7 @@ class WC_Order_Refund_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT im
);
foreach ( $meta_key_to_props as $meta_key => $prop ) {
if ( ! array_key_exists( $prop, $changed_props ) ) {
if ( ! array_key_exists( $prop, $changed_props ) && ! $force ) {
continue;
}
$value = $refund->{"get_$prop"}( 'edit' );

View File

@ -88,7 +88,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
if ( $id && ! is_wp_error( $id ) ) {
$product->set_id( $id );
$this->update_post_meta( $product );
$this->update_post_meta( $product, true );
$this->update_terms( $product );
$this->update_visibility( $product );
$this->update_attributes( $product );
@ -358,9 +358,10 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
* Helper method that updates all the post meta for a product based on it's settings in the WC_Product class.
*
* @param WC_Product
* @param bool $force Force all props to be written even if not changed. This is used during creation.
* @since 2.7.0
*/
protected function update_post_meta( &$product ) {
protected function update_post_meta( &$product, $force = false ) {
$updated_props = array();
$changed_props = array_keys( $product->get_changes() );
$meta_key_to_props = array(
@ -397,7 +398,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
);
foreach ( $meta_key_to_props as $meta_key => $prop ) {
if ( ! in_array( $prop, $changed_props ) ) {
if ( ! in_array( $prop, $changed_props ) && ! $force ) {
continue;
}
$value = $product->{"get_$prop"}( 'edit' );
@ -451,7 +452,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
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 ) ) ) {
if ( ( $force || in_array( $key, $changed_props ) ) && is_callable( array( $product, $function ) ) ) {
update_post_meta( $product->get_id(), '_' . $key, $product->{$function}( 'edit' ) );
}
}

View File

@ -16,9 +16,10 @@ class WC_Product_Grouped_Data_Store_CPT extends WC_Product_Data_Store_CPT implem
* Helper method that updates all the post meta for a grouped product.
*
* @param WC_Product
* @param bool $force Force all props to be written even if not changed. This is used during creation.
* @since 2.7.0
*/
protected function update_post_meta( &$product ) {
protected function update_post_meta( &$product, $force = false ) {
if ( update_post_meta( $product->get_id(), '_children', $product->get_children( 'edit' ) ) ) {
$child_prices = array();
foreach ( $product->get_children( 'edit' ) as $child_id ) {
@ -38,7 +39,7 @@ class WC_Product_Grouped_Data_Store_CPT extends WC_Product_Data_Store_CPT implem
$this->extra_data_saved = true;
}
parent::update_post_meta( $product );
parent::update_post_meta( $product, $force );
}
/**

View File

@ -97,7 +97,7 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
if ( $id && ! is_wp_error( $id ) ) {
$product->set_id( $id );
$this->update_post_meta( $product );
$this->update_post_meta( $product, true );
$this->update_terms( $product );
$this->update_attributes( $product );
$product->save_meta_data();
@ -250,9 +250,10 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
*
* @since 2.7.0
* @param WC_Product
* @param bool $force Force all props to be written even if not changed. This is used during creation.
*/
public function update_post_meta( &$product ) {
public function update_post_meta( &$product, $force = false ) {
update_post_meta( $product->get_id(), '_variation_description', $product->get_description() );
parent::update_post_meta( $product );
parent::update_post_meta( $product, $force );
}
}