1

このコードを改善するために、ユーザーがそれまでに行ったすべての購入の合計に応じて機能するようにしたいと考えています。つまり、合計金額が 100 ユーロ未満の場合は別のメールを送信し、100 ユーロから 200 ユーロの場合は別のメールを送信し、200 ユーロを超える場合は別のメールを送信します。

ここにコードがあります:

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>';
}

どうすれば達成できますか?

前もって感謝します。

4

1 に答える 1

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 ファイルに入ります。

于 2017-01-03T18:42:35.680 に答える