【发布时间】:2020-11-21 02:25:49
【问题描述】:
我的目标:使用 AJAX 对购物车的总价格应用 10% 的折扣。
背景:此折扣必须仅适用于来自给定网站的特定用户。这个想法是在我添加到购物车页面的输入字段中获取他们的会员编号。使用 API,我将检查此数字并应用折扣(考虑到进一步解释的问题,我不会在此详细说明此检查部分,仅关注问题)。
想法:
- 创建一个插件,该插件将在购物车页面中添加一个带有验证按钮的输入字段。
- 单击按钮时进行 AJAX 调用
- 如果用户会员号良好,则应用折扣(我不会在这里处理 API 检查,因为这不是问题)
问题:所以我使用 AJAX 来验证这个数字,然后对购物车的总价格应用 10% 的折扣。问题是我找不到如何更新总数。
我的 JS:
(function($) {
$("body").on("click", ".updateCart", function(e) {
e.preventDefault();
var form = $('#cartAjaxTcs');
var value = form.serialize();
$.ajax({
type:'POST',
data: {
action: 'test',
number: value
},
url: ajaxurl,
success: function(value) {
jQuery("[name='update_cart']").removeAttr('disabled');
jQuery("[name='update_cart']").trigger("click");
console.log(value);
},
error:function(){
console.log('error');
}
});
});
})( jQuery );
我的 PHP:
<?php
/*
Plugin Name: discount Plugin
Description: Apply 10% discount on cart's price
Author: Aurélien
Version: 1.0.0
*/
/**
* Check if WooCommerce is active
**/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
class Discount_Plugin{
public static function init() {
add_action('wp_enqueue_scripts', __CLASS__ .'::callback_for_setting_up_scripts');
//Add input field
add_action('woocommerce_cart_collaterals', __CLASS__.'::order_comments_custom_cart_field');
}
public static function callback_for_setting_up_scripts() {
$path = '/wp-content/plugins/discount-plugin/assets/css/style.css';
wp_register_style( 'discountStyle', $path );
wp_enqueue_style( 'discountStyle' );
wp_enqueue_script( 'ajax-script', plugins_url( 'assets/js/discount-plugin.js', __FILE__ ), array('jquery'), '1.0', true );
wp_localize_script( 'ajax-script', 'ajaxurl', admin_url( 'admin-ajax.php' ) );
}
public static function order_comments_custom_cart_field() {
?>
<form id="cartAjaxTcs" class="discount_input_wrapper" method="post" action="/wp-admin/admin-post.php">
<div class="discount_form_field">
<input type="text" name="number" id="number" placeholder="<?php echo 'My number, ie : 4GG524G42';?>">
<label for="number"><?php echo 'Membership number';?></label>
</div>
<button class="updateCart" type="submit"><?php echo 'Update cart';?></button>
</form>
<?php
}
Discount_Plugin::init();
add_action( 'wp_ajax_test', 'test' );
add_action( 'wp_ajax_nopriv_test', 'test' );
function test()
{
//Solution 1 or solution 2
}
}
我的第一个解决方案:我尝试像这样直接降低总价。它会更改总价但不显示它,即使在总价仍与折扣前相同的后端购物车报告中也是如此。
function test()
{
//This is displayed in console
echo 'test-init';
if (!DOING_AJAX){
return;
}
$total = (int) WC()->cart->get_totals()['total'];
$total *= 0.9;
WC()->cart->set_total($total);
wp_die();
}
我的第二个解决方案: 我试图触发“更新购物车”按钮(已经出现在页面上)并使用 woocommerce_before_calculate_totals 挂钩来更新价格,但它甚至没有被调用。
显然,在我的 AJAX 的操作函数中没有执行任何钩子或过滤器。我已经使用一个简单的过滤器进行了测试,以将一个类添加到正文中,它在我的动作函数内部不起作用,但在外部工作得很好。我放了一些标志来看看我的函数是否被 AJAX 调用了!
//This works well! If I put the same filter in my 'test' function it doesn't work
add_filter( 'body_class', function( $classes ) {
return array_merge( $classes, array( 'test-class' ) );
} );
function test()
{
//This is displayed in console
echo'test-init';
if (!DOING_AJAX){
return;
}
//This does't work
add_filter( 'body_class', function( $classes ) {
return array_merge( $classes, array( 'test-class2' ) );
} );
//The callback function isn't called
add_action( 'woocommerce_before_calculate_totals', 'update_price' );
//This is displayed in console
echo 'test';
wp_die();
}
//Not called
function update_price(){
echo 'update';
}
编辑:
我重写了我的解释,通过附加代码使它们更容易理解:
这里有一些使用woocommerce_cart_calculate_fees钩子的负费用尝试:
如果我在 AJAX 操作函数 之外使用 add_action,woocommerce_cart_calculate_fees 钩子的回调函数可以完美运行:
add_action( 'wp_ajax_test', 'test' );
add_action( 'wp_ajax_nopriv_test', 'test' );
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( ( is_admin() && ! defined( 'DOING_AJAX' )))
return;
$percentage = 0.1;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'discount', -$surcharge, true, '' );
}
function test(){
//My AJAX action function
}
所以这项工作,应用了负费用,但是当页面加载时,这不是我想要的,因为我想在触发 更新购物车按钮时应用负费用,所以这个想法是将 add_action 集成到我的 AJAX 动作函数 中,如下所示:
add_action( 'wp_ajax_test', 'test' );
add_action( 'wp_ajax_nopriv_test', 'test' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( ( is_admin() && ! defined( 'DOING_AJAX' )))
return;
$percentage = 0.1;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'discount', -$surcharge, true, '' );
}
//My AJAX action function
function test(){
echo 'test1'; //flag 1
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
echo 'test2'; //flag 2
}
根本不调用回调函数woocommerce_custom_surcharge。我的两个标志都显示在 chrome 控制台中,因此这意味着我的操作函数被正确调用。所以我的问题是:如何使这个 add_action 在我的操作函数中工作?
【问题讨论】:
-
您的解释、详细信息和提供的代码不是很清楚……如果您不想使用优惠券,可以使用负购物车费用来获得 10% 的折扣。
-
谢谢你的回答@LoicTheAztec,我做了修改的回复。
-
我也更新了我的代码和描述。我希望它现在可以测试。谢谢你的回答!
标签: ajax wordpress woocommerce cart hook-woocommerce