php - 访客客户检查 Woocommerce 订单账单电子邮件时可享受首单折扣

标签 php jquery ajax wordpress woocommerce

通过对照正在处理和已完成的订单检查 GUEST 客户的电子邮件地址,如果电子邮件中没有订单,我想为 GUEST 提供“首单折扣”。如果这能在客人输入电子邮件时发生,那就太好了。

我想我已经成功制作了折扣代码,现在我寻求帮助来合并这两个代码,使其一切正常。

这是折扣代码:

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

不知道如何将这两个合并为一个。

最佳答案

要检查来自“账单电子邮件”的客户(访客与否)订单,用户实时输入需要 Javascript 和 Ajax 来启用(应用)购物车折扣(负费用)> 在结帐页面上。

完整代码:

// 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 文件中。经过测试并有效。

<小时/>

添加:

要处理“暂停”状态,请替换以下行:

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

通过这个:

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

关于php - 访客客户检查 Woocommerce 订单账单电子邮件时可享受首单折扣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55387296/

相关文章:

javascript - PHP 和 Javascript 日期问题

jquery - 如何使包含一半文本和一半图像/照片库的扩展 div 正常工作?

javascript - 检查 .ajax 请求后加载的所有图像?

javascript - Jquery 选择器在 td 上不起作用

jQuery AJAX 返回结果

php - 如何将这些查询添加在一起(Yii2)?

javascript - 使用 Python 请求模拟 ajax POST 调用

php - HTML 删除表格行中的空格

javascript - 使用 jEditable、jQuery 和 DataTables 更新表中的单元格

javascript - 如何使用 httpwebrequest 在 C# 中获取网页(使用 ajax/javascript 的 php)的最终响应