【发布时间】: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