How to Programmatically Manipulate Products¶
Prerequisites¶
The Akeneo PIM project introduces a service to help you manage your product entities. This part of the application extends the FlexibleEntityBundle.
As stated above, the product manager is a service you can get from the symfony container:
$this->container->get('pim_catalog.manager.product');
In the following examples, we will use $pm
as the product manager object.
Create an Attribute¶
- Create a text attribute
// create an attribute
$attribute = $pm->createAttribute('pim_catalog_text');
$attribute->setCode('title');
$pm->getStorageManager()->persist($attribute);
$pm->getStorageManager()->flush();
- Create a simple select attribute
In some cases, you will want to restrain values to a list of possibilities for an attribute. For instance, this example creates a color attribute with a list of predefined options:
$att = $pm->createAttribute('pim_catalog_simpleselect');
$att->setCode('color');
$opt1 = $pm->createAttributeOption();
$opt1->setCode('purple');
$att->addOption($opt1);
$opt2 = $pm->createAttributeOption();
$opt2->setCode('yellow');
$att->addOption($opt2);
$opt3 = $pm->createAttributeOption();
$opt3->setCode('blue');
$att->addOption($opt3);
- Create a simple select attribute with localizable values
Keeping the color example, the value of the option purple is “Purple” in English and “Violet” in French.
$opt1 = $pm->createAttributeOption();
$opt1->setCode('purple');
$opt1->setLocalizable(true);
$opt1EN = $pm->createAttributeOptionValue();
$opt1EN->setLocale('en_US');
$opt1EN->setValue('Purple');
$opt1->addOptionValue($opt1EN);
$opt1FR = $pm->createAttributeOptionValue();
$opt1FR->setLocale('fr_FR');
$opt1FR->setValue('Violet');
$opt1->addOptionValue($opt1FR);
$att->addOption($opt1);
- Create a localizable attribute
// create a localizable attribute
$attribute = $pm->createAttribute('pim_catalog_text');
$attribute->setCode('name');
$attribute->setLocalizable(true);
- Create a scopable attribute
// create a scopable attribute
$attribute = $pm->createAttribute('pim_catalog_text');
$attribute->setCode('image_hd');
$attribute->setScopable(true);
- Create a localizable AND scopable attribute
// create a localizable and scopable attribute
$attribute = $pm->createAttribute('pim_catalog_textarea');
$attribute->setCode('short_description');
$attribute->setScopable(true);
$attribute->setLocalizable(true);
Create a Product¶
// create a product
$product = $pm->createProduct();
Enrich a Product¶
- Create a new value
$productValue = $pm->createProductValue();
$productValue->setAttribute($mySkuAttribute);
$product->addValue($productValue);
- Update a text Value
$product->getValue('sku')->setData('akeneo-001');
$product->getValue('title')->setData('My product title');
$productValue = $product->getValue('sku');
$sku = (string) $product->getValue('sku')->getData();
- Update an Option Value
$product->getValue('color')->setOption($opt1);
echo $product->getValue()->getData(); // returns [purple]
- Update a Localized Value
A product can have different values depending of the locale. With the locales en_US and fr_FR already existing:
$product->getValue('name', 'en_US')->setData('My name');
$product->getValue('name', 'fr_FR')->setData('Mon nom');
echo $product->getValue('name')->getData(); // returns "My name"
$product->setLocale('fr_FR');
echo $product->getValue('name')->getData(); // returns "Mon nom"
- Set Scopable Value
Akeneo PIM is a multi-channel application so you can define different scopes to use. We the channels (scope) ecommerce and mobile already existing:
$product->getValue('image_hd', null, 'ecommerce')->setData('my_ecommerce_image');
$product->getValue('image_hd', null, 'mobile')->setData('my_mobile_image');
$product->setScope('ecommerce');
$product->getValue('image_hd')->getData(); // returns "my_ecommerce_image"
$product->setScope('mobile');
$product->getValue('image_hd')->getData(); // returns "my_mobile_image"
- Set Localizable and Scopable Value
$product->getValue('short_description', 'en_US', 'ecommerce')->setData('Ecommerce and en_US');
$product->getValue('short_description', 'en_US', 'mobile')->setData('Mobile and en_US');
$product->getValue('short_description', 'fr_FR', 'ecommerce')->setData('Ecommerce et fr_FR');
$product->getValue('short_description', 'fr_FR', 'mobile')->setData('Mobile et fr_FR');
$product->setLocale('en_US');
$product->setScope('ecommerce');
echo $product->getValue('short_description'); // returns "Ecommerce and en_US"
Get a Product¶
$product = $pm->find($myProductId);