Search code examples
magento

{Magento} Advanced Options created how to set for each product in code


We have an advanced product option created called Courses. It contains all the fields that we want the user to enter information when ordering the product.

We have an external process that imports our courses into our store using this shortened code: ($data contains the course information to be inserted.)

$sku = $data['EventCode'];
$productId = Mage::getModel('catalog/product')->getIdBySku($sku);
if(!$productId) {
    $product = Mage::getModel('catalog/product');
    $product->setWebsiteIds(array(1));
    $product->setData('entity_type_id', '10');
    $product->setData('attribute_set_id', 4);
    $product->setData('type_id', 'virtual');
    $product->setData('created_at', strtotime('now'));
    $product->setData('updated_at', strtotime('now'));
    $product->setTaxClassId(1);
    $product->setStatus(1);
    $product->setData('manage_stock', 1);
    $product->setData('visibility', 1);
    $product->setData('has_options', 0);  // If this product has options, set to 1
    $product->setStockData(array('is_in_stock' => 1));
} else {
    // Product was found, so load it so it can be updated.
    $product = Mage::getModel('catalog/product')->load($productId);
} // if(!$productId)
$ename = "Unknown course name";
$product->setName($ename);
$product->setPrice($data['Price']);
$product->setDescription('Course Item');
$product->setShortDescription('Course Item');
$product->setData('is_online_course', ($data['LocState'] == 'OL' ? 1 : 0));
$product->setSku($sku);
$cat_id[] = 9;
$product->setData('category_ids', $cat_id);
$product->save();

How would I set each product to use the advanced option Courses in the code above? Note: All options in Courses are required.


Solution

  • You can use product Attributes API.

    <?php
       $proxy = new SoapClient('http://domain/api/soap/?wsdl');
       $sesId = $proxy->login('apiUser', 'apiKey');
       $attributeSet = $proxy->call($sessId, 'product_attribute_set.list');
       $attributeOptions = $proxy->call($sessId, 'product_attribute.options', array('attribute_id'=>'PRODUCT_ATTRIBUTE_INT_ID'));
       // change PRODUCT_ATTRIBUTE_INT_ID with your attribute id
    
    ?>