Monday 9 November 2020

Creating Woocommerce products pragmatically from json data, Variation product not working

Product variation not working, woocommerce version 4.6.2 Using zapiex API to fetch aliexpress product.

Frontend screenshot enter image description here

Backend screenshot enter image description here

code is working for creating new products with attributes and variations. all are working fine except the variation doesn't set properly. Please check the screenshot of the frontend and backend. You will find the API json sample aliexpress data from the zapiex documentation.

Any help is appreciated.

Code:

$response = $data['data'];
$shipping = $response["shipping"]["carriers"];

if ($response["hasVariations"]){
    $objProduct = new WC_Product_Variable();
} else{
    $objProduct = new WC_Product();
}

$objProduct->set_name($response["title"]);
$objProduct->set_status("publish");  // can be publish,draft or any wordpress post status
$objProduct->set_catalog_visibility('visible'); // add the product visibility status
$objProduct->set_description($response["htmlDescription"]);
$objProduct->set_sku($response["productId"]); //can be blank in case you don't have sku, but You can't add duplicate sku's
$objProduct->set_manage_stock(true); // true or false
$objProduct->set_stock_quantity($response["totalStock"]);
$objProduct->set_stock_status('instock'); // in stock or out of stock value
$objProduct->set_backorders('no');
$objProduct->set_reviews_allowed(true);
$objProduct->set_sold_individually(false);

if($response['hasSinglePrice']){
    $objProduct->set_regular_price($response['price']['web']['originalPrice']['value']); 
    if($response['price']['web']['hasDiscount']){
        $objProduct->set_sale_price($response['price']['web']['discountedPrice']['value']);
    }
}


$objProduct->set_category_ids(array(recentSearchCatID()));
// set attributes
$attributes= array();
$i= 0;
if($response['hasAttributes']){
    
    foreach($response["attributes"] as $attr){
        $att = array('id'=>0, 'name'=>$attr['name'], 'options'=>array($attr['value']['name']), 'visible'=> true, 'position'=>$i, 'variation'=>false );
        $attributes[]= create_attributes($att);
        $i++;
    }

}
if($response['hasProperties']){
    
    foreach($response["properties"] as $attr){
        
        $att = array('id'=>0, 'name'=>$attr['name'], 'visible'=> true, 'position'=>$i, 'variation'=>true );
        $options = array();
        foreach($attr['values'] as $opt){
            $options[]=$opt['name'];
        }
        $att['options']=$options;
        $attributes[]= create_attributes($att);
        $i++;
    }
    
}

$objProduct->set_attributes( $attributes );
$product_id = $objProduct->save();
if($response['hasVariations']) {
    foreach($response['variations'] as $vp) {
        $variation = new WC_Product_Variation();
        $variation->set_parent_id( $product_id );
        $props = array();
        foreach($vp['properties'] as $vpp){
            $props['pa_'.wc_sanitize_taxonomy_name($vpp['name'])] = wc_sanitize_taxonomy_name($vpp['value']['name']);
        }
        
        $variation->set_attributes($props);
        $variation->set_status('publish');
        $variation->set_sku($vp['sku']);
        
        if (array_key_exists("price",$vp)){
            $regularPrice = round($vp["price"]["web"]["originalPrice"]["value"]);
            $variation->set_regular_price($regularPrice);
            $discountPrice = NULL;
            if ($vp["price"]["web"]["hasDiscount"]){
                $discountPrice = round($vp["price"]["web"]["discountedPrice"]["value"]);
                $variation->set_sale_price($discountPrice);
            }
        }
        
        $variation->set_stock_status();
        $variation->set_stock_quantity(10);
        $variation->save();
        $product = wc_get_product($product_id);
        $product->save();
    }                    
    
}

exit();


from Creating Woocommerce products pragmatically from json data, Variation product not working

No comments:

Post a Comment