2015-01-15 44 views

回答

3

您使用购物车类的add_fee()方法添加一定的费用。没有内建的方法可以知道客户做了多少个订单,所以我们可以尝试通过一个名为_number_order的用户元字段进行跟踪。

function so_27969258_add_cart_fee() { 
    $orders = intval(get_user_meta(get_current_user_id(), '_number_orders', true)); 
    if($orders < 1){ 
     WC()->cart->add_fee(__('First time fee', 'your-plugin'), 100); 
    } 
} 
add_action('woocommerce_before_calculate_totals', 'so_27969258_add_cart_fee'); 

如果我们不实际更新_number_orders键,那么它将永远是空/空/零点,用户将总是收取第一次费用。所以我们可以尝试在用户完成付款时更新该密钥。

function so_27969258_track_orders_per_customer(){ 
    $orders = intval(get_user_meta(get_current_user_id(), '_number_orders', true)); 
    $orders = $orders + 1; 
    update_user_meta(get_current_user_id(), '_number_orders', $orders); 
} 
add_action('woocommerce_payment_complete', 'so_27969258_track_orders_per_customer'); 

这是完全未经测试的,所以使用时需要您自担风险。此外,您可能想要考虑更改退款/取消等情况下的订单总数,但这似乎是一般要点。

+0

非常感谢。你解决了我的严重问题。 我在第一个函数中做了一些小改动: 'add_action('woocommerce_cart_calculate_fees','so_27969258_add_cart_fee'); function so_27969258_add_cart_fee(){ global $ woocommerce; $ orders = intval(get_user_meta(get_current_user_id(),'_number_orders',false)); if($ orders <1){ $ fee = 1; $ woocommerce-> cart-> add_fee('First Time Fee',$ fee,true,'standard'); } } ' 一切都像魅力一样工作。 再次感谢:) – 2015-01-15 19:23:22

+1

不推荐使用全局'$ woocommerce',而应该使用'WC()'来检索WooCommerce类的实例。 – helgatheviking 2015-01-15 22:55:59

+0

嗯。感谢您提供有用信息。现在我将使用WC()。再次感谢所有的事情。对此,我真的非常感激。 – 2015-01-16 06:23:57

相关问题