Wednesday, July 16, 2014

Magento auto add product custom options

// *Special note: make sure you check your sql table name, 
//  and change the option title from what i have below.

// rbanh special methods
private function rbanh_get_custom_option_id($product_id)
{
    $sql = "
        select 
            o.option_id 
        from 
            catalog_product_option o
        inner join
            catalog_product_option_title t on t.option_id = o.option_id
        where
            t.title = 'Shipping Description'
            and o.product_id = '".$product_id."'
        limit 1
            ";
    $data = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql);
    if (count($data) > 0)
    {
        return current($data[0]);
    }
    return false;
}

// rbanh special methods
private function rbanh_add_custom_option($product_id)
{
    // make the option active
    //$write = Mage::getSingleton('core/resource')->getConnection('core_write');
    //$write->query("update catalog_product_entity set has_options = 1 where entity_id='$product_id'"); 
    $write = Mage::getSingleton('core/resource')->getConnection('core_write');
    $sql  = "update catalog_product_entity set has_options = 1 where entity_id=?";
    $write->query($sql, $product_id); 
    
    $product = Mage::getModel('catalog/product');
    $product->load($product_id);
    $opt = Mage::getModel('catalog/product_option');
    $opt->setProduct($product);
    $option = array(
        'is_delete' => 0,
        'is_require' => false,
        'previous_group' => 'text',
        'title' => 'Shipping Description',
        'type' => 'field',
        'price_type' => 'fixed',
        'price' => '0.0000'
    );
    $opt->addOption($option);
    $opt->saveOptions();
    
    return $this->rbanh_get_custom_option_id($product_id);
}

A blogger

I am passionate blogger cum B.Tech. Computer engineering graduate. I love writing blog post. I spend my free time in writing blog post that will useful to everyone (including me). I have had some success making money blogging and want to help others do the same. I just figured that by creating a great and free resource a lot of links would follow – and they have. Some people ask me how they can repay me – which is not necessary - but for those wanting to show their appreciation, I just say linking to the article from their blog is the best compensation I could receive. Thanks for reading!

0 comments:

Post a Comment

Thanks for commenting. I will Reply you soon

 

Copyright @ 2013 PHP WORDPRESS MAGENTO .

Designed by Kcon Technosoft