提问者:小点点

在Magento产品页面上调用JavaScript/jQuery函数onchange产品选项


我的任务是在magento产品页面上做一个盒子,上面有选定数量的总价。换句话说,数量*价格。所以我就这样做了:

$j = jQuery.noConflict();

function get_total_qty() {
    var qt_Price = parseInt(0);
    $j('.price').each(function() {
        if (this.offsetParent.className != 'dropdown-cart no_quickshop') {
            qt_Price = parseFloat(this.textContent);
        }
    });


    /*
     * AJAX call
     */
    var quantity = $j('#qty').val(); // get final quantity
    var price = qt_Price; // get price
    $j.post("http://example.com/myscripts/ajaxPriceCal.php", {
            qty: quantity,
            pr: price
        },
        function(data) {
            var qt_total_price = data;
            $j('#totpr').html(qt_total_price);
        });

}

$j(document).ready(function() {
    get_total_qty();
});

每次按下增加或减少数量的按钮时,我都会调用get_total_qty()函数。现在,我尝试使用产品选项的下拉列表,但它并不像我预期的那样工作。它在重新加载产品价格之前调用函数,我用它来计算。

例如。产品是10美元,数量是2美元,总20美元如果选择了选项(+5美元),那么我得到的显示是价格15美元,数量2美元,总20美元。因为在更新.price类元素之前调用了函数。如果更改数量,它再次显示正确的总数。

也许我做这些计算的方法很傻。我愿意听取关于如何正确地做这件事的建议。然而,我的紧迫问题是在正确的时间调用函数。我应该编辑什么文件,我应该在哪里发出呼叫?

抱歉,如果我的问题看起来很傻,我是JS或jQuery的新手...

编辑->

/app/design/frontend/base/default/template/catalog/product/view/type/options/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">
                <select onchange="get_total_qty()" name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select">
                  <option><?php echo $this->__('Choose an Option...') ?></option>
                </select>
            </div>
          </dd>
    <?php endforeach; ?>
    </dl>
<script type="text/javascript">
    var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
</script>
<?php endif;?>

其结果是产品页面上的HTML:

<dl>
        <dt><label class="required"><em>*</em>Color</label></dt>
    <dd>
        <div class="input-box">
            <select onchange="get_total_qty()" name="super_attribute[272]" id="attribute272" class="required-entry super-attribute-select">
                <option>Pasirinkite...</option>
              </select>
          </div>
    </dd>
        <dt><label class="required"><em>*</em>Puokštės dydis</label></dt>
    <dd class="last">
        <div class="input-box">
            <select onchange="get_total_qty()" name="super_attribute[975]" id="attribute975" class="required-entry super-attribute-select">
                <option>Pasirinkite...</option>
              </select>
          </div>
    </dd>
</dl>
<script type="text/javascript">
    var spConfig = new Product.Config({"attributes":{"272":{"id":"272","code":"color","label":"Color","options":[{"id":"26","label":"Red","price":"0","oldPrice":"0","products":["177","178"]}]},"975":{"id":"975","code":"puokstes_dydis","label":"Puok\u0161t\u0117s dydis","options":[{"id":"136","label":"Didel\u0117","price":"30","oldPrice":"30","products":["178"]},{"id":"137","label":"Vidutin\u0117","price":"20","oldPrice":"20","products":["177"]}]}},"template":"#{price}\u00a0Lt","basePrice":"10","oldPrice":"10","productId":"175","chooseText":"Pasirinkite...","taxConfig":{"includeTax":false,"showIncludeTax":false,"showBothPrices":false,"defaultTax":21,"currentTax":0,"inclTaxTitle":"Incl. Mokestis"}});
</script>

如您所见,我在