为新客户在Woocommerce购物车上应用百分比折扣 [英] Apply percentage discount on Woocommerce cart items for new customers
问题描述
我们正在使用Klarna Checkout(第三方插件)来为我们的WooCommerce平台处理付款.
We are using Klarna Checkout(3rd party plugin) to handle payments for our WooCommerce platform.
将产品添加到购物车后,将显示Klarna Checkout表格,其中包含所需的详细信息,例如电子邮件和联系电话.
When the product is added to cart, Klarna Checkout Form appears with the details needed like email and contact number.
当用户输入电子邮件时,我会确定这是不是要给予 50%折扣的新电子邮件:
When a user enters their email, I determine if it's a new email to give 50% discount:
our-custom.js
var j = jQuery.noConflict();
// check every second if email is filled
var check_is_email_done = setInterval(function() {
var is_email_done = j('.klarna-widget-form-user .email').text();
if(is_email_done.length > 0) {
console.log('email is filled: ' + is_email_done);
var notFound = j('.fortnox-users td').filter(function(){
return j(this).text() == is_email_done;
}).get();
var token = notFound.length;
if(token > 0) {
console.log('Old customer..');
} else {
console.log('New customer..');
// call new_customer_discount() method in functions.php
j.ajax({
type: 'GET',
url: ajaxurl,
cache: false,
data: { action: 'newcustomerdiscount'},
success: function(data) {
console.log('newcustomerdiscount' + data);
},
error: function(xhr,status,error) {
console.log('newcustomerdiscount error:'+error);
}
});
}
clearInterval(check_is_email_done);
}
},1000);
functions.php
function new_customer_discount() {
//echo "new_customer_discount123";
$my_total = wc_format_decimal(WC()->cart->total, 2);
echo 'Total: '.$my_total;
do_action('woocommerce_calculate_totals', function($cart) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
print_r($cart);
$computed_price = 0;
// Loop Through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Get the product id (or the variation id)
$product_id = $cart_item['data']->get_id();
// GET THE NEW PRICE (code to be replace by yours)
if($computed_price > 0)
$prod_price = $computed_price * .50; // 50% discount
// Updated cart item price
$cart_item['data']->set_price( $prod_price );
}
});
}
我上面的代码流程是当我确定客户是否是新客户时,我在 functions.php 中调用new_customer_discount()
方法,然后执行带有回调的do_action
The flow of my code above is when I determine if a customer is new, I call the new_customer_discount()
method in functions.php then execute do_action with callback
您知道如何正确地在functions.php中执行以上钩子吗?任何帮助是极大的赞赏.谢谢
Do you know how can I execute hook above in functions.php properly? Any help is greatly appreciated. Thanks
推荐答案
由于我无法测试您的jQuery代码,因此可以说jQuery Ajax请求有效.现在要更改购物车项目的价格,您需要使用woocommerce_before_calculate_totals
代替,而在php Ajax中,您将使用WC_Session
…
As I can't test your jQuery code, Let say that jQuery Ajax request works. Now to change cart items prices you need to use woocommerce_before_calculate_totals
instead and in your php Ajax you will use WC_Session
…
在您的jQuery代码上,您可能需要在success
部分添加以下行:
On your jQuery code, you might need to add on the success
part the following line:
j('body').trigger('update_checkout'); // Refresh checkout
因此您的PHP代码将是:
So your PHP code will be:
add_action('wp_ajax_nopriv_newcustomerdiscount', 'ajax_customer_discount');
add_action('wp_ajax_newcustomerdiscount', 'ajax_customer_discount');
function ajax_customer_discount() {
WC()->session->set('new_customer', true);
}
add_action('woocommerce_before_calculate_totals', 'set_new_customer_discount', 100, 1 );
function set_new_customer_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// If it's not a new customer we exit
if( ! WC()->session->get('new_customer') )
return; // Exit
// Loop Through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// 50% items discount
$cart_item['data']->set_price( $cart_item['data']->get_price() / 2 );
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以在WC()->session->get('new_customer')
是true
...
Code goes in function.php file of your active child theme (or active theme). Tested and works when WC()->session->get('new_customer')
is true
…
这篇关于为新客户在Woocommerce购物车上应用百分比折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!