From a43e3b6a59c3238fc8989b2c050b66a76b474b20 Mon Sep 17 00:00:00 2001 From: Matt Sherman Date: Mon, 17 Jun 2024 16:19:41 -0400 Subject: [PATCH] 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 --- .../changelog/fix-product-save-status | 4 ++ .../class-wc-product-data-store-cpt.php | 6 +++ .../legacy/unit-tests/product/data-store.php | 52 ++++++++++++++++++- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 plugins/woocommerce/changelog/fix-product-save-status diff --git a/plugins/woocommerce/changelog/fix-product-save-status b/plugins/woocommerce/changelog/fix-product-save-status new file mode 100644 index 00000000000..9faa186a475 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-product-save-status @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Calling $product->get_status() after $product->save() on a new product now returns correct status. diff --git a/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php b/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php index 92345687e99..3267c5b3529 100644 --- a/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php @@ -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 ); diff --git a/plugins/woocommerce/tests/legacy/unit-tests/product/data-store.php b/plugins/woocommerce/tests/legacy/unit-tests/product/data-store.php index 5e78b76051e..8c7b0f72db2 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/product/data-store.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/product/data-store.php @@ -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. *