Merge pull request #26629 from woocommerce/fix/25552

Schedule a deferred product sync for products with parent on delete.
This commit is contained in:
Néstor Soriano 2020-06-29 14:15:10 +02:00 committed by GitHub
commit 7b3e902952
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 5 deletions

View File

@ -1378,9 +1378,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
$this->data_store->create( $this );
}
if ( $this->get_parent_id() ) {
wc_deferred_product_sync( $this->get_parent_id() );
}
$this->maybe_defer_product_sync();
/**
* Trigger action after saving to the DB.
@ -1393,6 +1391,32 @@ class WC_Product extends WC_Abstract_Legacy_Product {
return $this->get_id();
}
/**
* Delete the product, set its ID to 0, and return result.
*
* @param bool $force_delete Should the product be deleted permanently.
* @return bool result
*/
public function delete( $force_delete = false ) {
$deleted = parent::delete( $force_delete );
if ( $deleted ) {
$this->maybe_defer_product_sync();
}
return $deleted;
}
/**
* If this is a child product, queue its parent for syncing at the end of the request.
*/
protected function maybe_defer_product_sync() {
$parent_id = $this->get_parent_id();
if ( $parent_id ) {
wc_deferred_product_sync( $parent_id );
}
}
/*
|--------------------------------------------------------------------------
| Conditionals
@ -1871,7 +1895,8 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* @return string
*/
public function get_shipping_class() {
if ( $class_id = $this->get_shipping_class_id() ) { // @phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found, WordPress.CodeAnalysis.AssignmentInCondition.Found
$class_id = $this->get_shipping_class_id();
if ( $class_id ) {
$term = get_term_by( 'id', $class_id, 'product_shipping_class' );
if ( $term && ! is_wp_error( $term ) ) {
@ -1963,7 +1988,8 @@ class WC_Product extends WC_Abstract_Legacy_Product {
public function get_price_suffix( $price = '', $qty = 1 ) {
$html = '';
if ( ( $suffix = get_option( 'woocommerce_price_display_suffix' ) ) && wc_tax_enabled() && 'taxable' === $this->get_tax_status() ) { // @phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found, WordPress.CodeAnalysis.AssignmentInCondition.Found
$suffix = get_option( 'woocommerce_price_display_suffix' );
if ( $suffix && wc_tax_enabled() && 'taxable' === $this->get_tax_status() ) {
if ( '' === $price ) {
$price = $this->get_price();
}

View File

@ -0,0 +1,41 @@
<?php
/**
* Unit tests for the base product class.
*
* @package WooCommerce\Tests\Product
*/
/**
* Tests for Product class.
* @package WooCommerce\Tests\Product
* @since 2.3
*/
class WC_Tests_Product extends WC_Unit_Test_Case {
/**
* @testdox When a product is saved or deleted its parent should be scheduled for sync at the end of the request.
*
* @testWith ["save"]
* ["delete"]
*
* @param string $operation The method to test, "save" or "delete".
*/
public function test_deferred_sync_on_save_and_delete( $operation ) {
$defer_sync_invoked = false;
$defer_product_callback = function() use ( &$defer_sync_invoked ) {
$defer_sync_invoked = true;
};
$product = $this->getMockBuilder( WC_Product::class )
->setMethods( array( 'maybe_defer_product_sync' ) )
->getMock();
$product->method( 'maybe_defer_product_sync' )
->will( $this->returnCallback( $defer_product_callback ) );
$product->$operation();
$this->assertTrue( $defer_sync_invoked );
}
}