【问题标题】:Magento : How to display selected custom option price in product detail page In price boxMagento:如何在产品详细信息页面中显示选定的自定义选项价格在价格框中
【发布时间】:2023-03-14 00:56:01
【问题描述】:

我想在产品详细信息页面的价格框中显示带有名称的自定义选项价格。

我也尝试了这个链接,但没有成功this is link i use

所以请给我建议任何解决方案。

【问题讨论】:

  • 我认为这个任务可以使用 javascript 完成
  • 嗯,感谢您的建议,目前我正在努力。我知道需要 js,因为我必须在选择它时显示选项。
  • @KeyurShah 我在最后一条评论中问你,请检查一下。

标签: php magento magento-1.7 customization


【解决方案1】:

首先你必须把按钮calculateprice

然后点击calculateprice你必须调用函数chkprice()

        function chkpice()
    {
        var a=document.getElementById("options_1_text").value; 
        var b=document.getElementById("options_2_text").value; 
        var c=document.getElementById("options_3_text").value; 
        var d=document.getElementById("options_4_text").value; 
        var e=<?php echo $_product = $this->getProduct()->getPrice()?>;       
        var f=(a+b+c+d)+e;
        var e=document.getElementById(('product-price-'+<?php echo $_product = $this->getProduct()->getId()?>)).innerHTML;
        $('product-price-'+<?php echo $_product = $this->getProduct()->getId()?>).innerHTML =''+e.replace(<?php echo $_product = $this->getProduct()->getPrice()?>,d)+'</span>';
    }

insted of options_1_text,options_2_text,options_3_text,options_4_text 输入你的 id 它会得到你的结果

【讨论】:

  • insted of options_1_text,options_2_text,options_3_text,options_4_text 输入你的 id 它会得到你的结果 但是 id 是由 magento 动态生成的,所以我不能把 id 放在这里。如果您检查 magento 中的自定义选项对于每个产品都有不同的 id,并且我在每个产品中都需要这个工具,所以我们不能把 id 放在这里。
  • 请在单个 magento 中创建两个产品并分配设置自定义选项并检查您的解决方案是否有效?我检查了它,它不起作用
  • 我已经在上面的评论中告诉过你,下拉列表 id 是由 magento 动态生成的。同样,如果产品 A 有 4 个下拉菜单,产品 B 有 4 个下拉菜单。如果我使用您的逻辑,则我必须设置 8 个 getElementById() 并且我有 5000 个产品,所以这对我来说是不可能的使用这种脚本。
  • 您是否针对两个以上的产品检查过此解决方案?
【解决方案2】:

首先您执行负责更新费用的功能:

function updateCharges(){
   var tmpText="";
   $("input[type='select']").each(function{
   tmpText+=$("#"+this.id+"option:selected").text()+"<br>";    
   });
   $("#detailDiv").html(tmpText)
}

然后你只需将选择绑定到该函数

$("input[type='select']").change(updateCharges)

现在您只需确保在模板中包含&lt;div id="detailDiv"&gt;&lt;/div&gt;

我将使用上述代码创建一个自定义块并将其放置在产品详细信息页面中。此外,您应该检查使用的选择器,它们会在页面上寻找绝对所有的选择,所以这不是您可能想要的。但这只是萤火虫的问题,直到找到合适的选择器

【讨论】:

    【解决方案3】:

    最近我需要类似的东西。或许对你有帮助。

    方块类:

    class Foo_Bar_Block_Baz extends Mage_Catalog_Block_Product_View {    
        protected function getOptionDataCollection($options) {
            $optionDataCollection = array();
            foreach ($options as $option) {
                $optionDataCollection[$option->getData('option_id')] = array_filter($option->getData());
            }
            return $optionDataCollection;
        }
    
        protected function getOptionValueDataCollection($options) {
            $optionValueDataCollection = array();
            foreach ($options as $option) {
                $optionType = $option->getType();
                if ($optionType == 'drop_down') {
                    $optionValues = $option->getValues();
                    foreach ($optionValues as $valueItem) {
                        // here you could also use the option_type_id (in my case  I needed the sku)
                        $optionValueDataCollection[$valueItem->getData('sku')] = array_filter($valueItem->getData());
                    }
                } else {
                    //Mage::throwException('Unexpected input. Processing for this optionType is not implemented');
                }
            }
            return $optionValueDataCollection;
        }
    
        public function getOptionsJson() {
            $data = array(
                'options' => array(),
                'optionValues' => array()
            );
            $options = $this->getProduct()->getOptions();
            array_push($data['options'], $this->getOptionDataCollection($options));
            array_push($data['optionValues'], $this->getOptionValueDataCollection($options));
            $optionsJson = json_encode($data);
    
            return $optionsJson;
        }
    }
    

    模板

    <script id="optionsJson" type="application/json">
        <?php echo $this->getOptionsJson(); ?>
    </script>
    

    JS

    var json = JSON.parse(document.getElementById("optionsJson").innerHTML),
        options = json.options[0],
        optionValues = json.optionValues[0];
    
        optionValues['<optionValueSKU>'].default_title
        optionValues['<optionValueSKU>'].price
    

    【讨论】:

      【解决方案4】:
              -----------Create controller-------------
      
               <?php
      class Magento_Guys_IndexController extends Mage_Core_Controller_Front_Action
      {
          public function indexAction()
          {
              echo "Thank you !";
          }
           public function genCartAction()
          {
              $id = $this->getRequest()->getParam('pid');
              $_product = Mage::getModel('catalog/product')->load($id);
              $buy = Mage::helper('checkout/cart')->getAddUrl($_product);
              echo $qty = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
      
              //echo $id;
              //echo $this->getRequest()->getParam('id');;
          }
      }
      ?>
      
          ----------------Change Add to cart code-------------------
      
          <?php if ($_product->isAvailable()): ?>
                    <b class="available_quanity" style="display: none">Available Quantity :</b> <span id="simplestock" class="simplestock">Please select a color to view the quantity</span>
                  <?php endif; ?>
                  </div>
                  <?php if(!$_product->isGrouped()): ?>
                  <div class="qty">
                      <label for="qty"><?php echo $this->__('Qty:') ?></label>
                        <?php $i = 0; ?>
                        <select id="qty" class="input-text" name="qty" style="width:50px;">
                          <?php while($i<=(int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()):?>
                            <option value="<?php echo $i; ?>"><?php echo $i++; ?></option>
                          <?php endwhile; ?>
                        </select>
                  </div>
                  <?php endif; ?>
      
          ----------------Add Ajax for get Quantity---------------
      
          <script>
          jQuery(document).ready(function() {
              jQuery(".product-options select[id^='attribute']").on('change', function() {
                  var id = getSimpleProductId();
                  var qty = "";
                  jQuery('.available_quanity').show();
                  jQuery("#fancybox-loading").show();
                  jQuery.ajax({      
                      type: "POST",
                      data: 'pid=' + id,
                      url:'https://www.thewirelesscircle.com/guys/index/genCart',
                      success:function(response){ 
                          if (response) {
                              qty = response;
                              var x = document.getElementById("qty");
                              var i;
                              removeOptions(x);
                              for(i=1;i<=qty;i++) {
                                  var option = document.createElement("option");
                                  option.text = i;
                                  option.value = i;
                                  x.add(option);
                              }
                          }
                          jQuery("#fancybox-loading").hide();
                      }
                  });
      
              });
          });
      
          </script>
      
          ---------------Get Selected Option Id in Magento-------------------
      
          <script type="text/javascript">
              function removeOptions(selectbox)
              {
                  var i;
                  for(i = selectbox.options.length - 1 ; i >= 0 ; i--)
                  {
                      selectbox.remove(i);
                  }
              }
              function getSimpleProductId() {
                  var productCandidates = [];
                  jQuery.each(spConfig.settings, function (selectIndex, select) {
                      var attributeId = select.id.replace('attribute', '');
                      var selectedValue = select.options[select.selectedIndex].value;
      
                      jQuery.each(spConfig.config.attributes[attributeId].options, function(optionIndex, option) {
      
                          if (option.id == selectedValue) {
                              var optionProducts = option.products;
      
                              if (productCandidates.length == 0) {
                                  productCandidates = optionProducts;
                              } else {
                                  var productIntersection = [];
                                  jQuery.each(optionProducts, function (productIndex, productId) {
                                      if (productCandidates.indexOf(productId) > -1) {
                                          productIntersection.push(productId);
                                      }
                                  });
                                  productCandidates = productIntersection;
                              }
                          }
                      });
                  });
                  return (productCandidates.length == 1) ? productCandidates[0] : null;
              }
          </script>
      

      【讨论】:

      • 欢迎来到 Stack Overflow!请不要只用源代码回答。尝试对您的解决方案如何工作提供一个很好的描述。请参阅:How do I write a good answer?。谢谢
      【解决方案5】:

      打开页面:app\code\core\Mage\Catalog\Block\Product\View\Options.php找到protected function _getPriceConfiguration($option)函数并在return $data;语句之前添加$data['title'] = $option-&gt;getTitle();

      现在,打开app\design\frontend\YOUT_TEMPLATE_PATH\default\template\catalog\product\view\options.phtml

      在页面顶部添加&lt;div id="optiontitle"&gt;&lt;/div&gt;

      然后找到这个:

      $A(element.options).each(function(selectOption){
          if ('selected' in selectOption && selectOption.selected) {
              if (typeof(configOptions[selectOption.value]) != 'undefined') {
                  curConfig = configOptions[selectOption.value];
      
              }
          }
      });
      

      替换为:

      $A(element.options).each(function(selectOption){
          if ('selected' in selectOption && selectOption.selected) {
              if (typeof(configOptions[selectOption.value]) != 'undefined') {
                  curConfig = configOptions[selectOption.value];
                  optionTitle = curConfig.title + "<br>";
                  $("optiontitle").insert(optionTitle);
              }
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-14
        • 1970-01-01
        • 2015-06-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多