2010-08-02 30 views
5

'da kod aracılığıyla açılan özel seçenekleri ekleyin, bir ürün eklendiğinde otomatik olarak özel seçenekler eklemek zorunda kaldım, kod düzgün çalışıyor ancak seçenekler içeren bir açılır menü oluşturmam gerekiyor ve seçeneklerin nasıl ekleneceğini bilmiyorum aşağı oluşturulan damla, benim kod ben 'type' => 'drop_down' oluşturduk ama nasıl seçenekleri ekleyebilirsinizMagento

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; 
} 

}

mı? Seçenekleri nasıl ekleyeceğimi bilmiyorum ve herhangi bir yardım çok takdir edilecek.

sayesinde

cevap

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> 

sevgili bu kod iştir ve bunu kontrol edin. Ancak bu kod, bu özel seçeneği mağazanızdaki tüm ürünlerle birlikte ekleyecektir. sıralama ordar ve fiyat ve seçenek değerlerini ve ayrıca açılan adı değiştirebilirsiniz. Bence işini daha iyi. Eğer bir başlık ile Mage_Catalog_Model_Product_Option_Value eklenmesi ve ile ilişkilendirmiş olursunuz Görüldüğü gibi

2
public function Add_CustomOptions_Automatically($observer) { 
    $product = $observer->getEvent()->getProduct(); 

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

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

     // Answer starts here 
     $value = Mage::getModel('catalog/product_option_value'); 
     $value->setOption($option) 
       ->setTitle('Hello world!'); 
     $option->addValue($value); 
     // Answer ends here 

     $option->save(); 
     $product->addOption($option); 
    endif; 
} 

$option Oluşturduğunuz. Ayrıca bir SKU'ya, fiyat ve sıralama düzenine sahip olabilir. Bu şekilde istediğiniz kadar çok değer oluşturun.

4

Seçenek dizisine bir değer dizisi sağlayabilirsiniz, bu seçenek için eklenir. aşağıdaki gibi:

$product = Mage::getModel('catalog/product'); 
$product->load(200); // product id here 

$opt = array(
    'is_delete'   => 0, 
    'is_require'  => false, 
    'previous_group' => '', 
    'title'    => 'New Example Option', 
    'type'    => 'drop_down', 
    'price_type'  => 'fixed', 
    'price'    => '20.0000', 
    'sort_order'  => 0, 
    /** array of values for this option **/ 
    'values'   => array(
     array(
      'is_delete'  => 0, 
      'title'   => 'Option One Here', 
      'price_type' => 'fixed', 
      'price'   => 999, 
      'sku'   => 'test-sku-here', 
      'option_type_id'=> -1, 
     ), 
     array(
      'is_delete'  => 0, 
      'title'   => 'Another Option', 
      'price_type' => 'fixed', 
      'price'   => 999, 
      'sku'   => 'another-sku-here', 
      'option_type_id'=> -1, 
    )), 
); 

$option = Mage::getModel('catalog/product_option') 
    ->setProduct($product) 
    ->addOption($opt) 
    ->saveOptions();