Magento Customize Attributes with PHP

I created new custom products and linked their simple products with PHP.

Now, when I edit any custom product, I see this screen:

Magento Select Configurable Attributes screen in admin

So, in the absence of any Magento documentation, what do I call in PHP to perform the same function as the screen above programmatically?

I saw $configurable_product->setConfigurableProductsData() used in some examples, but I donโ€™t think this is what I need.

+4
source share
1 answer

You are right, you are creating a relationship / relationship between custom and child products, but what happens when you create your custom product, you are not setting setConfigurableAttributesData strong>, which basically sets up the superapplication information for this custom product.

  foreach($configAttrCodes as $attrCode){ $super_attribute= Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product',$attrCode->code); $configurableAtt = Mage::getModel('catalog/product_type_configurable_attribute')->setProductAttribute($super_attribute); $newAttributes[] = array( 'id' => $configurableAtt->getId(), 'label' => $configurableAtt->getLabel(), 'position' => $super_attribute->getPosition(), 'values' => $configurableAtt->getPrices() ? $configProduct->getPrices() : array(), 'attribute_id' => $super_attribute->getId(), 'attribute_code' => $super_attribute->getAttributeCode(), 'frontend_label' => $super_attribute->getFrontend()->getLabel(), ); } $existingAtt = $product->getTypeInstance()->getConfigurableAttributes(); if(empty($existingAtt) && !empty($newAttributes)){ $configProduct->setCanSaveConfigurableAttributes(true); $configProduct->setConfigurableAttributesData($newAttributes); $configProduct->save(); } 

This is a small snippet that should get you there, let me know if you have any questions or need more help.

+11
source

Source: https://habr.com/ru/post/1386755/


All Articles