doudongdang4483 2014-05-23 13:52
浏览 43
已采纳

用于可配置产品的Magento SCP Extension和单选按钮

I am using the extension Simple Configurable Products (SCP) for Magento version 1.8.1.0

What I am trying to achieve is have a configurable product (using SCP!) that displays the different options as radio buttons instead of a dropdown (default). Eventhough this does sound quite easy, I couldn't find a proper solution so far (there are a LOT of suggestions out there, none of which seems to work). I would love to solve this on the backend, even though I will at this point appreciate working front-end solutions just as much.

What does work in the configurable.phtml is spitting out the different custom options, set for the product, which looks something like this:

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', '#ATTRIBUTENAME#'); 
            foreach ($attribute->getSource()->getAllOptions(true) as $option) {
                echo /*$option['value'] . ' ' .*/ $option['label'] . "
";
            }

I could imagine this to be a starting point, even though I haven't thought it through.

What I can confirm as not-working, plugin-wise are those two:

I know this is a very specific question and that it is a possible duplicate of this, however there might even be working solutions for v1.7 that are not working anymore in v1.8 and I have a feeling that this might be highly interesting for many others.

Edit: Just to elaborate on what SCP does a little more: It allows you to have a configurable product that can have dependencies between options. This works by having a single product for every possible option. This way you can for example have the price go up, depending on material and size of a product. So one size can have different prices depending on the material and then a new price range if the size goes up. The GitHub site also gives a better understanding, just in case this was way confusing.

If you want to install SCP, make sure to install the fixes that are provided in the pull requests of the repo.

  • 写回答

2条回答 默认 最新

  • douzhangbao2187 2014-05-29 10:26
    关注

    So this is what I ended up using. It does not change any code in the configurable.phtml or SCP core code. Instead I just make a call to skin/frontend/base/default/js/scp_product_extension.js to update the options and prices. This has to happen clientside, but so does most of the SCP logic apparently.

       var sizeselect = $(".catalog-product-view .input-box select").eq(0);
       var materialselect = $(".catalog-product-view .input-box select").eq(1);
    
    
          function optionsToRadios(element){
            var select = element;
    
            if (element == materialselect){
                $(".radios").append("<div class='secondradiofield'></div>")
                select.find('option').each(function(j, option){
                    var option = $(option);
                    var eachval = option.val();
    
                    $(".catalog-product-view .secondradiofield").append(
                        "<input type='radio' id='s1"+j+"' data-value='"+eachval+"' data-position='"+j+"'/><span><label for='s1"+j+"'>"+option.text()+"</label></span>"
                    );
    
                }); // ende find each
    
            } else{
    
                select.find('option').each(function(j, option){
                    var option = $(option);
                    var eachval = option.val();
    
                    $(".catalog-product-view .radios").append(
                        "<input type='radio' id='s2"+j+"' data-value='"+eachval+"' data-position='"+j+"'/><span><label for='s2"+j+"'>"+option.text()+"</label></span>"
                    );
    
                }); // end find each
            }   
        };
    
        // Make first <select> to radios
        optionsToRadios(sizeselect);
    
    
    
    
            /*
            *
            * @SCP - seems like the passed element cannot be a jQuery object!
            * @SCP - spConfig.reloadPrice() is autmatically called by spConfig.configureElement(el).
            *
            */
    
            $(document).on("click", ".radios>input[type='radio']", function(){
    
                var val = $(this).attr("data-value");
                var index = $(this).attr("data-position");
                var select = $(".catalog-product-view .input-box select").eq(0);
    
                // deselect other options
                select.prop("selected", false);
                select.find("option").removeAttr("selected"); 
    
                // synch options with <select>
                var clickedOption = $(select.find("option")[index]);
                clickedOption.attr("selected", "selected");
                clickedOption.prop("selected", true); // IMPORTANT: Firefox need the prop to work!
    
                // Make call to SCP to update
                var el = document.getElementsByClassName("super-attribute-select")[0];
                spConfig.configureElement(el);
                spConfig.reloadPrice();
    
                // Show second <select> as radios
                if($("input[type='radio']").length < 5){
                    optionsToRadios(materialselect);
                }
    
                // Deselect other radio buttons
                $(".radios>input[type='radio']").not($(this)).prop('checked', false);
            });
    

    First function just turns the default <select> elements into radio buttons. <select> elements are hidden with CSS as we don't want the options to appear twice. Next is just listening for the click events on the radio-buttons and basically synch the clicks to the <select> elements. When the select element has changed, you just have to call the spConfig.configureElement(el); function where el is the corresponding <select> element.

    If there is a second <select> element with dependencies to the first, this one will have to update as well when a radio-button is clicked.

    Hope that gives others a clue, who attempt to do the same.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀