• Resolved jacqueswarp

    (@jacqueswarp)


    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;
    }
    • This topic was modified 1 year, 5 months ago by jacqueswarp.
    • This topic was modified 1 year, 5 months ago by jacqueswarp.
    • This topic was modified 1 year, 5 months ago by jacqueswarp. Reason: Because for some reason when adding a new block, if i hit enter then it submits the form?!?!?!?!??
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi Jacques,

    Thanks for raising this question about syncing a large product catalog.

    When looking through your code, I was wondering if you have considered using Woo’s REST API for this purpose?

    Creating a custom table is possible of course. But Woo’s REST API could help you to simplify your approach.

    This would be the relevant documentation to create a product variation via the REST API.

    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.

    I understand that you’re trying to add new attribute values to the parent product. Is it just about adding a new value or are you trying to add new attributes too?

    Hello !

    I have a problem with the WooCommerce API, could anyone help me with this? See below:

    Product assembly with Variations via API => I posted the “Product attributes” method to create the Color and Size group, then I created “Product attributes terms” with the colors and sizes, linking with the Color and Size group that I created previously using the “Product attributes” method, until Everything is ok, it shows correctly in the WordPress panel, but then in the “Create a product variation” method, where I enter the JSON for example “attributes”: [{“id”: 6,”option”: “Black”} , where id is of the group created in “Product attributes” and the option is the value created by the other method “Product attribute terms”, and in Post and Put, it returns OK, but when viewing the product later, it does not show the variations. Remembering that in the main product, I entered the type attribute as variable instead of simple , that is, I first created the “Parent” product with type = variable attribute using the “Products” method, which gave me its correct ID and then using the “” method Product variations”, I created pointing to the Parent ID, so much so that when I do a GET in the “Products” method, in the “variations” JSON, it shows the product ID array created by the “Product variations” method. So where am I going wrong? Could anyone help me with this?

    I’m very grateful for the help, as I’ve been struggling with these two problems and nothing.

    • This reply was modified 1 year, 3 months ago by insertsoft.
    Saif

    (@babylon1999)

    Hello @insertsoft,

    In order to align with?forum best practices, please?start your own topic, so that we can address your issue(s) separately.

    Tank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Adding Product Variation Values WooCommerce CRUD’ is closed to new replies.