【问题标题】:Magento not recognizing custom javascript functionMagento 无法识别自定义 javascript 函数
【发布时间】:2014-02-12 14:23:07
【问题描述】:

好的,如果这个问题已经回答了,我很抱歉,我已经搜索并没有找到适合我情况的满意答案。

情况如下:

我有一个自定义模板文件用于显示类别中的产品,该列表的一部分是能够从类别页面直接将具有指定数量的产品添加到购物车中。为了实现这一点,我编写了一个函数,重写“添加到购物车”按钮的目标行,提交请求,然后再次将按钮重新写入原始版本(参见下面的代码)。我遇到的问题是,每次单击按钮时,我都会在调试面板中收到错误消息,指出customAddToCart is not defined。但是,如果您查看下面产品列表模板的代码,您会发现该函数甚至在生成列表之前就已定义(顺便说一句,我在页面底部有它并且遇到了同样的错误)。

如果有人能解释为什么 javascript 函数在与列表包含在同一页面上时会未定义...我当然可以使用一些帮助。

<?php
/**
 * Product list template
 *
 * @see Mage_Catalog_Block_Product_List
*/
?>
<?php
    $_productCollection=$this->getLoadedProductCollection();
    $_helper = $this->helper('catalog/output');
?>
<script type="text/javascript">
    function customAddToCart( product_id, url ){
        var qty = document.getElementById('qty_input_'+product_id).value;
        document.getElementById('addtocartbutton_'+product_id).setAttribute('onclick', "setLocation(" + url + "/qty/" + qty ")");
        document.getElementById('addtocartbutton_'+product_id).click(); return false;
        document.getElementById('addtocartbutton_'+product_id).setAttribute('onclick', "customAddToCart(" + product_id + ", " + url + ")");
    }
</script>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
    <?php // List mode ?>
    <?php if($this->getMode()!='grid'): ?>
    <ol class="products-list" id="products-list">
    <?php foreach ($_productCollection as $_product): ?>
        <li class="item">
            <?php // Product Image ?>
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(TRUE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(230, null); ?>" width="230" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
            <?php // Product description ?>
            <div class="product-shop">
                <div class="f-fix">
                    <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
                    <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h2>
                    <?php if($_product->getRatingSummary()): ?>
                    <?php echo $this->getReviewsSummaryHtml($_product) ?>
                    <?php endif; ?>
                    <?php echo $this->getPriceHtml($_product, true) ?>                    
                    <div class="desc std">
                        <?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                        <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>" class="link-learn"><?php echo $this->__('Learn More') ?></a>
                    </div>
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                             <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul>
                    <div class="add-to-cart-options">
                    <?php if($_product->isSaleable()): ?>
                    <?php if( !($_product->getTypeInstance(true)->hasRequiredOptions($_product) || $_product->isGrouped() )){ ?>
                        <label for="qty_input">Quantity: </label>
                        <input type="text" class="spinner qty-input" name="qty_input" id="qty_input_<?php echo $_product->getId(); ?>" />
                        <?php /*<p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p> */ ?>
                        <button type="button" class="button" onclick="javascript:customAddToCart(<?php echo $_product->getId(); ?>, '<?php echo $this->getAddToCartUrl($_product); ?>')"><span><span><?php echo $this->__('Add to Cart'); ?></span></span></button>
                    <?php } else { ?>
                        <button type="button" class="button" id="addtocartbutton_<?php echo $_product->getId(); ?>" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart'); ?></span></span></button>
                    <?php } ?>
                    <?php else: ?>
                    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                <?php endif; ?>
                </div>
            </div>
        </div>
    </li>
<?php endforeach; ?>
</ol>    
<script type="text/javascript">decorateList('products-list', 'none-recursive')</script>

<?php else: ?>

如您所见,页面顶部明确定义了该功能,那么为什么要这个按钮

<button type="button" class="button" onclick="javascript:customAddToCart(<?php echo $_product->getId(); ?>, '<?php echo $this->getAddToCartUrl($_product); ?>')"><span><span><?php echo $this->__('Add to Cart'); ?></span></span></button>

导致控制台中出现一条消息说customAddToCart is not defined 我不确定。

【问题讨论】:

  • 尝试在 CDATA.. 中设置 javascript 的范围
  • 感谢您的提示,我认为这是您推荐的内容和 wbaaron 下面建议的组合,但我能够让它发挥作用。

标签: javascript php magento


【解决方案1】:

是否有关于“意外字符串”的错误?函数第二行的连接缺少最后一个加号。

更新

"/qty/" + qty ")"

"/qty/" + qty + ")"

然后刷新。

【讨论】:

    猜你喜欢
    • 2016-01-30
    • 2012-08-04
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 2015-10-13
    • 2011-07-10
    相关资源
    最近更新 更多