Jump to content

Edit History

drasl

drasl

Done. The solution:

foreach ($product_array['tags_array'] as $lang => $lang_tags){				
	Tag::addTags($lang, $product->id, $lang_tags);
}

Now I update my code if someone need it:

	public function createProduct($product_array){
		/******** Example argument $product_array ********/
		/*
		$product_array['ean13'] = 9999999;		
		$product_array['name'] = [3 => 'Prueba producto',4 => 'Prueba producto portugal'];			
		$product_array['link_rewrite'] = [3 => 'prueba_producto', 4 => 'prueba_producto_portugal'];
		$product_array['id_category'] = [2,3];
		$product_array['id_category_default'] = 2;
		$product_array['redirect_type'] = '404';
		$product_array['wholesale_price'] = 0;
		$product_array['price'] = 0;
		$product_array['id_tax_rules_group'] = 9;
		$product_array['quantity'] = 2;
		$product_array['minimal_quantity'] = 1;
		$product_array['show_price'] = 1;
		$product_array['active'] = 0;
		$product_array['online_only'] = 1;
		$product_array['meta_keywords'] = 'test';
		$product_array['meta_description'] = 'test';
		$product_array['is_virtual'] = 0;
		$product_array['weight'] = 2;
		$product_array['reference'] = 'ASD';
		$product_array['id_manufacturer'] = 1;
		$product_array['images'][] = array(true,'url'); // $cover, $url
		$product_array['images'][] = array(false,'url'); // $cover, $url
		// Variables de combinación 
		$product_array['combinations'][1]['id_image'] = 1; //Imágen del producto correspondiente a esta combinación
		$product_array['combinations'][1]['wholesale_price'] = 0;
		$product_array['combinations'][1]['price'] = 5;
		$product_array['combinations'][1]['weight'] = 3;
		$product_array['combinations'][1]['quantity'] = 2;
		$product_array['combinations'][1]['reference'] = 'referencia_combinacion';
		$product_array['combinations'][1]['ean13'] = '1234567890123';
		$product_array['combinations'][1]['default_on'] = true;
		$product_array['combinations'][1]['upc'] = "";
		$product_array['combinations'][1]['minimal_quantity'] = 1;
		// Atributos de combinación 
		$product_array['combinations'][1]['combination_attributes'][] = 25;
		$product_array['combinations'][1]['combination_attributes'][] = 26;
		*/		
		
		//Creating the product	
		Shop::setContext(Shop::CONTEXT_ALL);
		$error = "";
		
		$product = new Product();
		$product->ean13					= $product_array['ean13'];		
		$product->name 		    		= $product_array['name'];			
		$product->link_rewrite  		= $product_array['link_rewrite'];
		$product->id_category 			= $product_array['id_category'];
		$product->id_category_default   = $product_array['id_category_default'];
		$product->redirect_type 		= $product_array['redirect_type'];
		$product->wholesale_price		= $product_array['wholesale_price'];
		$product->price 				= $product_array['price'];
		$product->id_tax_rules_group	= $product_array['id_tax_rules_group'];
		$product->quantity 				= $product_array['quantity'];
		$product->minimal_quantity 		= $product_array['minimal_quantity'];
		$product->show_price 			= $product_array['show_price'];
		$product->active 				= $product_array['active'];
		$product->online_only			= $product_array['online_only'];
		$product->meta_keywords 		= $product_array['meta_keywords']; 
		$product->meta_description 		= $product_array['meta_description']; 
		$product->is_virtual 			= $product_array['is_virtual']; 
		$product->weight				= $product_array['weight'];
		$product->referende 			= $product_array['reference'];
		$product->id_manufacturer		= $product_array['id_manufacturer'];				

		if ($product->add()){
			$product->updateCategories($product->id_category);
			StockAvailable::setQuantity((int)$product->id, 0, $product->quantity, Context::getContext()->shop->id);
			//Añado los tags				
			foreach ($product_array['tags_array'] as $lang => $lang_tags){				
				Tag::addTags($lang, $product->id, $lang_tags);
			}

			//Crea imágenes
			foreach ($product_array['images'] as $product_image){	
				$cover = $product_image[0];
				$url = $product_image[1];
				$id_product = $product->id;
				$image = new Image();
				$image->id_product = $id_product;
				$image->position = Image::getHighestPosition($id_product) + 1;
				$image->cover = $cover;
				if (($image->validateFields(false, true)) === true && ($image->validateFieldsLang(false, true)) === true && $image->add()){
				    $image->associateTo(Context::getContext()->shop->id);
				    if (!$this->copyImg($id_product, $image->id, $url, 'products')){
				        $image->delete();
				        $error .= "<li>Ha ocurrido un fallo con la imágen</li>";
				    }
				}
			}

			//Creo combinaciones			
			$id_shop_list = array(1,2);			
            $id_product_attribute = $product->addCombinationEntity(
                (float)$product_array['combinations'][1]['wholesale_price'],
                (float)$product_array['combinations'][1]['price'],
                (float)$product_array['combinations'][1]['weight'],
                0,
                0,
                (int)$product_array['combinations'][1]['quantity'],
                $product_array['combinations'][1]['id_image'],
                strval($product_array['combinations'][1]['reference']),
                0,
                strval($product_array['combinations'][1]['ean13']),
                (int)$product_array['combinations'][1]['default_on'],
                0,
                strval($product_array['combinations'][1]['upc']),
                (int)$product_array['combinations'][1]['minimal_quantity'],
                $id_shop_list                
            );		

            // Si la combinación se ha creado correctamente...
            if ($product->productAttributeExists($id_product_attribute)){
            	//Creo atributos
            	if (!$product->addAttributeCombinaison($id_product_attribute, $product_array['combinations'][1]['combination_attributes'])){
            		$error = "<li>Ocurrió un problem al crear el/los atributo/s para la combinacón";
            	}else{
            		StockAvailable::setQuantity((int)$product->id, $id_product_attribute, (int)$product_array['combinations'][1]['quantity'], Context::getContext()->shop->id);
            	}
            }else{
            	$error .= "<li>Ocurrió un error al crear la combinación!";
            }
			
			echo "Producto con ID: ".$id_product." creado correctamente!";
		}else{
			$error .= "<li>Ha ocurrido un error al crear el producto";
		}

		// Si no hay errores, creo producto en tienda remota.
		if ($error != ""){	
			return true;
		}else{
			return $error;
		}

	}

 

drasl

drasl

Done. The solution:

foreach ($product_array['tags_array'] as $lang => $lang_tags){				
	Tag::addTags($lang, $product->id, $lang_tags);
}

Now I update my code if someone need it:

	public function createProduct($product_array){
		/******** Example argument $product_array ********/
		/*
		$product_array['ean13'] = 9999999;		
		$product_array['name'] = [3 => 'Prueba producto',4 => 'Prueba producto portugal'];			
		$product_array['link_rewrite'] = [3 => 'prueba_producto', 4 => 'prueba_producto_portugal'];
		$product_array['id_category'] = [2,3];
		$product_array['id_category_default'] = 2;
		$product_array['redirect_type'] = '404';
		$product_array['wholesale_price'] = 0;
		$product_array['price'] = 0;
		$product_array['id_tax_rules_group'] = 9;
		$product_array['quantity'] = 2;
		$product_array['minimal_quantity'] = 1;
		$product_array['show_price'] = 1;
		$product_array['active'] = 0;
		$product_array['online_only'] = 1;
		$product_array['meta_keywords'] = 'test';
		$product_array['meta_description'] = 'test';
		$product_array['is_virtual'] = 0;
		$product_array['weight'] = 2;
		$product_array['reference'] = 'ASD';
		$product_array['id_manufacturer'] = 1;
		$product_array['images'][] = array(true,'https://mimale.com/4333-large_default/dingo-adult-daily.jpg'); // $cover, $url
		$product_array['images'][] = array(false,'https://mimale.com/5505-large_default/hill-s-prescription-diet-canine-jd-.jpg'); // $cover, $url
		// Variables de combinación 
		$product_array['combinations'][1]['id_image'] = 1; //Imágen del producto correspondiente a esta combinación
		$product_array['combinations'][1]['wholesale_price'] = 0;
		$product_array['combinations'][1]['price'] = 5;
		$product_array['combinations'][1]['weight'] = 3;
		$product_array['combinations'][1]['quantity'] = 2;
		$product_array['combinations'][1]['reference'] = 'referencia_combinacion';
		$product_array['combinations'][1]['ean13'] = '1234567890123';
		$product_array['combinations'][1]['default_on'] = true;
		$product_array['combinations'][1]['upc'] = "";
		$product_array['combinations'][1]['minimal_quantity'] = 1;
		// Atributos de combinación 
		$product_array['combinations'][1]['combination_attributes'][] = 25;
		$product_array['combinations'][1]['combination_attributes'][] = 26;
		*/		
		
		//Creating the product	
		Shop::setContext(Shop::CONTEXT_ALL);
		$error = "";
		
		$product = new Product();
		$product->ean13					= $product_array['ean13'];		
		$product->name 		    		= $product_array['name'];			
		$product->link_rewrite  		= $product_array['link_rewrite'];
		$product->id_category 			= $product_array['id_category'];
		$product->id_category_default   = $product_array['id_category_default'];
		$product->redirect_type 		= $product_array['redirect_type'];
		$product->wholesale_price		= $product_array['wholesale_price'];
		$product->price 				= $product_array['price'];
		$product->id_tax_rules_group	= $product_array['id_tax_rules_group'];
		$product->quantity 				= $product_array['quantity'];
		$product->minimal_quantity 		= $product_array['minimal_quantity'];
		$product->show_price 			= $product_array['show_price'];
		$product->active 				= $product_array['active'];
		$product->online_only			= $product_array['online_only'];
		$product->meta_keywords 		= $product_array['meta_keywords']; 
		$product->meta_description 		= $product_array['meta_description']; 
		$product->is_virtual 			= $product_array['is_virtual']; 
		$product->weight				= $product_array['weight'];
		$product->referende 			= $product_array['reference'];
		$product->id_manufacturer		= $product_array['id_manufacturer'];				

		if ($product->add()){
			$product->updateCategories($product->id_category);
			StockAvailable::setQuantity((int)$product->id, 0, $product->quantity, Context::getContext()->shop->id);
			//Añado los tags				
			foreach ($product_array['tags_array'] as $lang => $lang_tags){				
				Tag::addTags($lang, $product->id, $lang_tags);
			}

			//Crea imágenes
			foreach ($product_array['images'] as $product_image){	
				$cover = $product_image[0];
				$url = $product_image[1];
				$id_product = $product->id;
				$image = new Image();
				$image->id_product = $id_product;
				$image->position = Image::getHighestPosition($id_product) + 1;
				$image->cover = $cover;
				if (($image->validateFields(false, true)) === true && ($image->validateFieldsLang(false, true)) === true && $image->add()){
				    $image->associateTo(Context::getContext()->shop->id);
				    if (!$this->copyImg($id_product, $image->id, $url, 'products')){
				        $image->delete();
				        $error .= "<li>Ha ocurrido un fallo con la imágen</li>";
				    }
				}
			}

			//Creo combinaciones			
			$id_shop_list = array(1,2);			
            $id_product_attribute = $product->addCombinationEntity(
                (float)$product_array['combinations'][1]['wholesale_price'],
                (float)$product_array['combinations'][1]['price'],
                (float)$product_array['combinations'][1]['weight'],
                0,
                0,
                (int)$product_array['combinations'][1]['quantity'],
                $product_array['combinations'][1]['id_image'],
                strval($product_array['combinations'][1]['reference']),
                0,
                strval($product_array['combinations'][1]['ean13']),
                (int)$product_array['combinations'][1]['default_on'],
                0,
                strval($product_array['combinations'][1]['upc']),
                (int)$product_array['combinations'][1]['minimal_quantity'],
                $id_shop_list                
            );		

            // Si la combinación se ha creado correctamente...
            if ($product->productAttributeExists($id_product_attribute)){
            	//Creo atributos
            	if (!$product->addAttributeCombinaison($id_product_attribute, $product_array['combinations'][1]['combination_attributes'])){
            		$error = "<li>Ocurrió un problem al crear el/los atributo/s para la combinacón";
            	}else{
            		StockAvailable::setQuantity((int)$product->id, $id_product_attribute, (int)$product_array['combinations'][1]['quantity'], Context::getContext()->shop->id);
            	}
            }else{
            	$error .= "<li>Ocurrió un error al crear la combinación!";
            }
			
			echo "Producto con ID: ".$id_product." creado correctamente!";
		}else{
			$error .= "<li>Ha ocurrido un error al crear el producto";
		}

		// Si no hay errores, creo producto en tienda remota.
		if ($error != ""){	
			return true;
		}else{
			return $error;
		}

	}

 

×
×
  • Create New...