【问题标题】:Get order items from a specific product categories in Woocommerce 3从 Woocommerce 3 中的特定产品类别获取订单商品
【发布时间】:2019-01-26 01:34:00
【问题描述】:

我如何才能在 Woocommerce 中获得仅来自某个产品类别的特定订单商品?

我在 Woocommerce 文档中进行了搜索,但没有找到任何内容。

这是我的实际代码:

<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

$order = wc_get_order($order_id);

if ( sizeof( $order->get_items() ) > 0 ) {

    foreach( $order->get_items() as $item ) {

        $_product =$order->get_product_from_item( $item );

        ?>

        <a href="<?php echo $_product->get_permalink() ?>"><?php echo $_product->get_title() ?></a>
        <br>

        <?php 
    }
}
?>

非常感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce custom-taxonomy orders


    【解决方案1】:

    您的代码中也存在一些错误……在以下代码中,您将定义您的产品类别,可以是术语 ID、slug 或名称(数组):

    <?php
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    // HERE define your product category(ies) in this array (can be term Ids, slugs or names)
    $categories = array('clothing')
    
    // Get an instance of the WC_Order Object
    $order = wc_get_order($order_id);
    
    if ( sizeof( $order->get_items() ) > 0 ) {
        foreach( $order->get_items() as $item ) {
            // Just for a defined product category
            if( has_term( $categories, 'product_cat', $item->get_product_id() ) ) {
                // Get an instance of the WC_Product Object
                $_product = $item->get_product();
                ?>
                <a href="<?php echo $_product->get_permalink() ?>"><?php echo $item->get_name() ?></a><br>
                <?php
            }
        }
    }
    ?>
    

    经过测试并且有效。

    【讨论】:

    • 这帮助我按照我需要的方式修改代码,非常感谢!
    猜你喜欢
    • 2021-11-07
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 2022-07-08
    • 2018-01-24
    • 2019-08-29
    相关资源
    最近更新 更多