【问题标题】:Hide product from Woocommerce Shop page if it has already been purchased如果已经购买,则从 Woocommerce 商店页面隐藏产品
【发布时间】:2021-04-07 13:42:08
【问题描述】:

我正在尝试找到一种从 Woocommerce 商店页面隐藏产品的方法。如果登录的用户已经购买了该产品。

我已经能够使用woocommerce_is_purchasablewoocommerce_variation_is_purchasable 阻止用户再次购买。但我想把它全部隐藏起来,甚至在商店页面中也没有向他们展示。我一直空着这个。因此,我们将不胜感激。

【问题讨论】:

  • 是对所有用户隐藏还是只对当前用户隐藏?
  • 如果登录用户已经购买了产品,需要隐藏。为清晰起见进行了编辑。

标签: wordpress woocommerce


【解决方案1】:

您可以使用pre_get_posts 操作挂钩更改产品查询,然后您可以获得他们已经购买的当前用户产品。将所有产品 ID 传递给 post__not_in。检查下面的代码。代码会去激活主题function.php文件。

add_action( 'pre_get_posts', 'hide_product_from_shop_page_if_user_already_purchased', 20 );

function hide_product_from_shop_page_if_user_already_purchased( $query ) {
   
    if ( ! $query->is_main_query() ) return;
   
    if ( ! is_admin() && is_shop() ) {

        $current_user = wp_get_current_user();
        if ( 0 == $current_user->ID ) return;
       
        $customer_orders = get_posts( array(
            'numberposts' => -1,
            'meta_key'    => '_customer_user',
            'meta_value'  => $current_user->ID,
            'post_type'   => 'shop_order',
            'post_status' => array( 'wc-processing', 'wc-completed' ),
        ) );
       
        if ( ! $customer_orders ) return;
        
        $product_ids = array();
        
        foreach ( $customer_orders as $customer_order ) {
            $order = wc_get_order( $customer_order->ID );
            if( $order ){
                $items = $order->get_items();
                foreach ( $items as $item ) {
                    $product_id    = $item->get_product_id();
                    $product_ids[] = $product_id;
                }
            }
        }

        $product_ids = array_unique( $product_ids );

        $query->set( 'post__not_in', $product_ids );
    }

}

【讨论】:

  • 它几乎开箱即用。我不得不将 'post_status' => array( 'wc-processing', 'completed' ) 更改为 wc-completed。再次感谢!。我将标记为已接受。也许您想为找到此答案的其他人进行编辑?
  • 欢迎...对不起,我的错。我更新了我的答案。
【解决方案2】:

根据所提出的问题,Bhautik 的回答在技术上是正确的。如果用户之前购买过,则隐藏产品。

但是,正如我的问题中提到的,我已经禁止使用 woocommerce_is_purchasable 购买上述产品。

意味着应该隐藏的逻辑已经在软件中。使用他的想法作为模板,我使答案更加通用,可以在woocommerce_is_purchasable 之外进行操作。这意味着我只需在该函数中实现任何条件逻辑,商店将通过隐藏所述产品(不可购买的)来反映。

我将其发布为第二个答案,以防它帮助其他人搜索此问题。但会保留他作为问题的选定答案。

add_action( 'woocommerce_product_query', 'hide_product_from_shop_page_if_user_already_purchased', 20 );

function hide_product_from_shop_page_if_user_already_purchased( $query ) {
  //Get all the products in the store.
  $products = wc_get_products(['limit' => -1]);

  //Blank exclusion array. 
  $product_ids = array();

  //Check each product to see if it is purchasable or not.
  foreach($products as $product){
    if(!$product->is_purchasable()){
      //If product is not purchasable then add to exclusion array.
      $product_ids[] = $product->get_id();
    }
  }
    //Use array of id's to exclude products in the shop page.
    $query->set( 'post__not_in', $product_ids );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 2014-05-27
    相关资源
    最近更新 更多