首页>Magento>source

我的产品类别列表,购物车和结帐页面上的边栏上都有优惠券代码.每当我在产品或类别列表页面或结帐页面上尝试添加优惠券代码时,都会将我带到购物车页面.有没有什么办法可以让它在同一页面上应用而无需在ajax的帮助下重定向或重新加载?

<?php echo $this->getMessagesBlock()->toHtml(); ?>
<div class="coupon bg-gray">
<form id="discount-coupon-form" action="<?php echo $this->getUrl('checkout/cart/coupon') ?>" name="discountcode-form" method="post">
    <div class="discount">
        <h2><?php echo $this->__('Discount Codes') ?></h2>
        <div class="discount-form">
            <!--<label for="coupon_code"><?php echo $this->__('Discount Codes') ?></label>-->
            <input type="hidden" name="remove" id="remove-coupone" value="0" />
            <!--<div class="field-wrapper">-->
                <input class=" form-control" type="text" placeholder="Enter Coupon Code" id="coupon_code" name="coupon_code" value="<?php echo $this->escapeHtml($this->getCouponCode()) ?>" />
                <!--<div class="button-wrapper">-->
                   <!-- <button type="button" title="<?php echo $this->__('Apply') ?>" class="btn-danger" onclick="discountForm.submit(false)" value="<?php echo $this->__('Apply') ?>"><span><span><?php echo $this->__('REEDEM COUPON') ?></span></span></button>--><?php $block = $this->getLayout()->createBlock('checkout/onepage_link'); ?>   <?php if ($block->isPossibleOnepageCheckout()):?>       <?php if ($block->isDisabled()):?>               <input type="submit" title="Minimum order should be Rs.300 or above" value="<?php echo $this->__('REEDEM COUPON') ?>" class="btn-danger btn-checkoutno-checkout" disabled="disabled" />                    <?php else: ?>                   <input type="submit" title="Minimum order should be Rs.300 or above" value="<?php echo $this->__('REEDEM COUPON') ?>" class="btn-danger"  />
                   <?php endif?>    <?php endif?>                                  

 <?php if(strlen($this->getCouponCode())): ?>
                        <button type="button" title="<?php echo $this->__('Cancel') ?>" class="button2 cancel-coupon" onclick="discountForm.submit(true)" value="<?php echo $this->__('Cancel') ?>"><span><span><?php echo $this->__('Cancel') ?></span></span>
                        </button>
                    <?php endif;?>
                <!--</div>-->
            <!--</div>-->
        </div>
    </div>
</form>
</div>
<script type="text/javascript">
//<![CDATA[
function updateCoupon(reqid) {
var reqid;
if(reqid == 2) {  $('coupon_code').setValue('');  }
$('discount-coupon-form').request({
method: 'post',
onComplete: payment.onComplete,
onSuccess: payment.onSave,
onFailure: checkout.ajaxFailure.bind(checkout),
})
}
//]]>
</script>
最新回答
  • 2019-12-5
    1 #

    您的代码中存在很多问题.

    JavaScript is wrong. 你有电话 opcheckout.js   功能 payment.payment.onSave   和c heckout.ajaxFailure.bind(checkout )等 does not work on all pages

    所以你需要 Custom code   这将在所有页面上的ajax上应用优惠券代码

    You can follow the link for full your requirement:

    自定义AJAX折扣表格未处理优惠券

    lelandcope

  • 2019-12-5
    2 #

    我为折扣代码弹出创建了一个模块,该方法可以在不重新加载购物车页面的情况下应用代码

    以下是此模块的链接供参考

    在你的javascript方法updateCoupon()中你调用了一个调用控制器动作的ajax方法,你需要添加以下代码来应用代码而不刷新页面这里 applyAction   是我的ajax行动

    public function applyAction()
        {
            $response = array();
            $couponCode = (string) $this->getRequest()->getParam('coupon_code');
            if ($this->getRequest()->getParam('remove') == 1) {
                $couponCode = '';
            }
            $oldCouponCode = $this->_getQuote()->getCouponCode();
            if (!strlen($couponCode) && !strlen($oldCouponCode)) {
                $message = "Please enter valid code";
                $response['error'] = 1;
            }
            try {
                $codeLength = strlen($couponCode);
                $isCodeLengthValid = $codeLength && $codeLength <= Mage_Checkout_Helper_Cart::COUPON_CODE_MAX_LENGTH;
                $this->_getQuote()->getShippingAddress()->setCollectShippingRates(true);
                $this->_getQuote()->setCouponCode($isCodeLengthValid ? $couponCode : '')
                    ->collectTotals()
                    ->save();
                if ($codeLength) {
                    if ($isCodeLengthValid && $couponCode == $this->_getQuote()->getCouponCode()) {
                            $message = $this->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode));
                        $this->_getSession()->setCartCouponCode($couponCode);
                    } else {
                            $message = $this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode));
                            $response['error'] = 1;
                    }
                } else {
                    $message = $this->__('Coupon code was canceled.');
                }
            } catch (Mage_Core_Exception $e) {
                $message = $e->getMessage();
                $response['error'] = 1;
            } catch (Exception $e) {
                $response['error'] = 1;
                $message = $this->__('Cannot apply the coupon code.');
                Mage::logException($e);
            }
            $response['message'] = $message;
            $response['code'] = $couponCode;
            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
        }
    

相关问题

  • Magento控制器覆盖多次
  • magento2:如何检查基于magento的网站中使用的主题。