我正在尝试在Woocommerce商店中实现“兑换优惠券”功能,我已经找到了有用的教程,但无法使其正常运行。

This is the tutorial.

我已经完成的工作:


使用以下代码创建了新页面模板:

<div class="redeem-coupon">
<form id="ajax-coupon-redeem">
    <p>
        <input type="text" name="coupon" id="coupon"/>
        <input type="submit" name="redeem-coupon" value="Redeem Offer" />
    </p>
    <p class="result"></p>
</form><!-- #ajax-coupon-redeem -->



将此添加到我主题的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 {
// 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();
    }
}

创建了“ kody.js”:

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

使用以下代码从functions.php调用脚本:

function my_scripts_method() {
wp_register_script('kody',
get_template_directory_uri() . '/js/kody.js',
array('jquery'),
'1.0' );
enqueue the script
wp_enqueue_script('kody');
}
add_action('wp_enqueue_scripts', 'my_scripts_method');



这是奇怪的事情:这有点奏效。我设置了一个页面,可以在其中输入优惠券代码,粘贴代码,单击“兑换”,然后它将与优惠券相关的产品添加到购物车。它不会,但是会应用预定义的折扣。

“兑换优惠券”页面也只能工作一半。当有人将该字段留空或输入错误代码时,它应该显示错误消息-仅前者会这样做。输入错误的代码会导致重定向到购物车。

我对Ajax和JS的了解非常有限,我试图找到其他一些教程,但是没有任何运气。

代码有问题吗?这是从2014年开始的,因此Wordpress引擎可能有所更改,从而引起麻烦。

预先感谢您的任何答复!

问候

最佳答案

解决的问题,如果有人在提供的教程中遇到类似的问题,则必须执行以下操作:


要应用折扣,请添加以下代码:

global $woocommerce;
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 );

要显示无效的代码消息,请更改此:

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


对此:

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



现在一切正常。

(还更改了标题,以使将来其他人更容易找到此帖子。)

10-05 20:30
查看更多