ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php-WooCommerce中付费客户的自动折扣

2019-12-10 22:31:42  阅读:357  来源: 互联网

标签:coupon cart woocommerce wordpress php


我想向在我的网站上至少购买过一次的所有人自动应用折扣券.这是我尝试的代码,但页面上出现致命错误…

function has_bought( $customer_email ){
    $orders = get_posts(array(
        'numberposts' => -1,
        'post_type' => 'shop_order',
        'post_status' => array('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;
    }
}

add_action( 'woocommerce_before_cart', 'apply_matched_coupons' );
function apply_matched_coupons() {
    global $woocommerce;

    $coupon_code = '10fidelity'; // coupon code

    if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;

    if ( has bought() {
        $woocommerce->cart->add_discount( $coupon_code );
        $woocommerce->show_messages();
    }
}

解决方法:

您的实际代码繁重且过时了……相反,请尝试使用WC_Customer is_paying_customer属性的以下更简便,有效的方法:

add_action( 'woocommerce_before_calculate_totals', 'enable_customer_fidelity_discount', 10, 1 );
function enable_customer_fidelity_discount( $cart ) {
    if ( ! ( is_cart() || is_checkout() ) )
        return;

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

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // The discount coupon code below
    $coupon_code = '10fidelity';

    if( ! in_array( $coupon_code, $cart->get_applied_coupons() ) && WC()->customer->get_is_paying_customer() ) {
        $cart->apply_coupon( $coupon_code );
    } elseif( in_array( $coupon_code, $cart->get_applied_coupons() ) && ! WC()->customer->get_is_paying_customer() ) {
        $cart->remove_coupon( $coupon_code );
    }
}

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试和工作

或使用此改进的轻巧功能来检查客户是否已下订单:

function has_bought( $user_id = 0 ) {
    global $wpdb;
    $customer_id = $user_id == 0 ? get_current_user_id() : $user_id;
    $paid_order_statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );

    $results = $wpdb->get_col( "
        SELECT p.ID FROM {$wpdb->prefix}posts AS p
        INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
        WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $paid_order_statuses ) . "' )
        AND p.post_type LIKE 'shop_order'
        AND pm.meta_key = '_customer_user'
        AND pm.meta_value = $customer_id
    " );

    // Count number of orders and return a boolean value depending if higher than 0
    return count( $results ) > 0 ? true : false;
}

add_action( 'woocommerce_before_calculate_totals', 'enable_customer_fidelity_discount', 10, 1 );
function enable_customer_fidelity_discount( $cart ) {
    if ( ! ( is_cart() || is_checkout() ) )
        return;

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

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // The discount coupon code below
    $coupon_code = 'summer';

    if( ! in_array( $coupon_code, $cart->get_applied_coupons() ) && has_bought() ) {
        $cart->apply_coupon( $coupon_code );
    } elseif( in_array( $coupon_code, $cart->get_applied_coupons() ) && ! has_bought() ) {
        $cart->remove_coupon( $coupon_code );
    }
}

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试和工作.

有关:

> Checking if customer has already bought something in WooCommerce
> Auto apply coupon only one time per user based on total spent in WooCommerce

标签:coupon,cart,woocommerce,wordpress,php
来源: https://codeday.me/bug/20191210/2104524.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有