Jump to content

Add combinations from script


Aegidius

Recommended Posts

  • 8 months later...

Hello there

I agree Aegidius and want to type sometings more about the Prestashop classes as a software developer:

 

Current class is too much complicated and bad (I'll explain) for understand.

For example If you want create a php code (by using Prestashop native class and functions) to insert, update or import products with attributes, you have to 

  1. Check attribute group exists in DB (there is not a function search by name, you must create a great mysql query )
  2. If not exists you must create it add(); function of AttributeGroup class
  3. Check attribute exists in DB (there is not a function search by name too)
  4. If not exists you must create it by add(); function of Attribute class
  5. If you want to use this attribute in a product combination must use $product->addCombinationEntity() (there is no information about parameters and function)
  6. Get id from 5. step
  7. Assign the combination class with the id
  8. use $combination->setAttributes()
  9. use StockAvailable::setProductDependsOnStock() (There is not enough information about thşs function too)
  10. use StockAvailable::setProductOutOfStock() (There is not enough information about thşs function too)
  11. use StockAvailable::setQuantity();
  12. use AttributeGroup::cleanDeadCombinations();
  13. use Product::SetDefaultAttribute();
  14. use Product::save();
  15. ...
  16. ..

This is just for a product insert without product variables and functions.If you successfully type a code with this crazy methods and run the code, you will see not working as usually.

 

We need a documentation about this class at least more comment lines at codes

  • Like 4
Link to comment
Share on other sites

  • 7 months later...

Hello there

I agree Aegidius and want to type sometings more about the Prestashop classes as a software developer:

 

Current class is too much complicated and bad (I'll explain) for understand.

For example If you want create a php code (by using Prestashop native class and functions) to insert, update or import products with attributes, you have to 

  1. Check attribute group exists in DB (there is not a function search by name, you must create a great mysql query )
  2. If not exists you must create it add(); function of AttributeGroup class
  3. Check attribute exists in DB (there is not a function search by name too)
  4. If not exists you must create it by add(); function of Attribute class
  5. If you want to use this attribute in a product combination must use $product->addCombinationEntity() (there is no information about parameters and function)
  6. Get id from 5. step
  7. Assign the combination class with the id
  8. use $combination->setAttributes()
  9. use StockAvailable::setProductDependsOnStock() (There is not enough information about thşs function too)
  10. use StockAvailable::setProductOutOfStock() (There is not enough information about thşs function too)
  11. use StockAvailable::setQuantity();
  12. use AttributeGroup::cleanDeadCombinations();
  13. use Product::SetDefaultAttribute();
  14. use Product::save();
  15. ...
  16. ..

This is just for a product insert without product variables and functions.If you successfully type a code with this crazy methods and run the code, you will see not working as usually.

 

We need a documentation about this class at least more comment lines at codes

I have this problem i create a module that import products from amazon to prestasho with all atributes and i can use only classe product-> add, only because the another classes are so complex.

Link to comment
Share on other sites

  • 1 year later...

Hy there. I managed to do a script that creates combinations for my products, with attributes and stuff. It goes like this:

function combination($id_product, $arr, $lang_id){
	$id_product_attribute = Combination::getIdByReference($id_product, $arr['code']);
	$comb = new Combination($id_product_attribute);
	if($id_product_attribute){
		StockAvailable::setQuantity($id_product, $id_product_attribute, $arr['quantity']);
	} else {

		$id_atribute = array();
		if($arr['size_group_id']){
			$id_attribute_size = attribute_id($arr['size_group_id'], $arr['sizes'], $lang_id);
			$id_attribute[] = $id_attribute_size;
		}
		if($arr['color_group_id']){
			$id_attribute_color = attribute_id($arr['color_group_id'], $arr['color'], $lang_id);
			$id_attribute[] = $id_attribute_color;
		}
		if(count($id_attribute)){
			$comb->quantity = $arr['quantity'];
			$comb->id_product = $id_product;
			$comb->reference = $arr['code'];
			$comb->add();
			$comb->setAttributes($id_attribute);
		}
		if (isset($arr['supplyer']) && $arr['supplyer'] == 'Supplyer') {
			
			$id_product_attribute = Combination::getIdByReference($id_product, $arr['code']);
			$comb = new Combination($id_product_attribute);
			
			if ($arr['combination_image'] != '') {
					$combination_image = $arr['combination_image'][0];
					product_img($id_product, $combination_image);
			}

			if($id_product_attribute){
				StockAvailable::setQuantity($id_product, $id_product_attribute, $arr['quantity']);
				
				$comb->default_on = 1;
				$comb->update();
			}
		}
	}
}

The lang_id can be set like this:

$defaultLanguage = new Language((int)(Configuration::get('PS_LANG_DEFAULT')));
$languages = Language::getLanguages();
$lang_id = $defaultLanguage->id;

Hope this helps.

  • Like 3
Link to comment
Share on other sites

  • 8 months later...

Hello, my solution is a following: 

 

1. You need the 'id_attribute' from the 'attribute_lang' table

 

After: 

 

       $product = new Product( $product->id );
            $combinationAttributes = array();
            $combinationAttributes[] = 9;   //id_attribute
            $idProductAttribute = $product->addProductAttribute(
                (float)0, //price 
                (float)0, //weight
                0,        //unit_impact
                null ,    //ecotax
                (int)0,   //quantity
                "",       //id_images
                "" , //reference
                strval(""), //suppliers
                strval(""), //ean13
                NULL, //default 
                NULL,  //location
                NULL  //upc
                );
                $product->addAttributeCombinaison($idProductAttribute,                     $combinationAttributes);     
Link to comment
Share on other sites

  • 2 months later...
  • 2 weeks later...

I But the function 

 

$id_attribute_color = attribute_id($arr['color_group_id'], $arr['color'], $lang_id);

 

where is?

 

Thank You

 

 

The function is a Prestashop function. To use PS functions, you call like this:

include_once 'config/config.inc.php';
include(dirname(__FILE__).'/init.php');
Link to comment
Share on other sites

  • 5 months later...
On 18/9/2017 at 10:04 AM, tony_mik said:

 

 

The function is a Prestashop function. To use PS functions, you call like this:


include_once 'config/config.inc.php';
include(dirname(__FILE__).'/init.php');

I get error on this, telling function not defined.

I tried to search in the entire prestashop code but didn't found a reference to that function.

Someone can help me?

Link to comment
Share on other sites

  • 2 years later...

Hi, I use this in Prestashop 1.7.X in the header of the file to access all functions

    require_once(dirname(__FILE__) . '/../../../config/config.inc.php');

    require_once(dirname(__FILE__) . '/../../../init.php');

Link to comment
Share on other sites

  • 2 months later...

Hi, if u want import all functions use only:

 

require_once(dirname(__FILE__) . '/../../../config/config.inc.php'); <- have all functions

 

If u have doubts about the folder location use that in header file :

    ini_set('display_errors', 1);

    ini_set('display_startup_errors', 1);

    error_reporting(E_ALL);

 

Then PHP can show you errors or warnings.

Link to comment
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...