Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 电子商务中支付客户的自动折扣_Php_Wordpress_Woocommerce_Cart_Coupon - Fatal编程技术网

Php 电子商务中支付客户的自动折扣

Php 电子商务中支付客户的自动折扣,php,wordpress,woocommerce,cart,coupon,Php,Wordpress,Woocommerce,Cart,Coupon,我想将折扣券自动应用于在我的网站上至少购买过一次的所有人。这是我尝试过的代码,但是我在页面上遇到了一个致命错误 function has_bought( $customer_email ){ $orders = get_posts(array( 'numberposts' => -1, 'post_type' => 'shop_order', 'post_status' => array('wc-completed'),

我想将折扣券自动应用于在我的网站上至少购买过一次的所有人。这是我尝试过的代码,但是我在页面上遇到了一个致命错误

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\u Customer
is\u paying\u 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文件。测试和工作


相关的:


我还尝试了您在55822058这篇文章中输入的代码#55822058,我从这行中得到语法错误//如果客户已经使用了优惠券,我们非常感谢您的回答@LoicTheAztec!我尝试了这两种代码,但都不起作用,优惠券不适用。我发现,如果我从你的代码中删除这一行,它就会工作(尽管我收到许多优惠券消息,并尝试自动结束购买)“如果(!(Is_cart()| | Is_checkout())返回;”