Skip to content
Open
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
18 changes: 17 additions & 1 deletion tests/php/src/Factories/ProductFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,23 @@ public function create_grouped_product() {
* @return WC_Product_Variable
*/
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 );
}
Comment on lines 135 to +149
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

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.

}

return $variable_product;
}

/**
Expand Down
Loading