【问题标题】:Apply WooCommerce Coupon code to checkout应用 WooCommerce 优惠券代码结帐
【发布时间】:2017-07-13 00:42:25
【问题描述】:

我正在尝试在 SitePoint 上创建一个“产品兑换页面,如 this tutorial 所示。

问题是该产品确实已添加到购物车中,您可以继续结帐,但与优惠券代码相关的折扣不会自动应用。在我创建的优惠券代码中,值设置为 100% 折扣。

您可以通过结帐页面上飞出的“您有优惠券代码”再次申请优惠券代码,但这违背了整个目的。

我也没有让这段代码开始工作,但我能够弄清楚:

// Check coupon to make determine if its valid or not if( ! $coupon->id && ! isset( $coupon->id ) ) { ...Rest of code here...

应该是:

// Check coupon to make determine if its valid or not
   if( ! $coupon->id && ! isset( $coupon_id ) ) {

请注意第二个 Not isset 变量名。也许这确实有效,但不是正确的处理方式,每个人都知道,但我除外。

遗憾的是,上午我已经走出了自己的舒适区,但我愿意通过犯错并找出解决方法来学习,并向比我更聪明和/或更先进的人学习。在我的直接朋友圈中,我没有人可以询问并得到任何其他答案:“嗯?!?”,所以我在 Stackoverflow 上试了一下。

可能不喜欢仅指向 SitePoint 上的教程的链接,所以这是我正在使用的完整代码:

functions.php 中添加的 Ajax 处理程序

add_action( 'wp_ajax_spyr_coupon_redeem_handler', 'spyr_coupon_redeem_handler' );
add_action( 'wp_ajax_nopriv_spyr_coupon_redeem_handler', 'spyr_coupon_redeem_handler' );

优惠券登录也添加到functions.php中

function spyr_coupon_redeem_handler() {

    // Get the value of the coupon code
    $code = $_REQUEST['coupon_code'];

    // Check coupon code to make sure is not empty
    if( empty( $code ) || !isset( $code ) ) {
        // Build our response
        $response = array(
            'result'    => 'error',
            'message'   => 'Code text field can not be empty.'
        );

        header( 'Content-Type: application/json' );
        echo json_encode( $response );

        // Always exit when doing ajax
        exit();
    }

    // Create an instance of WC_Coupon with our code
    $coupon = new WC_Coupon( $code );

    // Check coupon to make determine if its valid or not
    if( ! $coupon->id && ! isset( $coupon_id ) ) {
        // Build our response
        $response = array(
            'result'    => 'error',
            'message'   => 'Invalid code entered. Please try again.'
        );

        header( 'Content-Type: application/json' );
        echo json_encode( $response );

        // Always exit when doing ajax
        exit();

    } else {

        // Attempting to add the coupon code as a discount.
        WC()->cart->add_discount( $code );

        // Coupon must be valid so we must
        // populate the cart with the attached products
        foreach( $coupon->product_ids as $prod_id ) {
            WC()->cart->add_to_cart( $prod_id );
        }

        // Build our response
        $response = array(
            'result'    => 'success',
            'href'      => WC()->cart->get_cart_url()
        );

        header( 'Content-Type: application/json' );
        echo json_encode( $response );

        // Always exit when doing ajax
        exit();
    }
}

jQuery 表单提交代码,通过在 functions.php 中注册的 Ajax 处理程序入队

jQuery( document ).ready( function() {
   jQuery( '#ajax-coupon-redeem input[type="submit"]').click( function( ev ) {

    // Get the coupon code
    var code = jQuery( 'input#coupon').val();

    // We are going to send this for processing
    data = {
        action: 'spyr_coupon_redeem_handler',
        coupon_code: code
    }

    // Send it over to WordPress.
    jQuery.post( woocommerce_params.ajax_url, data, function( returned_data ) {
        if( returned_data.result == 'error' ) {
            jQuery( 'p.result' ).html( returned_data.message );
        } else {
            // Hijack the browser and redirect user to cart page
            window.location.href = returned_data.href;
        }
    })

    // Prevent the form from submitting
    ev.preventDefault();
    }); 
});

在此先感谢您为我指明正确的方向。

【问题讨论】:

    标签: php jquery ajax wordpress woocommerce


    【解决方案1】:

    更新:此时我得到了想要的功能。

    需要做的是添加:

    // Let's add the discount to the cart.
    global $woocommerce;
    WC()->cart->add_discount( $code );
    

    在 foreach 语句中。完整的 else 语句现在如下所示:

    } else {
    
        // Coupon must be valid so we must
        // populate the cart with the attached products
        foreach( $coupon->product_ids as $prod_id ) {
            WC()->cart->add_to_cart( $prod_id );
    
            // Let's add the discount to the cart.
            global $woocommerce;
            WC()->cart->add_discount( $code );
        }
    
        // Build our response
        $response = array(
            'result'    => 'success',
            'href'      => WC()->cart->get_cart_url()
        );
    
        header( 'Content-Type: application/json' );
        echo json_encode( $response );
    
        // Always exit when doing ajax
        exit();
    

    我仍然不确定这是否是处理此问题的正确方法,但它似乎有效。

    例如,我正在调用(?!)全局 $woocommerce 变量,但在下面我使用全局(?!)类 WC() 添加优惠券。不确定这是否像它得到的那样干净和合乎逻辑。

    如果有人知道更好/更清洁的方法,请告诉我!我很高兴向你们学习,也许有一天我可以回报你们,谁知道呢。

    【讨论】:

      猜你喜欢
      • 2016-02-25
      • 2020-03-19
      • 2016-02-09
      • 2014-11-26
      • 2021-11-01
      • 2021-01-03
      • 2011-07-05
      • 2014-09-08
      • 1970-01-01
      相关资源
      最近更新 更多