应用WooCommerce优惠券代码结帐

问题描述:

我想创建一个“产品兑换页面,如图this tutorial上SitePoint。应用WooCommerce优惠券代码结帐

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

您可以通过“您是否有优惠券代码“在结帐页面上飞出去,但是这打败了整个目的。

我也没有g这等人的工作代码来开始的,但我能弄清楚的是:

// 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)) { 

请注意,第二不isset变量名。也许这确实奏效,但不是处理事情的正确方式,大家都知道,但我。

不幸的是,我已经离开了我的舒适区a.t.m.,但我愿意通过犯错误并学会如何解决这些问题,并从那些更聪明,更聪明的人那里学习和/或更先进。在我的朋友们的直接讨论中,我没有一个能够得到答案的人,然后得到任何其他答案:“嗯?!?”,所以我在Stackoverflow上给了它一个镜头。

对SitePoint教程链接只可能是不理解,所以这里是我使用的完整代码:在functions.php中添加

阿贾克斯处理

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的表单提交代码,经由注册的Ajax处理排队在functions.php的

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

在此之前感谢我指出了正确的方向。

更新:在这一刻,我得到了所需的功能工作。

什么需要做的是补充:

// 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(); 

我仍然不知道这是HANDELING这个正确的方式,但它似乎工作。

例如,我打电话(?!)全球$woocommerce变量,但下面我使用全球(?!)类WC()添加优惠券。不确定这是否如同干净和合乎逻辑。

如果有人知道更好/更清洁的方式,请让我知道!我很高兴向你们学习,也许有一天我能够回报这份恩惠,谁知道呢。