【问题标题】:Woocommerce admin Orders: Check if user has already purchased itemsWoocommerce 管理员订单:检查用户是否已经购买了商品
【发布时间】:2021-01-27 04:29:25
【问题描述】:

我有下面的代码,我将它作为插件运行。我想检查用户(访客或注册用户)是否两次订购了相同的产品并收到通知,但它不起作用。

有什么想法吗?

我的代码是:

add_action('manage_shop_order_posts_custom_column', 'woocommerce_admin_orders_frequent_customer_flash_notice_flash_notice_in_order_number_column', 10, 2);

function woocommerce_admin_orders_frequent_customer_flash_notice_flash_notice_in_order_number_column($column) {
    global $post;
    switch ($column) {
        case 'order_number' :
            $dateFrom = date('Y-m-d', strtotime("-1 days")) . " 12:00";
            $dateTo = date('Y-m-d') . " 12:00";

            $query = new WP_Query(  array(
                'numberposts' => -1,
                'meta_key'    => '_billing_email',
                'meta_value'  => get_post_meta($post->ID, '_billing_email'),
                'post_type'   => wc_get_order_types(),
                'post_status' => array_keys( wc_get_order_statuses() ),
                'date_query' => array(array('before' => $dateTo, 'after' => $dateFrom), 'inclusive' => true)
            )  );
            
            $count = $query->found_posts;
                        
            if ($count > 1) {
                $count -= 1;
                echo "<span style='white-space: nowrap' class='redblink'>$count other orders between 12PM yesterday and 12PM today</span>";
            }
        break;
    }
}

add_action('admin_head', 'woocommerce_admin_orders_frequent_customer_flash_notice_admin_styles');
function woocommerce_admin_orders_frequent_customer_flash_notice_admin_styles() {
  echo '<style>
    .post-type-shop_order tr.type-shop_order td.order_number { position: relative; }
    .post-type-shop_order tr.type-shop_order td.order_number .redblink { color: red; position: absolute; bottom: 0; animation: blinker 1s linear infinite; }
    @keyframes blinker {
          50% {
            opacity: 0;
          }
        }
  </style>';
}

【问题讨论】:

  • 请对以下答案提供一些反馈。

标签: php wordpress woocommerce hook-woocommerce orders


【解决方案1】:

您需要一些不同的东西来检查用户是否再次订购了相同的产品。

在下面的代码中,我使用一个 foreach 循环来获取订单商品 (product id(s)) 和一个自定义函数来检查订单商品之前是否已购买。然后如果是这种情况会显示闪烁的通知:

function has_bought_items_before( $user_var = 0, $product_ids = 0, $current_order = false ) {
    global $wpdb;

    // Based on user ID (registered users)
    if ( is_numeric( $user_var) ) {
        $meta_key       = '_customer_user';
        $meta_value     = $user_var == 0 ? (int) get_current_user_id() : (int) $user_var;
    }
    // Based on billing email (Guest users)
    else {
        $meta_key       = '_billing_email';
        $meta_value     = sanitize_email( $user_var );
    }

    $paid_statuses      = array_map( 'esc_sql', wc_get_is_paid_statuses() );
    $product_ids        = is_array( $product_ids ) ? implode(',', $product_ids) : $product_ids;

    $line_meta_value    = $product_ids !=  ( 0 || '' ) ? 'AND woim.meta_value IN ('.$product_ids.')' : 'AND woim.meta_value != 0';
    $line_current_order = $current_order ? 'AND woi.order_id != ' . $current_order->get_id() .
        ' AND UNIX_TIMESTAMP(p.post_date) < ' . strtotime( get_the_date( 'Y-m-d H:i:s', $current_order->get_id() ) ) : '';

    // Count the number of products
    $count = $wpdb->get_var( "
        SELECT COUNT(p.ID) FROM {$wpdb->prefix}posts AS p
        INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
        WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $paid_statuses ) . "' )
        AND pm.meta_key = '$meta_key'
        AND pm.meta_value = '$meta_value'
        AND woim.meta_key IN ( '_product_id', '_variation_id' ) $line_meta_value $line_current_order
    " );

    // Return true if count is higher than 0 (or false)
    return $count > 0 ? true : false;
}


add_action( 'manage_shop_order_posts_custom_column', 'woocommerce_admin_orders_product_purchased_before' );
function woocommerce_admin_orders_product_purchased_before( $column ) {
    global $post;

    if ( $column === 'order_number' ) {
        $order = wc_get_order($post->ID); // Get the WC_Order Object

        if( is_a($order, 'WC_Order' ) && in_array( $order->get_status(), ['processing', 'completed']) ) {
            $user_id     = $order->get_user_id(); // The customer Id
            $email       = $order->get_billing_email(); // The billing email
            $user_var    = $user_id > 0 ? $user_id : $email;
            $product_ids = [];

            // Loop through order items
            foreach( $order->get_items() as $item ) {
                $product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();

                // Check if current item has been already purchased before
                if ( has_bought_items_before( $user_var, $product_id, $order ) ) {
                    $product_ids[] = $product_id;
                }
            }

            if ( count($product_ids) > 0 ) {
                $text_string = sprintf( _n("Product id %s has", "Products ids %s have", count($product_ids), "woocommerce"), implode(', ', $product_ids ) );
                $text_string = sprintf( __("%s been purchased before", "woocommerce"), $text_string );
                echo '<span style="white-space: nowrap" class="redblink">' . $text_string . ' ' . $order->get_status() .'</span>';
            }
        }
    }
}

add_action('admin_head', 'woocommerce_admin_orders_frequent_customer_flash_notice_admin_styles');
function woocommerce_admin_orders_frequent_customer_flash_notice_admin_styles() {
  echo '<style>
    .post-type-shop_order tr.type-shop_order td.order_number { position: relative; }
    .post-type-shop_order tr.type-shop_order td.order_number .redblink { color: red; position: absolute; bottom: 0; animation: blinker 1s linear infinite; }
    @keyframes blinker {
          50% {
            opacity: 0;
          }
        }
  </style>';
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

相关:Check if a user has purchased specific products in WooCommerce

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 2016-12-10
    • 2022-12-21
    • 2016-12-16
    • 2017-05-06
    • 2018-03-26
    • 2019-02-27
    相关资源
    最近更新 更多