Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new _variations_skus meta field for products #2763

Merged
merged 2 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions includes/classes/Feature/WooCommerce/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public function whitelist_meta_keys( $meta, $post ) {
'_billing_first_name',
'_shipping_first_name',
'_shipping_last_name',
'_variations_skus',
)
)
);
Expand Down Expand Up @@ -805,6 +806,7 @@ public function setup() {
add_filter( 'ep_facet_include_taxonomies', [ $this, 'add_product_attributes' ] );
add_filter( 'ep_weighting_fields_for_post_type', [ $this, 'add_product_attributes_to_weighting' ], 10, 2 );
add_filter( 'ep_weighting_default_post_type_weights', [ $this, 'add_product_default_post_type_weights' ], 10, 2 );
add_filter( 'ep_prepare_meta_data', [ $this, 'add_variations_skus_meta' ], 10, 2 );
}

/**
Expand Down Expand Up @@ -935,6 +937,42 @@ public function keep_order_fields( $skip, $post_args ) {
return $skip;
}

/**
* Add a new `_variations_skus` meta field to the product to be indexed in Elasticsearch.
*
* @since 4.2.0
* @param array $post_meta Post meta
* @param WP_Post $post Post object
* @return array
*/
public function add_variations_skus_meta( $post_meta, $post ) {
if ( 'product' !== $post->post_type ) {
return $post_meta;
}

$product = wc_get_product( $post );
$variations_ids = $product->get_children();

$post_meta['_variations_skus'] = array_reduce(
$variations_ids,
function ( $variations_skus, $current_id ) {
$variation = wc_get_product( $current_id );
if ( ! $variation || ! $variation->exists() ) {
return $variations_skus;
}
$variation_sku = $variation->get_sku();
if ( ! $variation_sku ) {
return $variations_skus;
}
$variations_skus[] = $variation_sku;
return $variations_skus;
},
[]
);

return $post_meta;
}

/**
* Determines whether or not ES should be integrating with the provided query
*
Expand Down
37 changes: 37 additions & 0 deletions tests/php/features/TestWooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,41 @@ public function testSearchOnAllFrontEnd() {

$this->assertTrue( $query->elasticsearch_success );
}

/**
* Test the addition of variations skus to product meta
*
* @since 4.2.0
* @group woocommerce
*/
public function testAddVariationsSkusMeta() {
ElasticPress\Features::factory()->activate_feature( 'woocommerce' );
ElasticPress\Features::factory()->setup_features();

$this->assertTrue( class_exists( '\WC_Product_Variable' ) );
$this->assertTrue( class_exists( '\WC_Product_Variation' ) );

$main_product = new \WC_Product_Variable();
$main_product->set_sku('main-product_sku');
$main_product_id = $main_product->save();

$variation_1 = new \WC_Product_Variation();
$variation_1->set_parent_id( $main_product_id );
$variation_1->set_sku('child-sku-1');
$variation_1->save();

$variation_2 = new \WC_Product_Variation();
$variation_2->set_parent_id( $main_product_id );
$variation_2->set_sku('child-sku-2');
$variation_2->save();

$main_product_as_post = get_post( $main_product_id );
$product_meta_to_index = ElasticPress\Features::factory()
->get_registered_feature( 'woocommerce' )
->add_variations_skus_meta( [], $main_product_as_post );

$this->assertArrayHasKey( '_variations_skus', $product_meta_to_index );
$this->assertContains( 'child-sku-1', $product_meta_to_index['_variations_skus'] );
$this->assertContains( 'child-sku-2', $product_meta_to_index['_variations_skus'] );
}
}