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

Only sync selected categories as product type #2233

Merged
merged 2 commits into from
Feb 5, 2024
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
8 changes: 6 additions & 2 deletions src/Product/WCProductAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,11 @@ protected function map_wc_general_attributes() {
*/
protected function map_product_categories() {
// set product type using merchants defined product categories
$base_product_id = $this->is_variation() ? $this->parent_wc_product->get_id() : $this->wc_product->get_id();
$this->product_category_ids = wc_get_product_cat_ids( $base_product_id );
$base_product_id = $this->is_variation() ? $this->parent_wc_product->get_id() : $this->wc_product->get_id();

// Fetch only selected term ids without parents.
$this->product_category_ids = wc_get_product_term_ids( $base_product_id, 'product_cat' );

if ( ! empty( $this->product_category_ids ) ) {
$google_product_types = self::convert_product_types( $this->product_category_ids );
do_action(
Expand Down Expand Up @@ -234,6 +237,7 @@ public static function convert_product_types( $category_ids ): array {
}

/**
* Return category names including ancestors, separated by ">"
*
* @param int $category_id
*
Expand Down
25 changes: 23 additions & 2 deletions tests/Unit/Product/WCProductAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,20 @@ public function test_product_types_are_set_when_categories_defined() {
'product_cat',
[ 'parent' => $category_2['term_id'] ]
);
$product->set_category_ids( [ $category_1['term_id'], $category_3['term_id'] ] );
$category_4 = wp_insert_term(
'Charlie Category',
'product_cat',
[ 'parent' => $category_3['term_id'] ]
);
$category_5 = wp_insert_term( 'Delta Category', 'product_cat' );

$product->set_category_ids(
[
$category_1['term_id'], // Zulu
$category_3['term_id'], // Beta
$category_4['term_id'], // Charlie
]
);
$product->save();

$adapted_product = new WCProductAdapter(
Expand All @@ -545,9 +558,17 @@ public function test_product_types_are_set_when_categories_defined() {
'targetCountry' => 'US',
]
);

// Confirm the selected categories Zulu, Beta and Charlie are included (with their parent types).
$this->assertContains( 'Alpha Category > Beta Category', $adapted_product->getProductTypes() );
$this->assertContains( 'Alpha Category > Beta Category > Charlie Category', $adapted_product->getProductTypes() );
$this->assertContains( 'Zulu Category', $adapted_product->getProductTypes() );
$this->assertContains( 'Alpha Category', $adapted_product->getProductTypes() );

// Confirm Alpha is not included as it should only be included as a parent of Beta and Charlie.
$this->assertNotContains( 'Alpha Category', $adapted_product->getProductTypes() );

// Confirm unrelated category is not included.
$this->assertNotContains( 'Delta Category', $adapted_product->getProductTypes() );
}

public function test_images_are_set() {
Expand Down
Loading