通过Magento中的代码添加自定义选项到下拉列表

jar*_*rus 5 magento

我不得不在添加产品时自动添加自定义选项,代码工作正常,但我需要创建一个带有选项的下拉菜单,我不知道如何添加选项到创建的下拉列表,我的代码是

public function Add_CustomOptions_Automatically($observer) {
    $product = $observer->getEvent()->getProduct();
    $save = false; if (!$product->getOptions()) $save = true;

    $optionData = array(
        'previous_group'    => 'text',
        'title'             => 'Size',
        'type'              => 'drop_down',
        'is_require'        => 0,
        'sort_order'        => 0,
        'price'             => 0,
        'price_type'        => 'fixed');    


    if($save):
        $product->setHasOptions(1)->save();
        $option = Mage::getModel('catalog/product_option')
                    ->setProductId($product->getId())
                    ->setStoreId($product->getStoreId())           
                    ->addData($optionData);

        $option->save();
        $product->addOption($option);
    endif;
}
Run Code Online (Sandbox Code Playgroud)

}

我已创建,'type' => 'drop_down'但如何添加选项?我不知道如何添加选项,任何帮助将非常感激.

谢谢,

小智 0

<!DOCTYPE html PUBLIC "-W3CDTD XHTML 1.0 TransitionalEN" "http:www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http:www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<pre>
<?php
$res = mysql_pconnect('localhost', 'your data base name', 'passward');
mysql_select_db('your database name');
mysql_query("SET NAMES 'utf8';", $res);
mysql_query("SET CHARACTER SET 'utf8';", $res);
$query = 'select entity_id from catalog_product_entity';
$res = mysql_query($query);
$products=array();
while ($ret = mysql_fetch_array($res)) {
  $products[]=$ret[0];
  $query = "UPDATE catalog_product_entity set has_options=1 where entity_id=$ret[0]";
  //echo "$query<br>";
  //$res1 = mysql_query($query);
}
echo "Set all products for has_options in catalog_product_entity.<br>";
//$res = mysql_query('DELETE from catalog_product_option');
//$res = mysql_query('DELETE from catalog_product_option_title');
//$res = mysql_query('DELETE from catalog_product_option_type_price');
//$res = mysql_query('DELETE from catalog_product_option_type_title');
//$res = mysql_query('DELETE from catalog_product_option_type_value');
echo "Deleted all rows from catalog_product_option* tables.<br>";

$ress=array();
foreach ($products as $product){
  $query = "insert into catalog_product_option (product_id,type,is_require,image_size_x,image_size_y,sort_order) values ($product,'drop_down',0,0,0,0)";
  echo "$query<br>";
  $res1 = mysql_query($query);
  $ress[] = array("option_id" => mysql_insert_id());
}
echo '<pre>';
print_r($ress);


echo "Populated catalog_product_option.<br>";
$option_titles=array('en_US'=> 'Warrenty' );// here change title of option titles #Optional Coating

$res = mysql_query('SELECT * FROM `core_config_data` WHERE path="general/locale/code"');
while ($ret = mysql_fetch_array($res)) {
  $stores[$ret['value']][]=$ret['scope_id'];
}

$res = mysql_query('select * from catalog_product_option');// get all product here which agains data inserted

$sort_orders=array(
                 1,
                 2,
                 3,
                 4);

//while ($ret = mysql_fetch_array($res)) {
foreach ($ress as $key => $ret) 
{
    //echo '<br>'.$ret[$key]["option_id"];
  foreach($stores as $locale=>$scopes){
      foreach($scopes as $scope){
          $query = "insert into catalog_product_option_title (option_id,store_id,title) values ($ret[option_id],$scope,'$option_titles[$locale]')";
          echo "$query<br>";
         $res1 = mysql_query($query);
      }
  }
  foreach($sort_orders as $order){
      $query = "insert into catalog_product_option_type_value (option_id,sort_order) values ($ret[option_id],$order)";
      echo "$query<br>";
      $res1 = mysql_query($query);
  }
}

echo "Populated catalog_product_option_title.<br>";
echo "Populated catalog_product_option_type_value.<br>";
$prices=array(
            0.00,//Standard (12 months)
            29.95,//Silver (12 months +loan phone + pic up)
            59.95,//Gold(12 months)
            89.95//Platinum (24 months +loan phone + pic up)
            );
$option_type_titles=array('en_US'=>array(
                                        'Standard (12 months).',
                                        'Silver (12 months +loan phone + pic up).',
                                        'Gold(12 months).',
                                        'Platinum (24 months +loan phone + pic up).'
                                        )
                            );
$res = mysql_query('select * from catalog_product_option_type_value');
$i = 0;
$j = count($prices)-1;


while ($ret = mysql_fetch_array($res)) {
  foreach($stores as $locale=>$scopes){
      foreach($scopes as $scope){
          $query = "insert into catalog_product_option_type_price (option_type_id,store_id,price,price_type) values ($ret[0],$scope,$prices[$i],'fixed')";
          echo "$query<br>";
          $res1 = mysql_query($query);
          $query = "insert into catalog_product_option_type_title (option_type_id,store_id,title) values ($ret[0],$scope,'{$option_type_titles[$locale][$i]}')";
          echo "$query<br>";
          $res1 = mysql_query($query);
      }
  }
  ($j==$i) ? $i= 0 : $i++ ;
}
echo "<br>Populated catalog_product_option_type_price.<br>";
echo "<br>Populated catalog_product_option_type_title.<br>";
?>
</pre>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

亲爱的,这段代码可以工作,我检查了它。但此代码在执行时会为您商店中的所有产品添加此自定义选项。您可以更改排序顺序、价格和选项值以及下拉列表名称。我认为它的工作更好。