来宾客户检查Woocommerce订单结算电子邮件的首笔订单折扣 [英] First order discount for guest customers checking Woocommerce orders billing email

查看:70
本文介绍了来宾客户检查Woocommerce订单结算电子邮件的首笔订单折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过检查GUEST客户的电子邮件地址是否有已处理和已完成的订单,如果电子邮件中没有订单,我想给GUEST一个第一订单折扣".如果在访客输入电子邮件时发生这种情况,那会很好.

By checking the GUEST customer's email address against processing and completed orders, I want to give the GUEST a "first order discount" if the email has no orders. It would be nice if this could happen as the guest types in their email.

我认为我已经设法制作了折扣代码,现在我希望在合并这两个代码方面寻求帮助.

I think I've managed to make the discount code and now I am asking for help on merging these two codes, making it all work.

这是折扣代码:

add_action( 'woocommerce_cart_calculate_fees', 'first_time_order_discount', 10, 1 );
function first_time_order_discount( $wc_cart ) {

    if ( is_admin() && ! defined('DOING_AJAX') ) return;

    // discount percentage, change this into whatever you like
    // currently set to 5%
    $percent = 5;

    // calculate first order discount based on the percentage above
    $first_order_discount = $wc_cart->cart_contents_total * $percent / 100;
    $wc_cart->add_fee( __( 'First Order Discount', 'woocommerce')." ($percent%)", -$first_order_discount );
}

这是控制代码:

function has_bought( $customer_email ){
    $orders = get_posts( array(
        'numberposts' => -1,
        'post_type' => 'shop_order',
        'post_status' => array('wc-processing', 'wc-completed'),
    ) );

    $email_array = array();

    foreach($orders as $order) {
        $order_obj = wc_get_order($order->ID);
        $order_obj_data = $order_obj->get_data();
        array_push($email_array, $order_obj_data['billing']['email']); 
    }
    if (in_array($customer_email, $email_array)) {
        return true;
    } else {
        return false;
    }
}

不确定如何将两者合并为一个.

Not sure how to merge these two into one.

推荐答案

要从结算电子邮件"用户实时输入中检查客户的(来宾与否)订单,需要Javascript和Ajax启用(应用)结帐页面上的购物车折扣(负费用).

To check customer (guest or not) orders from "Billing email" user live input requires Javascript and Ajax to enable (apply) a cart discount (a negative fee) on checkout page.

完整代码:

// Conditionally apply a percentage discount if customer has already make a purchase
add_action( 'woocommerce_cart_calculate_fees', 'first_time_purchase_percentage_discount', 10, 1 );
function first_time_purchase_percentage_discount( $cart ) {
    $percent = 5; // HERE set the discount percentage (float)

    if ( is_admin() && ! defined('DOING_AJAX') )
        return;

    if ( WC()->session->get( 'first_purchase_discount' ) && is_checkout() ) {
        // The First order discount percentage calulation
        $first_order_discount = $cart->get_subtotal() * $percent / 100;

        // Apply the discount
        $cart->add_fee( __( 'First Order Discount', 'woocommerce')." ($percent%)", -$first_order_discount );
    }
}

// The Wordpress Ajax PHP receiver
add_action( 'wp_ajax_checkout_billing_email', 'get_ajax_checkout_billing_email' );
add_action( 'wp_ajax_nopriv_checkout_billing_email', 'get_ajax_checkout_billing_email' );
function get_ajax_checkout_billing_email() {
    // Checking that the posted email is valid
    if ( isset($_POST['cb_email']) && filter_var($_POST['cb_email'], FILTER_VALIDATE_EMAIL) ) {
        // Get one of customer orders from billing email
        $orders = get_posts( array(
            'numberposts' => 1, // Just one is enough
            'meta_key'    => '_billing_email',
            'meta_value'  => sanitize_email( $_POST['cb_email'] ),
            'post_type'   => 'shop_order',
            'post_status' => array('wc-processing', 'wc-completed')
        ) );

        // If the inputed billing email is not set on a customer order we set the value to TRUE
        $value = sizeof($orders) == 0 && ! empty($_POST['cb_email']) ? true : false;

        // Set the value in custom Woocommerce session identifier
        WC()->session->set('first_purchase_discount', $value );

        // Return the session value to jQuery
        echo WC()->session->get('first_purchase_discount');
    }
    die();
}

add_action('wp_footer', 'checkout_billing_email_js_ajax' );
function checkout_billing_email_js_ajax() {
    // Only on Checkout
    if( is_checkout() && ! is_wc_endpoint_url() ) :

    // Initializing
    WC()->session->set('first_purchase_discount', false);
    ?>
    <script type="text/javascript">
    jQuery(function($){
        if (typeof wc_checkout_params === 'undefined') 
            return false;

        $( 'input#billing_email' ).on('change blur', function() {
            var value = $(this).val();
            console.log(value);
            $.ajax({
                type: 'POST',
                url: wc_checkout_params.ajax_url,
                data: {
                    'action': 'checkout_billing_email',
                    'cb_email': value,
                },
                success: function (result) {
                    if( result == 1 ) {
                        // Update checkout
                        $(document.body).trigger('update_checkout');
                    }
                    console.log(result); // For testing (to be removed)
                }
            });
        });
    });
    </script>
    <?php
    endif;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

添加:

要处理保留"状态,请替换以下行:

To handle "on hold" status too, replace the following line:

'post_status' => array('wc-processing', 'wc-completed')

由此:

'post_status' => array('wc-on-hold', 'wc-processing', 'wc-completed')

这篇关于来宾客户检查Woocommerce订单结算电子邮件的首笔订单折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆