Fix: product attributes lookup table not updating on REST API batch requests

When the "products/batch" endpoint is used to create or update products,
the product attributes lookup table wasn't being updated as it's the
case of the single product endpoints.
This commit is contained in:
Nestor Soriano 2022-05-06 10:20:38 +02:00
parent 3d77547d77
commit ff76f898f7
No known key found for this signature in database
GPG Key ID: 08110F3518C12CAD
2 changed files with 27 additions and 3 deletions

View File

@ -227,7 +227,7 @@ abstract class WC_REST_Controller extends WP_REST_Controller {
if ( ! empty( $items['create'] ) ) {
foreach ( $items['create'] as $item ) {
$_item = new WP_REST_Request( 'POST' );
$_item = new WP_REST_Request( 'POST', $request->get_route() );
// Default parameters.
$defaults = array();
@ -264,7 +264,7 @@ abstract class WC_REST_Controller extends WP_REST_Controller {
if ( ! empty( $items['update'] ) ) {
foreach ( $items['update'] as $item ) {
$_item = new WP_REST_Request( 'PUT' );
$_item = new WP_REST_Request( 'PUT', $request->get_route() );
$_item->set_body_params( $item );
$_response = $this->update_item( $_item );
@ -291,7 +291,7 @@ abstract class WC_REST_Controller extends WP_REST_Controller {
continue;
}
$_item = new WP_REST_Request( 'DELETE' );
$_item = new WP_REST_Request( 'DELETE', $request->get_route() );
$_item->set_query_params(
array(
'id' => $id,

View File

@ -6,6 +6,7 @@
namespace Automattic\WooCommerce\Internal\ProductAttributesLookup;
use Automattic\WooCommerce\Utilities\ArrayUtil;
use Automattic\WooCommerce\Utilities\StringUtil;
defined( 'ABSPATH' ) || exit;
@ -66,6 +67,15 @@ class LookupDataStore {
1
);
add_action(
'woocommerce_rest_insert_product',
function ( $product_post, $request ) {
$this->on_product_created_or_updated_via_rest_api( $product_post, $request );
},
100,
2
);
add_filter(
'woocommerce_get_settings_products',
function ( $settings, $section_id ) {
@ -633,6 +643,20 @@ class LookupDataStore {
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
}
/**
* Handler for the woocommerce_rest_insert_product hook.
* Needed to update the lookup table when the REST API batch insert/update endpoints are used.
*
* @param WP_Post $product The post representing the created or updated product.
* @param \WP_REST_Request $request The REST request that caused the hook to be fired.
* @return void
*/
private function on_product_created_or_updated_via_rest_api( WP_Post $product, \WP_REST_Request $request ): void {
if ( StringUtil::ends_with( $request->get_route(), '/batch' ) ) {
$this->on_product_changed( $product->ID );
}
}
/**
* Tells if a lookup table regeneration is currently in progress.
*