cancel
Showing results for 
Search instead for 
Did you mean: 

add attribute product programmatically

add attribute product programmatically

hello everyone!

I would need to add the attribute "color" and "size" to a simple product that will later be associated with a configurable
this is the code I'm using, but unfortunately I can not add the attributes to product

 

foreach($prod->versioni->versione as $prodConfig) {
$datiConf = array();
foreach($prodConfig->riposta as $risposta){
if($risposta->attributes()->domanda.'' != 'CODORI'){
$datiConf[$risposta->attributes()->domanda.''] = $risposta;
if($risposta->attributes()->domanda == 'COL'){
$variantecolore = $risposta.'';
if(!in_array(85, $attribute)) {
$attribute[] = 85;
}
}
if($risposta->attributes()->domanda == 'MIS'){
$variantemisura = $risposta.'';
if(!in_array(130, $attribute)) {
$attribute[] = 130;
}
}
}
}
$skuConfig = $sku . '||' . $prodConfig->attributes()->cod;

$_product = $objectManager->create('\Magento\Catalog\Model\Product');
$_product->setName($titolo);
$_product->setStoreId(1);
$_product->setTypeId('simple');
$_product->setAttributeSetId(4);
$_product->setSku($skuConfig);
$_product->setCreatedAt(strtotime('now'));
$_product->setWebsiteIds(array(1));
$_product->setVisibility(1);
$_product->setCondition('nuovo');
$_product->setWeight($peso_netto);
$_product->setStatus(1);
$_product->setTaxClassId(2);
$_product->setManufacturer($marca_id);
$_product->setDescription($desc_est);
$_product->setShortDescription('<p></p>');
$_product->setStockData(
array(
'use_config_manage_stock' => 0, //'Use config settings' checkbox
'manage_stock' => 1, //manage stock
'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
'max_sale_qty' => 1000, //Maximum Qty Allowed in Shopping Cart
'is_in_stock' => 1, //Stock Availability
'qty' => 9999 //qty
)
);

$_product->save();

if ($price != '') {
$_product->setPrice($price);
}else{
$_product->setPrice(99999); //price in form 11.22
}

$_product = $model->loadByAttribute('sku', $skuConfig); //Ricarico il prodotto per gli attributi
$prodID = $_product->getID();

if ($i == 1) {
if( array_key_exists('COL', $datiConf) ){
foreach ($colvar as $keysc) {
if($keysc['cod'] == $variantecolore) {
$colvarvalue = $keysc['value'];
$sql = 'SELECT option_id FROM eav_attribute_option_value WHERE value=\''.$colvarvalue.'\'';
$result = $connection->fetchAll($sql);
$attColID = $result[0]['option_id'];
$_product = $stockRegistry->getStockItem($prodID);
$_product->setData('color', $attColID);
}
}
exit;
}
if( isset($datiConf['MIS']) ){
foreach ($misvar as $keysm) {
if($keysm['cod'] == $variantemisura) {
$misvarvalue = $keysm['value'];
$sql = 'SELECT option_id FROM eav_attribute_option_value WHERE value=\''.$misvarvalue.'\'';
$result = $connection->fetchAll($sql);
$attMisID = $result[0]['option_id'];
$_product = $stockRegistry->getStockItem($prodID);
$_product->setData('misura', $attMisID);
}
}
}
}

$_product->save();

if ($priceB2B != '') {
if ($i == 1) {
$sql = 'INSERT INTO catalog_product_entity_tier_price(entity_id,all_groups,customer_group_id,qty,value,website_id) VALUES(' . $prodID . ',0,4,1.0000,' . $priceB2B . ',0)';
$connection->query($sql);
}
}

if($attColID!='') {
$arr[] = array(
'label' => 'Color', //attribute label
'attribute_id' => '85', //attribute ID of attribute 'color' in my store
'value_index' => $attColID, //value of 'Green' index of the attribute 'color'
'is_percent' => '0', //fixed/percent price for this option
'pricing_value' => $price //value for the pricing
);
}

if($attMisID!='') {
$arr[] = array(
'label' => 'Misura', //attribute label
'attribute_id' => '130', //attribute ID of attribute 'color' in my store
'value_index' => $attMisID, //value of 'Green' index of the attribute 'color'
'is_percent' => '0', //fixed/percent price for this option
'pricing_value' => $price //value for the pricing
);
}

$configurableProductsData[$prodID.''] = array($arr); //[$prodID] = id of a simple product associated with this configurable
$associatedProductIds = $configurableProductsData[$prodID.'']; //Product Ids Of Associated Products
unset($_product);


can someone help me? Thanks in advance

1 REPLY 1

Re: add attribute product programmatically

EDIT: i see that if i run this code

if( array_key_exists('COL', $datiConf) ){
foreach ($colvar as $keysc) {
if($keysc['cod'] == $variantecolore) {
$colvarvalue = $keysc['value'];
$sql = 'SELECT option_id FROM eav_attribute_option_value WHERE value=\''.$colvarvalue.'\'';
$result = $connection->fetchAll($sql);
$attColID = $result[0]['option_id'];
$_product->setColor($attColID);
} 
}
}

the color is set well! but i still try to set "misura"

 

i tried with "$_product->setData('misura', $attMisID);" or "$_product->setMisura($attMisID);"

but nothing