Update product status after calling save() on a new product (#48241)
* Assert that a new product that is saved returns the correct status ('publish' by default) * Update product status after saving * Changelog * Add additional unit tests to verify status is handled correctly
This commit is contained in:
parent
eabc599c51
commit
a43e3b6a59
|
@ -0,0 +1,4 @@
|
|||
Significance: patch
|
||||
Type: fix
|
||||
|
||||
Calling $product->get_status() after $product->save() on a new product now returns correct status.
|
|
@ -138,6 +138,12 @@ 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 );
|
||||
|
||||
// get the post object so that we can set the status
|
||||
// to the correct value; it is possible that the status was
|
||||
// changed by the woocommerce_new_product_data filter above.
|
||||
$post_object = get_post( $product->get_id() );
|
||||
$product->set_status( $post_object->post_status );
|
||||
|
||||
$this->update_post_meta( $product, true );
|
||||
$this->update_terms( $product, true );
|
||||
$this->update_visibility( $product, true );
|
||||
|
|
|
@ -24,7 +24,7 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {
|
|||
}
|
||||
|
||||
/**
|
||||
* Test creating a new product.
|
||||
* Test creating a new product (published by default).
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
|
@ -34,12 +34,62 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {
|
|||
$product->set_name( 'My Product' );
|
||||
$product->save();
|
||||
|
||||
$this->assertEquals( 'publish', $product->get_status() );
|
||||
|
||||
$read_product = new WC_Product( $product->get_id() );
|
||||
|
||||
$this->assertEquals( '42', $read_product->get_regular_price() );
|
||||
$this->assertEquals( 'My Product', $read_product->get_name() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a new product (explicitly set to published).
|
||||
*/
|
||||
public function test_product_create_published() {
|
||||
$product = new WC_Product();
|
||||
$product->set_status( 'publish' );
|
||||
$product->save();
|
||||
|
||||
$this->assertEquals( 'publish', $product->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a new draft product.
|
||||
*/
|
||||
public function test_product_create_draft() {
|
||||
$product = new WC_Product();
|
||||
$product->set_status( 'draft' );
|
||||
$product->save();
|
||||
|
||||
$this->assertEquals( 'draft', $product->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a new product with woocommerce_new_product_data filter.
|
||||
*/
|
||||
public function test_product_create_with_woocommerce_new_product_data_filter() {
|
||||
$force_draft_status_fn = function ( $data ) {
|
||||
$data['post_status'] = 'draft';
|
||||
return $data;
|
||||
};
|
||||
|
||||
add_filter(
|
||||
'woocommerce_new_product_data',
|
||||
$force_draft_status_fn
|
||||
);
|
||||
|
||||
$product = new WC_Product();
|
||||
$product->set_status( 'pending' );
|
||||
$product->save();
|
||||
|
||||
$this->assertEquals( 'draft', $product->get_status() );
|
||||
|
||||
remove_filter(
|
||||
'woocommerce_new_product_data',
|
||||
$force_draft_status_fn
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reading a product.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue