• Resolved Bhanot

    (@bhanot)


    public static function create_variable_product(){
    		$variant_data = wp_clean(isset($_POST['data']) ? wp_unslash($_POST['data']) : '');
    		$product_id = wp_clean(isset($_POST['product_id']) ? wp_unslash($_POST['product_id']) : '');
    		$product = wc_get_product($product_id);
    		if($product->has_child() == false){
    			$product = new WC_Product_Variable($product_id);
    		}
    		$attribute = self::create_product_attribute($product,$variant_data);
    		
    		wp_send_json($attribute);
    	}
    	
    public static function create_product_attribute($product,$variant_data){
    		
    		$id = [];
    		for($i=0; $i<count($variant_data); $i++){
    			$attribute = new WC_Product_Attribute();
    			$attribute->set_id(0);
    			foreach($variant_data[$i] as $key => $value){
    				
    				if($key == 'attribute_name'){
    					$attribute->set_name($value);
    				}
    				if($key == 'options'){
    					$attribute->set_options($value);
    				}
    	
    			}	
    				$attribute->set_position( 1 );
    
    				$attribute->set_visible( 1 );
    
    				$attribute->set_variation( 1 );
    				$attribute->is_taxonomy(0);
    
    				$product->set_attributes(array($attribute));
    				array_push($id,$product->save());
    			
    		}
    		return $id;
    }

    The data being passed in $_POST[‘data’] is

    [{
      attribute_name: 'Color',
      options: ['red','yellow','blue']
    }]
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi there!

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers!

    Thread Starter Bhanot

    (@bhanot)

    
    //credit for solving this https://stackoverflow.com/users/3730754/loictheaztec
    public static function create_variable_product(){
        $data       = wp_clean(isset($_POST['data'] ? wp_unslash($_POST['data']) : '');
        $product_id = wp_clean(isset($_POST['product_id']) ? wp_clean( wp_unslash($_POST['product_id']) ) : '');
        $product    = wc_get_product($product_id);
        
        if( is_a($product, 'WC_Product') && ! $product->is_type('variable') && isset($_POST['data']) ){
            $product = new WC_Product_Variable($product_id);
        }
        
        $attribute = self::create_product_attribute($product, $data);
        $product->save()
        
        wp_send_json($attribute);
    }
    
    public static function create_product_attribute( $product, $data ) {
        $attributes = $product->get_attributes();
        $attribute  = new WC_Product_Attribute();
    
        foreach($variant_data[$i] as $key => $values){
            if( $key === 'attribute_name'){
                $attribute->set_name($values);
            } elseif( $key === 'options' ){
                $attribute->set_options($values);
            }
        }
        $attribute->set_position( 1 );
        $attribute->set_visible( 1 );
        $attribute->set_variation( 1 );
        $attribute->is_taxonomy(0);
        
        $attributes[$attribute->get_name()] = $attribute;
    
        $product->set_attributes($attributes);
    }
    //credit for solving this https://stackoverflow.com/users/3730754/loictheaztec
    
    • This reply was modified 4 years ago by Bhanot.
    • This reply was modified 4 years ago by Bhanot.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WooCommerce Products attribute overwrites over older attributes when saved’ is closed to new replies.