【问题标题】:WooCommerce – wc_customer_bought_product() or $order->get_items()WooCommerce – wc_customer_bought_product() 或 $order->get_items()
【发布时间】:2015-05-13 14:02:08
【问题描述】:

我束手无策。我需要根据客户的订阅在客户的帐户页面上显示某些产品。示例:如果客户订阅了产品 #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() 函数在用户的“我的帐户”页面上正常工作。

谁能帮我解决这个问题?

【问题讨论】:

    标签: php woocommerce


    【解决方案1】:

    最后,我通过从 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      相关资源
      最近更新 更多