2011-11-13 21 views
12

ho creato un prodotto configurabile, ha tre possibilità: colore, dimensioni e stile.

Ora nella pagina del prodotto, ogni opzione ha il testo predefinito "Scegli un'Opzione ..." in discesa, ma voglio il testo dovrebbe essere "Scegli il colore", "Scegli le dimensioni" e "Seleziona lo stile ".

Ho modificato la funzione getJsonConfig() in app \ code \ Core \ Mage \ Catalog \ Blocco \ Visualizza \ Type \ Configurable.phpModificare il testo "Scegli un'opzione ..." sulla pagina del prodotto Magento

Da:

'chooseText'  => Mage::helper('catalog')->__('Choose an Option...'), 

A:

'chooseText'  => ('Select ').$attribute->getLabel(), 

E modifica la riga 39 del file frontend/base/default/template/catalog/product/view/type/options/configurable.phtml a:

<option><?php echo $this->__('Select ') ?><?php echo $_attribute->getLabel() ?></option> 

Ma il risultato non è buono, mostra sempre il testo "Scegli stile" in tre opzioni. Per favore, dammi un suggerimento per questo problema, grazie mille!

risposta

2

L'unico modo in cui penso è giusto modificare la classe javascript che popola i menu a discesa. Come possiamo vedere nella frontend/base/default/template/catalog/product/view/type/options/configurable.phtml che classe è:

<script type="text/javascript"> 
     var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>); 
    </script> 

Il file con classe necessaria si trova nella js/varien/product.js

Il luogo in cui primo tag <option> è configurato è:

fillSelect: function(element){ 
     var attributeId = element.id.replace(/[a-z]*/, ''); 
     var options = this.getAttributeOptions(attributeId); 
     this.clearSelect(element); 
     element.options[0] = new Option(this.config.chooseText, ''); 
     ... 

La variabile chooseText utilizzato lì sulla riga 368. Questa variabile è stata creata nella funzione getJsonConfig() in app/code/core/Mage/Catalog/Block/Product/View/Type/Configurable.php (stavi scavando nel modo giusto). È necessario modificare il javascript che ho descritto in precedenza per achive ciò che è necessario (in base a var attributeId è possibile assegnare opzioni con testo diverso a elementi necessari)

1

ho esteso classe Product.Config (metodo fillselect) di questi codice:

fillSelect: function(element){ 
        var attributeId = element.id.replace(/[a-z]*/, ''); 
        var options = this.getAttributeOptions(attributeId); 
        this.clearSelect(element); 
         element.options[0] = new Option('Select '+element.config.label,''); 
        ........ 

Va bene!

0

file di catalogo/prodotto/view/tipo/opzioni/configurable.phml

<?php 
$_product = $this->getProduct(); 
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes()); 
?> 
<?php if ($_product->isSaleable() && count($_attributes)):?> 
    <dl> 
    <?php foreach($_attributes as $_attribute): ?> 
     <?php 
      $_attributeId = $_attribute->getAttributeId(); 
      $_attributeInfo = Mage::getModel('eav/entity_attribute')->load($_attributeId); 
      $_attributeLabel = str_replace(' ','-',strtolower($_attributeInfo->getFrontendLabel())); 
     ?> 
     <dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>> 
      <div class="input-box"> 
       <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select kevin-black-<?php echo $_attributeLabel;?>"> 
        <option><?php echo $this->__('Select '.$_attributeLabel) ?></option> 
        </select> 
       </div> 
     </dd> 
    <?php endforeach; ?> 
    </dl> 
    <script type="text/javascript"> 
     var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>); 
     //Change Text follow attribute Label 
     function changeFristText(){ 
      <?php foreach($_attributes as $_attribute): ?> 
       <?php 
        $_attributeId = $_attribute->getAttributeId(); 
        $_attributeInfo = Mage::getModel('eav/entity_attribute')->load($_attributeId); 
        $_attributeLabel = str_replace(' ','-',strtolower($_attributeInfo->getFrontendLabel())); 
       ?> 
       var label = '<?php echo $_attributeLabel;?>'; 
       $$('select.kevin-black-'+label).each(function(elem){ 
        var options = elem.childElements(); 
        options[0].update('Select ' + label); 
       }); 
      <?php endforeach;?> 
     } 
    </script> 
<?php endif;?> 

e aggiungere una riga dopo riga changeFristText(); 171 (element.options[0] = new Option(this.config.chooseText, '');) nel file di js/varien/configurable.js

E ' per tutti gli attributi impostati.

1

Se si modifica solo il file configurabile.js
sarà solo cambiare prima selezionare quando caricamento della pagina
Quindi deve cambiare file modello
Ottenere file allegato per il test. (ho appena scrivo a una piccola estensione)

<?php 
$_product = $this->getProduct(); 
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes()); 
?> 
<?php if ($_product->isSaleable() && count($_attributes)):?> 
    <dl> 
    <?php foreach($_attributes as $_attribute): ?> 
     <?php 
     $_attributeId = $_attribute->getAttributeId(); 
     $_attributeInfo = Mage::getModel('eav/entity_attribute')->load($_attributeId); 
     $_attributeLabel = str_replace(' ','-',strtolower($_attributeInfo->getFrontendLabel())); 
     ?> 
      <dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>> 
       <div class="input-box"> 
        <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select kevin-black-<?php echo $_attributeLabel;?>"> 
        <option><?php echo $_attributeInfo->getFrontendLabel() ?></option> 
        </select> 
       </div> 
      </dd> 
     <?php endforeach; ?> 
    </dl> 
<script type="text/javascript"> 
    var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>); 
    //[email protected] Change Text follow attribute Label 
    function changeFristText(){ 
     <?php foreach($_attributes as $_attribute): ?> 
      <?php 
      $_attributeId = $_attribute->getAttributeId(); 
      $_attributeInfo = Mage::getModel('eav/entity_attribute')->load($_attributeId); 
      ?> 
      var label = '<?php echo $_attributeInfo->getFrontendLabel();?>'; 
      $$('select.kevin-black-'+label).each(function(elem){ 
       var options = elem.childElements(); 
       options[0].update(label); 
      }); 
     <?php endforeach;?> 
    } 
</script> 
<?php endif;?> 


in file : js/varien/configurable.js replace line 171 = element.options[0] = new Option(element.config.label, ‘’); 

E per tutti set di attributi .

4

Stavo cercando un modo più semplice per farlo. Non volevo estendere nessun file di base o andare in giro con l'estensione di JavaScript. Invece ho analizzato le impostazioni di JSON, aggiornati l'impostazione chooseText, e riconvertiti in JSON:


/~theme/default/template/catalog/product/view/type/options/configurable.phtml

<?php 
$jsonConfig = json_decode($this->getJsonConfig()); 
$jsonConfig->chooseText = 'Select..'; 
?> 

<script type="text/javascript"> 
    var spConfig = new Product.Config(<?php echo json_encode($jsonConfig); ?>); 
</script> 

Ulteriori informazioni e ulteriori esempi here.

0
<script type="text/javascript"> 
    <?php 
     $jsonConfig = $this->getJsonConfig(); 
     $jsonConfig = str_replace("Choose an Option...", "Select Size", $jsonConfig); 
    ?> 
    var spConfig = new Product.Config(<?php echo $jsonConfig; ?>); 
</script> 
11

La mia versione dello stesso problema. È necessario modificare solo modello catalogo/prodotto/view/tipo/opzioni/configurable.phtml:

<?php 
$_product = $this->getProduct(); 
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes()); 
?> 
<?php if ($_product->isSaleable() && count($_attributes)):?> 
    <dl> 
    <?php foreach($_attributes as $_attribute): ?> 
     <dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt> 
     <dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>> 
      <div class="input-box"> 
       <?php $chooseText = $this->__('Select %s', $_attribute->getLabel()); ?> 
       <select data-choose-text="<?php echo $chooseText; ?>" name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select"> 
        <option><?php echo $chooseText; ?></option> 
       </select> 
       </div> 
     </dd> 
    <?php endforeach; ?> 
    </dl> 
    <script type="text/javascript"> 
     Product.ConfigDefaultText = new Class.create(Product.Config, { 
      fillSelect: function($super, element) { 
       $super(element); 
       var chooseDefaultText = element.getAttribute('data-choose-text'); 
       $(element).options[0] = new Option(chooseDefaultText, ''); 
      } 
     }); 
     var spConfig = new Product.ConfigDefaultText(<?php echo $this->getJsonConfig() ?>); 
    </script> 
<?php endif;?> 

Nota (estratto da commenti) Se il valore predefinito selezionato capita di non essere "Seleziona% s ", sostituire

$(element).options[0] = new Option(chooseDefaultText, ''); 

con

$(element).options[0].innerHTML = chooseDefaultText; 
+1

ha funzionato perfettamente, e la modifica di un non -linea modello migliore è l'approccio migliore – seekay

+0

Seleziona la prima opzione non predefinita per il primo input di selezione. È questo? C'è un modo per impedirlo? – Justin

+0

@Justin Bene, non ho nessuna installazione di Magento a portata di mano, ma per quanto mi ricordo questo codice cambia solo la prima opzione in selectbox, che di solito è 'Scegli un'opzione'. Se è selezionata qualsiasi altra opzione, forse l'attributo configurabile ha un altro valore di opzione predefinito o ripristina il vecchio valore selezionato dall'utente qualche tempo fa dalla sessione o smth. –

1

risposta più semplice:

sostituire js/varien/configurable.js linea 172

element.options[0].innerHTML = 'Choose ' + this.config.attributes[attributeId].label; 
+1

Questo mi sembra logico perché il javascript traduce comunque qualsiasi cosa scrivo nel PHP, ma l'unico problema è che non funziona per me. – sklrboy

0

Questo ha funzionato per me su CE 1.8.1. Si basa sulla risposta di Shein e affronta l'opzione sbagliata che viene selezionata al caricamento. Fondamentalmente ho appena copiato/incollato il Product.Config. fillSelect() metodo da /js/varien/product.js. All'interno del codice incollato ho cambiato:

element.options[0].innerHTML = this.config.chooseText; 

a

element.options[0].innerHTML = element.config.label; 

Questo consente di mantenere product.js non modificato, e solo override del metodo. L'unico inconveniente è che qualsiasi futuro aggiornamento del core a quel metodo richiederà la migrazione.

Dal momento che il nuovo codice ottiene solo l'impostazione "etichetta", la-scelta-di testo Dati attributo non è necessario il tag select


<?php 
$_product = $this->getProduct(); 
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes()); 
?> 
<?php if ($_product->isSaleable() && count($_attributes)):?> 
    <dl> 
    <?php foreach($_attributes as $_attribute): ?> 
     <dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt> 
     <dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>> 
      <div class="input-box"> 
       <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select"> 
        <option><?php echo $_attribute->getLabel() ?></option> 
       </select> 
       </div> 
     </dd> 
    <?php endforeach; ?> 
    </dl> 
    <script type="text/javascript"> 
     Product.ConfigDefaultText = new Class.create(Product.Config, { 
      fillSelect: function (element) { 
       var attributeId = element.id.replace(/[a-z]*/, ''); 
       var options = this.getAttributeOptions(attributeId); 
       this.clearSelect(element); 
       element.options[0] = new Option('', ''); 
       element.options[0].innerHTML = element.config.label; 

       var prevConfig = false; 
       if (element.prevSetting) { 
        prevConfig = element.prevSetting.options[element.prevSetting.selectedIndex]; 
       } 

       if (options) { 
        var index = 1; 
        for (var i = 0; i < options.length; i++) { 
         var allowedProducts = []; 
         if (prevConfig) { 
          for (var j = 0; j < options[i].products.length; j++) { 
           if (prevConfig.config.allowedProducts 
            && prevConfig.config.allowedProducts.indexOf(options[i].products[j]) > -1) { 
            allowedProducts.push(options[i].products[j]); 
           } 
          } 
         } else { 
          allowedProducts = options[i].products.clone(); 
         } 

         if (allowedProducts.size() > 0) { 
          options[i].allowedProducts = allowedProducts; 
          element.options[index] = new Option(this.getOptionLabel(options[i], options[i].price), options[i].id); 
          element.options[index].config = options[i]; 
          index++; 
         } 
        } 
       } 
      } 
     }); 
     var spConfig = new Product.ConfigDefaultText(<?php echo $this->getJsonConfig() ?>); 
    </script> 
<?php endif;?> 
Problemi correlati