Jump to content
plune

Ajouter un produit via un script PHP

Recommended Posts

Bonjour,

 

J'essaye de mettre en place une création de produit dans un de mes scripts mais je suis bloqué concernant deux points : 

 

L'ajout de catégories multiple et l'ajout de caractéristiques (Clé, valeur).

Voilà, ce que j'ai :

foreach ($products->prod as $product)
	{
		echo $product->const_ref . '<br>';

		$prod = new Product();
		$prod->reference = (string)$product->const_ref;
		$prod->weight = (float)$product->poids;
		$prod->name = array((int)Configuration::get('PS_LANG_DEFAULT') => (string)$product->lib);
		$prod->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') => Tools::link_rewrite((string)$product->lib));
		$prod->description = (string)$product->descr_l;
		$prod->description_short = (string)$product->descr_c;
		$prod->id_tax_rules_group = 0;
		$prod->quantity = 5000;
		$prod->redirect_type = '404';
		$prod->indexed = 1;
		$quantity = 1000;

		$fb = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('SELECT fb FROM `'._DB_PREFIX_.'frais_bancaire` WHERE ' . $product->prx_ht . ' BETWEEN min AND max');

		$price = ($product->prx_ht * 1.196 + $product->prx_taxe + $fb) + ($product->prx_public - ($product->prx_ht * 1.196 + $product->prx_taxe + $fb)) * 20 / 100;
		if($price >= $product->prx_public)
			$prod->price = $product->prx_public;
		else
			$prod->price = $price;	

		$cat = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('SELECT id_category AS id, link_rewrite FROM `'._DB_PREFIX_.'category_lang` WHERE meta_title = "' . $product->category . '"');
		$prod->id_category_default = $cat['id'];
		$prod->category = $cat['link_rewrite'];
		$prod->add();

		StockAvailable::setQuantity ($prod->id, false, $quantity);
		
		foreach ($product->caracts->caract as $feat)
		{
			$id_feature = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('SELECT id_feature FROM `'._DB_PREFIX_.'feature_lang` WHERE name = "' . (string)$feat->lib . '"');
			
			if(!isset($id_feature) || empty($id_feature))
			{
				$feature = new Feature();
				$feature->name = array((int)Configuration::get('PS_LANG_DEFAULT') => (string)$feat->lib);
				$feature->add();

				$id_feature = $feature->id;
			}

			$id_value = $prod->addFeaturesToDB($id_feature, 0, 1);
			$prod->addFeaturesCustomToDB($id_value, 1, (string)$feat->val);
		}
		
		print_r($prod);
		die();
	}

EDIT : J'ai réussi pour les caractéristiques (features), je l'ai ajouté au code.

Edited by plune (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More