我已经向我的帐户添加了一个自定义终结点"推荐图书".使用快捷码,我显示了一个产品列表.我想隐藏用户已经购买的产品.但我所有的try 都没有奏效.

// That's how I added the Recommended books endpoint to my account
 add_filter ( 'woocommerce_account_menu_items', 'recommended_books_link', 25 );
 function recommended_books_link( $menu_links ){

$menu_links = array_slice( $menu_links, 0, 1, true ) + array( 'recommended-books' => 'Recommended books' ) + 
array_slice( $menu_links, 1, NULL, true );
return $menu_links;

 }

add_action( 'init', 'you_add_endpoints', 25 );
  function you_add_endpoints() {

add_rewrite_endpoint( 'recommended-books', EP_PAGES );

  }

add_action( 'woocommerce_account_recommended-books_endpoint', 'you_contents', 25 );
function you_contents() {
echo '<div class="sav" style= "margin-bottom: 25px;" > <h1>Recommended books_link</h1> 
</div>';
echo do_shortcode('[products limit="10" columns="1" paginate="true"]');

 }

这是我用来隐藏用户购买的产品的代码.但它不起作用...

 //Hide product
 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_page() && 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 );
   }

} 

推荐答案

不可能通过woocommerce_shortcode_products_query专用挂钩将post__not_in英寸的产品从WooCommerce短码[products]的产品查询中排除.

所以我所做的是,我已经获得了除客户购买的产品之外的所有产品ID,并使用ids参数将其插入到短码中:

// Utiility function: Get all unpurchased product Ids
function get_unpurchased_products_ids( $current_user ) {
    // Get Customer orders
    $customer_orders = wc_get_orders( array(
        'limit'         => -1,
        'customer'      => $current_user->ID,
        'status'        => wc_get_is_paid_statuses(),
    ) );

    $customer_product_ids = array(); // Initializing

    if ( count($customer_orders) > 0 ) {
        foreach ( $customer_orders as $customer_order ) {
            $order = wc_get_order( $customer_order->get_id() );

            foreach ( $order->get_items() as $item ) {
                $customer_product_ids[] = $item->get_product_id();
            }
        }
    }
    // Get all unpurchased product ids
    $product_ids  = wc_get_products( array(
        'status'  => 'publish',
        'limit'   => -1,
        'exclude' => array_unique($customer_product_ids),
        'return'  => 'ids',
    ) );
    // return a coma separated string of unpurchased product Ids
    return implode(',', $product_ids);
}

// That's how I added the Recommended books endpoint to my account
add_filter ( 'woocommerce_account_menu_items', 'recommended_books_link', 25 );
function recommended_books_link( $menu_links ){
    $menu_links = array_slice( $menu_links, 0, 1, true ) + array( 'recommended-books' => 'Recommended books' ) + 
    array_slice( $menu_links, 1, NULL, true );
    return $menu_links;
}

add_action( 'init', 'you_add_endpoints', 25 );
function you_add_endpoints() {
    add_rewrite_endpoint( 'recommended-books', EP_PAGES );
}

add_action( 'woocommerce_account_recommended-books_endpoint', 'recommended_book_endpoint_content', 25 );
function recommended_book_endpoint_content() {
    global $current_user;

    echo '<div class="sav" style= "margin-bottom: 25px;">
        <h1>Recommended books_link</h1>
    </div>';

    $product_ids = get_unpurchased_products_ids( $current_user );   

    echo do_shortcode("[products limit='20' columns='1' paginate='true' ids='{$product_ids}']");
}

经过测试,效果良好.

Php相关问答推荐

Postgrid Webhook不叫laravel

邮箱打开跟踪器不工作时发送邮件使用laravel调度程序

自定义WooCommerce帐户下载列表

在Laravel Eloquent中实现一对多关系

在WooCommerce中设置带有国家/地区和年份前缀的顺序序号

将一个按钮添加到WooCommerce产品的管理视图中,该按钮链接到一个自定义的WebHook,并在末尾附加产品ID

基于自定义域的每个购物车项目的WooCommerce定制数量输入步骤

模仿GUZLE中的curl 工作脚本(分块数据和二进制上传一起)

在Laravel中创建辅助函数时的约定

随机显示WordPress快捷代码功能时出现问题

PHP 8.2 弃用日志(log)存储在 Laravel 10.22.0 中的哪里?

对产品/库存结果集进行分组和计数,形成多维分层数组

WooCommerce 回购自定义插件:如何更新用户钱包?

多个产品类别 Select 自定义 WooCommerce 插件设置页面

主机中未检索用户表数据

如何将文件上传添加到 WooCommerce 结帐?

避免在 WooCommerce 中多次触发挂钩函数

WooCommerce – 在新订单邮箱通知中显示产品分类计数

优化后的标题:如何在WooCommerce我的帐户编辑表单中添加账单字段

获取具有所有特定相关模型的模型