Magento:如何在产品详细信息页面中显示所选自定义选项价格在价格框中

时间:2013-08-31 09:35:29

标签: php magento magento-1.7 customization

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

enter image description here

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

所以请建议我解决。

5 个答案:

答案 0 :(得分:0)

首先你必须把按钮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>';
    }

options_1_text,options_2_text,options_3_text,options_4_text放弃了你的身份,它会得到你的结果

答案 1 :(得分:0)

打开页面:app\code\core\Mage\Catalog\Block\Product\View\Options.php找到protected function _getPriceConfiguration($option)功能,并在$data['title'] = $option->getTitle();声明之前添加return $data;

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

在页面顶部添加<div id="optiontitle"></div>

然后找到这个:

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

答案 2 :(得分:0)

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

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

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

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

现在您只需确保在模板中加入<div id="detailDiv"></div>

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

答案 3 :(得分:0)

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

Block class:

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 :(得分:0)

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