【问题标题】:If Customer Has Never Bought, Add Product to Cart - Code Not Working如果客户从未购买过,请将产品添加到购物车 - 代码不起作用
【发布时间】:2019-10-16 19:07:33
【问题描述】:

我正在尝试实现一项功能,该功能将检查客户之前是否曾从我的商店购买过任何产品,如果没有 - 在首次购买时为他们提供免费的“注册礼物”。

我能够将产品自动添加到购物车中,但问题是在之后出现的 - 即使在客户购买后,产品也会继续添加到购物车中。

下面的代码 - 无法确定问题可能是什么。

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

function aaptc_add_product_to_cart() {
    if( ! has_bought() && ! is_admin() && is_user_logged_in() ) {
        $product_id = 2449;  // Product Id of the free product which will get added to cart
        $found  = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->get_id() == $product_id )
                    $found = true;
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }
    }    
}

add_action( 'init', 'aaptc_add_product_to_cart' );

**** 编辑 ****

根据提供的支持,我有一些新代码可以自动将产品添加到购物车,检查订单并在订单存在时删除产品。

有人可以确认下面的代码是否正确吗?

/*
* Automatically add product to cart
*/
function insta_add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 2449;  // Product Id of the free product which will get added to cart
        $found  = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->get_id() == $product_id )
                    $found = true;
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }
    }    
}
add_action( 'init', 'insta_add_product_to_cart' );

function remove_shirt_returning_customer() {
    $product_id = 2449;  // Product Id of the free product which will get added to cart
    $user_id = get_current_user_id();
    // Get orders by customer.
    $args = array(
        'customer_id' => $user_id,
    );
    $orders = wc_get_orders( $args );

    if ( !empty($orders) ) {
        WC()->cart->remove_cart_item( $product_id );
    }
}
add_action( 'init', 'remove_shirt_returning_customer' );

**** 编辑 ***

解决方案

所以之前的代码仍然给了我错误。下面的代码似乎有效(在我的本地主机环境中)。

/*
* Automatically add product to cart
*/
function insta_add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 51;  // Product Id of the free product which will get added to cart
        $found  = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->get_id() == $product_id )
                    $found = true;
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }
    }    
}
add_action( 'init', 'insta_add_product_to_cart' );

/*
* Remove item from cart if previous order exists
*/
function remove_shirt_returning_customer() {
        if ( ! is_admin() ) {
            $cart_items = WC()->cart->get_cart();
            $product_id = 51;  // Product Id of the free product which will get added to cart
            $user_id = get_current_user_id();
            // Get orders by customer.
            $args = array(
                'customer_id' => $user_id,
            );
            $orders = wc_get_orders( $args );
            foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                 if ( $cart_item['product_id'] == $product_id && !empty( $orders ) ) {
                      WC()->cart->remove_cart_item( $cart_item_key );
             }
        }
    }
}
add_action( 'init', 'remove_shirt_returning_customer' );

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您可以使用email addressuser id 检索客户的所有订单,而不是使用该自定义查询尝试使用wc_get_orders 函数

    // Get orders by customer with ID 12.
    $args = array(
        'customer_id' => 12,
    );
    $orders = wc_get_orders( $args );
    

    更多关于wp_get_orders的信息可以在here找到

    【讨论】:

    • 但是我需要脚本来检查每个客户。该代码适用于所有客户。还是我错过了什么?
    • 是的,只需更改变量$user_id的数字12@
    【解决方案2】:

    只有在用户登录帐户后,您才能知道他们之前是否曾向您订购过。因此,如果用户在您的网站上并已登录该帐户,您将必须找到which user,即:

    $user_id = get_current_user_id();
    

    从那里使用 Enrique 的回答来获取该客户是否已向您下订单。

    $user_id = get_current_user_id();
    $args = array(
        'customer_id' => 12,
    );
    $orders = wc_get_orders( $args );
    

    从那里,测试$orders 是否为空,如果为空,则将该商品作为默认值保留在购物车中,如果没有,则将其删除。

    更好的方法是自动将商品放入所有购物车,然后有条件地检查此用户是否已登录,并且之前已向您下过订单。如果是这样,请删除该项目。

    【讨论】:

    • 根据您和 Enrique 的回复,我已按照您的建议更新了代码。介意看一下,让我知道是否有任何不正确的地方吗?这应该工作吗?谢谢。
    • 是的,我的意思是它看起来不错,但您需要在您的开发环境中对其进行测试,看看会发生什么。这样做并在问题中再次发布你的结果,如果它不起作用。
    • 它没有用,但我设法进行了一些调整,现在似乎有效。看一看,让我知道是否有任何问题(讨厌乱七八糟的代码,但我不是最好的程序员——感谢所有帮助)。感谢大家的帮助!
    • 是的,另一件事是您可能想考虑使用与init 不同的钩子,我通常不使用init 作为动作钩子。我假设你在插件中构建它?使用init 并不可怕,但如果在其他 WP 和 WC 进程之前运行它可能会导致一些并发症。查看参考资料,看看你是否能找到另一个足够早地初始化它的参考资料,你只是想确保你的代码没有在任何核心 WP 或 WC 代码之前加载:developer.wordpress.org/plugins/hooks
    • 除此之外它看起来不错,如果它在您的测试环境中运行,那么您至少走在正确的道路上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多