【问题标题】:Custom message depending on the sum of all the amount purchased by an user自定义消息取决于用户购买的所有金额的总和
【发布时间】:2017-01-03 17:07:48
【问题描述】:

在我努力改进this code 的过程中,我想让它根据用户在此之前所做的所有购买的总和来工作。也就是说,如果总量小于

代码如下:

add_action( 'woocommerce_email_before_order_table', 'completed_order_mail_message', 20 );
function completed_order_mail_message( $order ) {

    // Getting order user data to get the user roles
    $user_data = get_userdata($order->customer_user);

    if ( $order->post_status == 'wc-completed' && in_array('customer', $user_data->roles) )
        echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}

我怎样才能实现它?

提前致谢。

【问题讨论】:

  • 目前只有一个订单被传递给您的函数,所以我认为您需要使用用户 ID ($order->customer_user) 从数据库中获取除当前订单 ($order) 之外的所有订单。然后使用 foreach 循环并添加订单值。当您知道该用户花费了多少时,您将能够使用 IF..ELSE 或 SWITCH..CASE 输出您想要的消息。

标签: php wordpress woocommerce orders email-notifications


【解决方案1】:

你可以试试这样的:

add_action( 'woocommerce_email_before_order_table', 'completed_order_mail_message', 20 );
function completed_order_mail_message( $order ) {

    // Getting order user data to get the user roles
    $user_data = get_userdata($order->customer_user);

    $user_total_calculated = 0;

    // Get all current customer orders
    $customer_orders = wc_get_orders( $args = array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => $order->customer_user,
        'post_status' => 'wc_completed'
    ) );

    // Iterating and summing all paid current customer orders
    foreach ( $customer_orders as $customer_order )
        $user_total_calculated += get_post_meta($customer_order->ID, '_order_total', true);

    // After this you can now set your 3 conditions displaying 3 different notices or custom message
    if ( $order->post_status == 'wc-completed' && in_array('customer', $user_data->roles) ){

        if (100 >= $user_total_calculated)
            echo '<h2 id="h2thanks">Get 5% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More5</strong>" to receive a 5% discount on your next purchase! Click here to continue shopping.</p>';

        elseif ( 100 < $user_total_calculated && 200 >= $user_total_calculated )
            echo '<h2 id="h2thanks">Get 10% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More10</strong>" to receive a 10% discount on your next purchase! Click here to continue shopping.</p>';

        elseif ( 200 < $user_total_calculated )
            echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More20</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';

    }

}

这段代码应该可以工作

代码进入您的活动子主题(活动主题或任何插件文件)的 function.php 文件中。

【讨论】:

  • 惊人的洛伊克!你成功了!用作魅力!非常感谢!
猜你喜欢
  • 2019-04-06
  • 2016-08-02
  • 1970-01-01
  • 2019-12-17
  • 1970-01-01
  • 2022-06-20
  • 1970-01-01
  • 2021-07-18
  • 2018-04-22
相关资源
最近更新 更多