-
Notifications
You must be signed in to change notification settings - Fork 214
Updated variation-product function #3095
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
base: develop
Are you sure you want to change the base?
Conversation
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@tests/php/src/Factories/ProductFactory.php`:
- Around line 135-149: In create_variation_product, initialize $product_ids as
an array and merge the variation children into it so each child ID is an integer
(e.g. $product_ids = []; $product_ids[] = $variable_product->get_id();
$product_ids = array_merge( $product_ids, $variable_product->get_children() );),
replacing the current array_push( $product_ids,
$variable_product->get_children() ) which nests the children array; this ensures
the foreach over $product_ids in create_variation_product passes scalar post IDs
to wp_update_post and avoids an uninitialized variable notice.
| public function create_variation_product( $product = null ) { | ||
| return WC_Helper_Product::create_variation_product( $product ); | ||
| $variable_product = WC_Helper_Product::create_variation_product( $product ); | ||
| $product_ids[] = $variable_product->get_id(); | ||
|
|
||
| array_push( $product_ids, $variable_product->get_children() ); | ||
|
|
||
| foreach ( $product_ids as $product_id ) { | ||
| if ( $this->seller_id ) { | ||
| $post_data = [ | ||
| 'ID' => $product_id, | ||
| 'post_author' => $this->seller_id, | ||
| ]; | ||
|
|
||
| wp_update_post( $post_data ); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix nested IDs array and initialize $product_ids.
array_push( $product_ids, $variable_product->get_children() ) adds the children array as a single element, so the loop passes an array to wp_update_post, and child authors won’t be updated. Also, $product_ids is uninitialized and emits a notice.
🧩 Proposed fix
- $variable_product = WC_Helper_Product::create_variation_product( $product );
- $product_ids[] = $variable_product->get_id();
-
- array_push( $product_ids, $variable_product->get_children() );
-
- foreach ( $product_ids as $product_id ) {
- if ( $this->seller_id ) {
- $post_data = [
- 'ID' => $product_id,
- 'post_author' => $this->seller_id,
- ];
-
- wp_update_post( $post_data );
- }
- }
+ $variable_product = WC_Helper_Product::create_variation_product( $product );
+ $product_ids = array_merge(
+ array( $variable_product->get_id() ),
+ $variable_product->get_children()
+ );
+
+ if ( $this->seller_id ) {
+ foreach ( $product_ids as $product_id ) {
+ $post_data = [
+ 'ID' => $product_id,
+ 'post_author' => $this->seller_id,
+ ];
+
+ wp_update_post( $post_data );
+ }
+ }🤖 Prompt for AI Agents
In `@tests/php/src/Factories/ProductFactory.php` around lines 135 - 149, In
create_variation_product, initialize $product_ids as an array and merge the
variation children into it so each child ID is an integer (e.g. $product_ids =
[]; $product_ids[] = $variable_product->get_id(); $product_ids = array_merge(
$product_ids, $variable_product->get_children() );), replacing the current
array_push( $product_ids, $variable_product->get_children() ) which nests the
children array; this ensures the foreach over $product_ids in
create_variation_product passes scalar post IDs to wp_update_post and avoids an
uninitialized variable notice.
All Submissions:
Changes proposed in this Pull Request:
Related Pull Request(s)
Closes
How to test the changes in this Pull Request:
Changelog entry
Title
Detailed Description of the pull request. What was previous behaviour
and what will be changed in this PR.
Before Changes
Describe the issue before changes with screenshots(s).
After Changes
Describe the issue after changes with screenshot(s).
Feature Video (optional)
Link of detailed video if this PR is for a feature.
PR Self Review Checklist:
FOR PR REVIEWER ONLY:
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.