首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使这个Woocommerce-短代码只显示某些类别

使这个Woocommerce-短代码只显示某些类别
EN

Stack Overflow用户
提问于 2022-01-24 11:20:27
回答 3查看 143关注 0票数 -1

我找到了这个函数片段,并将它插入到我的站点中。到目前为止,效果很好,但是我需要做一个小的改变:如何才能使函数显示成为一个特定的类别.?

以下是代码:

代码语言:javascript
复制
add_shortcode( 'my_purchased_products', 'products_bought_by_curr_user' );
   
function products_bought_by_curr_user() {
   
    $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'   => wc_get_order_types(),
        'post_status' => array_keys( wc_get_is_paid_statuses() ),
    ) );
   
    if ( ! $customer_orders ) return;
    $product_ids = array();
    foreach ( $customer_orders as $customer_order ) {
        $order = wc_get_order( $customer_order->ID );
        $items = $order->get_items();
        foreach ( $items as $item ) {
            $product_id = $item->get_product_id();
            $product_ids[] = $product_id;
        }
    }
    $product_ids = array_unique( $product_ids );
    $product_ids_str = implode( ",", $product_ids );
   
    return do_shortcode("[products ids='$product_ids_str']");
   
}

有人能把我推向正确的方向吗?

向安迪问好

EN

回答 3

Stack Overflow用户

发布于 2022-01-24 13:47:04

我误解了你的问题,所以现在是编辑时间。您将无法在get_posts()上过滤这个类别,因为您是获得订单而不是产品(这就是为什么我的解决方案不能工作)。

要实现过滤,您需要在以下部分进行工作:

代码语言:javascript
复制
    foreach ( $items as $item ) {
        $product_id = $item->get_product_id();
        $product_ids[] = $product_id;
    }

通过检查产品是否包含您希望从其中获取产品的类别。像这样的事情应该有效:

代码语言:javascript
复制
        foreach ( $items as $item ) {
        $product_id = $item->get_product_id();
        $terms = get_the_terms( $product_id, 'product_cat' );
    
        foreach ( $terms as $term ) {
            if($term->slug === 'the_category_slug') {
              $products_ids[] = $product_id;
            }
        }
    }

我使用了这个帖子How to get categories from an order at checkout in WooCommerce?的代码

所以这里发生的事情是,在获得product_id之后,我们使用get_terms获得产品类别( 'product_cat‘param是只获取产品类别,而不是产品标签,因为类别和标记都是术语)。

在得到术语后,我们循环进入它,并检查每一个是否是我们想要得到的类别)。如果是,我们将乘积id推送到结果数组。

我们在这里使用的是类别段塞,但是我们可以通过替换

代码语言:javascript
复制
if($term->slug === 'the_category_slug')

通过

代码语言:javascript
复制
if($term->term_id === 149)

其中149是所需类别的ID。

如果你还需要更多的帮助和好运,请告诉我。

票数 1
EN

Stack Overflow用户

发布于 2022-01-24 12:33:02

您可以按product获得产品类别。现在,您只需使用if条件来检查。

如果类别id是X,那么就做作业吧。

票数 0
EN

Stack Overflow用户

发布于 2022-01-25 15:09:26

要应对新的崩溃问题:

我建议您激活PHP (如果站点崩溃,它会显示屏幕上的WordPress错误)。您可以在项目根目录(项目的顶层文件夹)编辑wp-config.php文件,并将下面的行放在其中:

代码语言:javascript
复制
define( 'WP_DEBUG', true ); 

(你可以把它放在任何地方)。然后,你应该看看为什么你的网站崩溃。

我还没有时间测试代码,但我看到的是,您忘记了行中的$和;字符。

代码语言:javascript
复制
products_ids[] = $product_id

它应该成为

代码语言:javascript
复制
$products_ids[] = $product_id;

如果您在屏幕上看到一个错误,请返回并在这里发布:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70833033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档