0

i want to add custom option for all product say ring size:7,6,5,4,3

i want to show if for all product types with attribute set as ring , i tried adding as custom option but in this way we have to add for all products , any easy way too show it as drop down fro all product

Bascially it drop down and user have to select it

  • 1
    Maybe you should look into configurable products. – Lord Skeletor Jan 15 '15 at 9:16
1

Hi you can do this by magento event observer.try trigger an event on catalog_product_prepare_save observer check product attribute set is ring

Study about Creating Custom Option on Product Save

On This function you need check your attribute set for product is ring and your custom option is exit on not

See some logic:

$exit=false;

    $options = $product->getOptions();
    if ($options){
        foreach ($options as $option) {
            $optionType = $option->getType();
        if ($optionType == 'drop_down' && $option->getTitle()=='Ring' ) :
            $exit=true;
            break;
        endif;

        }
    }

    if($exit==false):
    $newOption = array(
        'title' => "Ring",
        'type' => 'drop_down',
        'is_require' => 1,
        'sort_order' => 20,
        'values' => array(
            array(
                'title' => "7",
                'price' => 42.00,
                'price_type' => 'fixed',
                'sku' => "",
                'sort_order' => '1'
            ),
            array(
                'title' => "6",
                'price' => 50,
                'price_type' => 'percent',
                'sku' => "",
                'sort_order' => '2'
            )
        )
    );

    //don't use it like this because it has no effect
    //$product->setProductOptions($options);
    $product->setCanSaveCustomOptions(true);
    //use it this way. Populate `$product->getOptionInstance()` directly
    $product->getOptionInstance()->addOption($newOption);
    //don't forget to state that the product has custom options
    $product->setHasOptions(true);

    endif;
  • i there any way via magento admin – user1799722 Jan 15 '15 at 6:00
  • please use magento api... – Amit Bera Jan 15 '15 at 6:05
  • magento api ??.. – user1799722 Jan 15 '15 at 6:08
  • yes...usign api you need save magento product – Amit Bera Jan 15 '15 at 6:13
1

When I cannot add some product options using configurable product, I create the custom option in one product, and use the following to duplicate it to another product:

class Company_Module_Model_Resource_Catalog_Product_Option extends Mage_Catalog_Model_Resource_Product_Option
{

    /**
     * Duplicate custom options from productA to productB
     *
     * @param int|string $productA sku or id
     * @param int|string $productB sku or id
     * @param string|array $optionTitle the title to duplicate    
     * @return array
     */
    public function duplicateOptions($productA, $productB, $optionTitle)
    {
        if (!is_int($productA)) {
            $productIdA = Mage::getResourceSingleton('catalog/product')->getIdbySku($productA);
        }
        if (!is_int($productB)) {
            $productIdB = Mage::getResourceSingleton('catalog/product')->getIdbySku($productB);
        } 

        $write  = $this->_getWriteAdapter();
        $read   = $this->_getReadAdapter();
        $option = Mage::getModel('catalog/product_option');

        $optionsCond = array();
        $optionsData = array();

        $cond = is_array($optionTitle) ?
                    '`t2`.`title` IN (?)' :
                    '`t2`.`title` = ?';
        $select  = $read->select()
            ->from(array('t1'=>$this->getMainTable()))
            ->joinLeft(array('t2'=>$this->getTable('catalog/product_option_title')),
                        "`t1`.`option_id` = `t2`.`option_id`",
                        null)
            ->where($cond, $optionTitle)
            ->where('`t1`.`product_id` = ?', $productIdA);
        $query = $read->query($select);

        while ($row = $query->fetch()) {
            $optionsData[$row['option_id']] = $row;
            $optionsData[$row['option_id']]['product_id'] = $productIdB;
            unset($optionsData[$row['option_id']]['option_id']); // auto optionIdB when insert
        }

        // insert option to product B
        foreach ($optionsData as $optionIdA => $data) {
            $write->insert($this->getMainTable(), $data);
            $optionsCond[$optionIdA] = $write->lastInsertId($this->getMainTable()); // optionIdB
        }

        // insert option_type title, price, and value to product B
        foreach ($optionsCond as $optionIdA => $optionIdB) {
            // title
            $table = $this->getTable('catalog/product_option_title');

            $select = $read->select()
                ->from($table, array(new Zend_Db_Expr($optionIdB), 'store_id', 'title'))
                ->where('option_id = ?', $optionIdA);

            $insertSelect = $write->insertFromSelect(
                $select,
                $table,
                array('option_id', 'store_id', 'title'),
                Varien_Db_Adapter_Interface::INSERT_ON_DUPLICATE
            );
            $write->query($insertSelect);

            // price
            $table = $this->getTable('catalog/product_option_price');

            $select = $read->select()
                ->from($table, array(new Zend_Db_Expr($optionIdB), 'store_id', 'price', 'price_type'))
                ->where('option_id = ?', $optionIdA);

            $insertSelect = $write->insertFromSelect(
                $select, 
                $table,
                array('option_id', 'store_id', 'price', 'price_type'),
                Varien_Db_Adapter_Interface::INSERT_ON_DUPLICATE
            );
            $write->query($insertSelect);

            $option->getValueInstance()->duplicate($optionIdA, $optionIdB);
        }

        return $optionsData;
    }        
}

To call it:

$result = Mage::getResourceSingleton('company/module_product')->duplicateOptions($sourceProductId, $targetProductId, 'Ring Size'); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.