2017-02-22 542 views
1

我想創建一個「產品兌換頁面,如圖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(); 
    }); 
}); 

在此之前感謝我指出了正確的方向。

回答

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

我仍然不知道這是HANDELING這個正確的方式,但它似乎工作。

例如,我打電話(?!)全球$woocommerce變量,但下面我使用全球(?!)類WC()添加優惠券。不確定這是否如同乾淨和合乎邏輯。

如果有人知道更好/更清潔的方式,請讓我知道!我很高興向你們學習,也許有一天我能夠回報這份恩惠,誰知道呢。