Fix restoring product prices on bulk edit (#50836)

* Set the regular price when the `Change to:` price is left empty

* Avoid fatal error when price is empty

* Add changefile(s) from automation for the following project(s): woocommerce

* Add e2e test

* Fix lint errors

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Alba Rincón 2024-08-23 13:28:11 +02:00 committed by GitHub
parent 091141218e
commit 257762b564
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 108 additions and 3 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: fix
Product bulk edit: allow to restore original price and avoid fatal errors.

View File

@ -906,13 +906,17 @@ class WC_Admin_Post_Types {
switch ( $change_price ) {
case 1:
$new_price = $price;
if ( empty( $price ) ) {
$new_price = $product->get_regular_price();
} else {
$new_price = $price;
}
break;
case 2:
if ( $is_percentage ) {
$percent = $price / 100;
$new_price = $old_price + ( $old_price * $percent );
} else {
} elseif ( ! empty( $price ) ) {
$new_price = $old_price + $price;
}
break;
@ -920,7 +924,7 @@ class WC_Admin_Post_Types {
if ( $is_percentage ) {
$percent = $price / 100;
$new_price = max( 0, $old_price - ( $old_price * $percent ) );
} else {
} elseif ( ! empty( $price ) ) {
$new_price = max( 0, $old_price - $price );
}
break;

View File

@ -185,3 +185,100 @@ test(
} );
}
);
test(
'can restore regular price when bulk editing products',
{ tag: [ '@gutenberg', '@services' ] },
async ( { page, products } ) => {
await page.goto( `wp-admin/edit.php?post_type=product` );
const salePriceDecrease = 10;
await test.step( 'select and bulk edit the products', async () => {
for ( const product of products ) {
await page.getByLabel( `Select ${ product.name }` ).click();
}
await page
.locator( '#bulk-action-selector-top' )
.selectOption( 'Edit' );
await page.locator( '#doaction' ).click();
await expect(
await page.locator( '#bulk-titles-list li' ).count()
).toEqual( products.length );
} );
await test.step( 'update the sale price', async () => {
await page
.locator( 'select[name="change_sale_price"]' )
.selectOption(
'Set to regular price decreased by (fixed amount or %):'
);
await page
.getByPlaceholder( 'Enter sale price ($)' )
.fill( `${ salePriceDecrease }%` );
} );
await test.step( 'save the updates', async () => {
await page.getByRole( 'button', { name: 'Update' } ).click();
} );
await test.step( 'verify the changes', async () => {
for ( const product of products ) {
await page.goto( `product/${ product.slug }` );
const expectedRegularPrice = product.regular_price;
const expectedSalePrice = (
expectedRegularPrice *
( 1 - salePriceDecrease / 100 )
).toFixed( 2 );
await expect
.soft(
await page
.locator( 'ins' )
.getByText( `$${ expectedSalePrice }` )
.count()
)
.toBeGreaterThan( 0 );
}
} );
await test.step( 'Update products leaving the "Sale > Change to" empty', async () => {
await page.goto( `wp-admin/edit.php?post_type=product` );
for ( const product of products ) {
await page.getByLabel( `Select ${ product.name }` ).click();
}
await page
.locator( '#bulk-action-selector-top' )
.selectOption( 'Edit' );
await page.locator( '#doaction' ).click();
await page
.locator( 'select[name="change_sale_price"]' )
.selectOption( 'Change to:' );
await page.getByRole( 'button', { name: 'Update' } ).click();
} );
await test.step( 'Verify products have their regular price again', async () => {
for ( const product of products ) {
await page.goto( `product/${ product.slug }` );
const expectedRegularPrice = product.regular_price;
await expect
.soft( await page.locator( 'ins' ).count() )
.toBe( 0 );
await expect
.soft( await page.locator( 'bdi' ).first() )
.toContainText( expectedRegularPrice );
}
} );
}
);