如何按采购订单的类别获取 Woocommerce 类似产品

时间:2021-02-12 18:40:57

标签: woocommerce

这个问题是关于 Woocommerce 我的账户页面。

我想要达到的是

从当前用户购买的产品中获取类别并显示该特定类别中的其他产品。所以让我们假设客户购买了 A 类、B 类、C 类中的 3 种产品。 我想展示每个类别的 1 个产品。

我是这样开始的。

function similar_products(){

$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) return;

// GET USER ORDERS (COMPLETED + PROCESSING)
$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();
    $terms = get_the_terms( $product_id, 'product_cat' );
    foreach ( $items as $item ) {
        $product_id = $item->get_product_id();
        $product_ids[] = $product_id;
    }      

    foreach ( $terms as $term ) {
        
        $product_cat_slug= $term->slug;
    }
    
}
$product_ids = array_unique( $product_ids );
$product_ids_str = implode( ",", $product_ids );

}

add_action('woocommerce_account_content', 'similar_products', 10);

0 个答案:

没有答案