【问题标题】:First order discount for guest customers checking Woocommerce orders billing email检查 Woocommerce 订单账单电子邮件的访客客户的首次订单折扣
【发布时间】:2019-08-18 14:46:57
【问题描述】:

通过检查 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;
    }
}

不确定如何将这两个合并为一个。

【问题讨论】:

  • @helgatheviking 你不能帮忙吗?
  • 仅供参考:重新启用已删除的答案并更新...请检查。

标签: php jquery ajax wordpress woocommerce


【解决方案1】:

要检查客户(客人与否)来自“结算电子邮件”用户实时输入的订单需要 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')

【讨论】:

  • 谢谢您,非常感谢您的帮助。我不确定这是否属于“新问题”规则,但是否可以在电子邮件字段上方添加文本,通知用户已应用折扣?否则很容易“错过”。再次,非常感谢。
  • 当然,我赞成并接受了。再次谢谢你。这是新问题。请看一看。 stackoverflow.com/questions/55409078/…
猜你喜欢
  • 2017-08-08
  • 2022-11-22
  • 2018-09-19
  • 2021-06-11
  • 2019-08-28
  • 1970-01-01
  • 2020-06-03
  • 2017-08-19
  • 1970-01-01
相关资源
最近更新 更多