Adding Product Variation Values WooCommerce CRUD
-
I am writing a plugin for a client that will connect to their internal API and sync all of their products to their WooCommerce WordPress website. There are approx 94 000 products to sync.
I have nailed down the code that creates the actual product and the variations which I am quite happy with. So the theory behind my code is to import all 94k products to a tmp table on the WordPress site, I will then Import the products from the tmp table into WooCommerce. Doing it this way allows me to isolate any API errors from WooCommerce errors.
So the theory is that I will loop the tmp table, and for each entry in tmp table i will create the product.
Now what if I come across a product that has already been created and I need to add a variation to it? Well sure I will just add the variation. But what about the attributes, I need to add the new color and size values to the product as well AND THIS is where I am getting stuck. Now I could probably get all possible values for this product when creating it, and that will add all of the values, but if a product has 13 variations then that means that I will be doing 12 unnecessary DB calls because the data will already be there. I could try propagating it while its being loaded into the import table but then I am bogging down the API.
This code doesn’t seem to update the attribute values and I can’t see why not? Any help will be appreciated. <3
This is the sample code that is not working, its more of a proof of concept that is not working
$product = wc_get_product(714); $productAttributes = $product->get_attributes(); foreach ( $productAttributes as $attribute) { $attributeOptions = $attribute->get_options(); $attributeOptions[] = 'Blue'; $attributeOptions[] = 'Yellow'; $attribute->set_options($attributeOptions); $product->set_attributes([$attribute]); $product->save(); }
This is the full function that handles the creation of the product
public static function importToWoo( omProduct $productModel ): bool|int { $myClientAPIProductId = $productModel->getProductId(); $WCProduct = self::getWooProductBymyClientAPIProductId($myClientAPIProductId); if($productModel->getProductName() === "Camo Stationery Set 15cm Full col"){ error_log("Something to trigger a break point"); } // If parent doesn't exist, create it if ($WCProduct === false) { $WCProduct = new WC_Product_Variable(); $WCProduct->set_name($productModel->getProductName()); $WCProduct->set_sku($productModel->getCode()); $WCProductId = $WCProduct->save(); update_post_meta($WCProductId, 'omProductId', $myClientAPIProductId); } else $WCProductId = $WCProduct->get_id(); // one available for variation attribute $productAttributes = $WCProduct->get_attributes(); $colourAttribute = null; // Does the color attribute exist? foreach($productAttributes as $productAttribute){ $attributeName = $productAttribute->get_name(); if($attributeName === "Colour") $colourAttribute = $productAttribute; if(!empty($colourAttribute)) break; } // If the color attribute exists, check if the color variation exists if(!empty($colourAttribute)){ $colourAttributeOptions = $colourAttribute->get_options(); $productColourVariationExists = false; foreach ( $colourAttributeOptions as $colour_attribute_option ) { if ( $colour_attribute_option === $productModel->getColour() ) { $productColourVariationExists = true; break; } } if(!$productColourVariationExists){ $colourAttributeOptions[] = $productModel->getColour(); $colourAttribute->set_options($colourAttributeOptions); $WCProduct->set_attributes([$colourAttribute]); $WCProduct->save(); } } else { // Create the color Product Attribute $attribute = new WC_Product_Attribute(); $attribute->set_name( 'Colour' ); $attribute->set_id(0); $attribute->set_options( [$productModel->getColour()] ); $attribute->set_position( 0 ); $attribute->set_visible( true ); $attribute->set_variation( true ); $WCProduct->set_attributes( [$attribute] ); $WCProduct->save(); } // Create the variation // Get the color attributes taxonomy $variation = new WC_Product_Variation(); $variation->set_parent_id( $WCProductId ); $variation->set_attributes( [ 'attribute_colour' => $productModel->getColour() ] ); $variation->set_regular_price( $productModel->getSellingPrice() ); $variation->save(); return true; }
- The topic ‘Adding Product Variation Values WooCommerce CRUD’ is closed to new replies.