WooCommerce - wc_customer_bought_product()或$ order-> get_items()

时间:2015-05-13 14:02:08

标签: php woocommerce

我在我的智慧结束。我需要根据客户的订阅在客户的帐户页面上显示某些产品。示例:如果客户订阅了产品#517,但随后切换到产品#5910,我希望他们的电子邮件和帐户页面显示与#5910相关联的数据($ menu_listing)。

在他们的电子邮件中,我可以使用$ order-> get_items()实现此目的:

add_filter( 'woocommerce_email_order_meta', 'add_hgf_to_order');

function add_hgf_to_order( $order_id ) {
global $posts; 
global $woocommerce;

$order = new WC_Order( $order_id );
$user_id = (int)$order->user_id;
$items = $order->get_items();

   foreach ($items as $item) {

       if ( $item['product_id' ]== 517 || $item['product_id' ]== 5938 ) {
       $menu_listing = "menu_listing";

       } elseif ( $item['product_id' ]== 5910 || $item['product_id' ]== 5915 ) {
       $menu_listing = "menu_listing_c"; 

       } elseif ( $item['product_id' ]== 5934 || $item['product_id' ]== 5926 ) {
       $menu_listing = "menu_listing_a"; 
       }

   }

 return $order_id;    

}

虽然$ order-> get_items()在'woocommerce_email_order_meta'的功能上正常工作,但它不适用于客户的我的帐户页面。相反,我能够使用wc_customer_bought_product()将数据调用到页面上,但是它没有显示客户最近的购买:

add_filter( 'woocommerce_after_my_account', 'hgf_customer_orders' );

function hgf_customer_orders() {

global $current_user;
global $posts; 
global $woocommerce;

    $email = $current_user->email;

    if ( wc_customer_bought_product( $email, $current_user->ID, 517 ) || wc_customer_bought_product( $email, $current_user->ID, 5938) )  {
    $menu_listing = "menu_listing";

    } elseif ( wc_customer_bought_product( $email, $current_user->ID, 5910 ) || wc_customer_bought_product( $email, $current_user->ID, 5915) )  {
    $menu_listing = "menu_listing_c";

    } elseif ( wc_customer_bought_product( $email, $current_user->ID, 5934 ) || wc_customer_bought_product( $email, $current_user->ID, 5926) )  {
    $menu_listing = "menu_listing_a";

    } else {
    return null;
    }

}

它正在做什么(使用顶部的示例),显示产品#517而不是#5910。所以我要么能够显示wc_customer_bought_product()的最新购买,要么让$ order-> get_items()函数在用户的“我的帐户”页面上正常工作。

任何人都能帮我排除故障吗?

1 个答案:

答案 0 :(得分:0)

最后,我通过从WooCommerce>中提取代码找到了解决方案。模板> my-order.php围绕我的foreach()声明:

add_filter( 'woocommerce_after_my_account', 'hgf_customer_orders' );

function hgf_customer_orders() {
  wp_reset_query();

      global $posts; 
      global $woocommerce;

        $customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
            'numberposts' => 1,
            'meta_key'    => '_customer_user',
            'meta_value'  => get_current_user_id(),
            'post_type'   => wc_get_order_types( 'view-orders' ),
            'post_status' => array_keys( wc_get_order_statuses() )
        ) ) );

    if ( $customer_orders ){

        foreach ( $customer_orders as $customer_order ) {
            $order      = wc_get_order();
            $order->populate( $customer_order );
            $items = $order->get_items();

            //      echo $order->get_order_number();

            foreach ($items as $item) {

          if ( $item['product_id' ]== 517 || $item['product_id' ]== 5938 ) {
          $menu_listing = "menu_listing";

          } elseif ( $item['product_id' ]== 5910 || $item['product_id' ]== 5915 ) {
          $menu_listing = "menu_listing_c"; 

          } elseif ( $item['product_id' ]== 5934 || $item['product_id' ]== 5926 ) {
          $menu_listing = "menu_listing_a"; 
          }

                }

        }

        } // Close for if ( $customer_orders )

        echo $menu_listing;
}