2010-08-02 13 views
5

ho dovuto aggiungere automaticamente le opzioni personalizzate quando si aggiunge un prodotto, il codice funziona bene ma ho bisogno di creare un menu a discesa con opzioni e non so come aggiungere opzioni al Scendete giù creato, il mio codice èAggiungi opzioni personalizzate al menu a discesa in 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; 
} 

}

ho creato 'type' => 'drop_down' ma come posso aggiungere opzioni? Non ho idea di come aggiungere le opzioni e qualsiasi aiuto sarebbe molto apprezzato.

grazie,

risposta

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> 

cari questo codice è il lavoro e lo controllo. ma questo codice quando viene rimosso aggiungerà questa opzione personalizzata a tutti i prodotti nel tuo negozio. puoi cambiare l'ordine e il prezzo e i valori delle opzioni e anche il nome del menu a discesa. penso che funzioni meglio.

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

Come si può vedere si sta aggiungendo un Mage_Catalog_Model_Product_Option_Value con un titolo ed associazioni con l'$option si è creato. Può anche avere un SKU, prezzo e ordine. Crea quanti più valori vuoi in questo modo.

4

È possibile fornire una matrice di valori all'array di opzioni, che verrà quindi aggiunto all'opzione. come di seguito :-)

$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(); 
Problemi correlati