Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将产品添加到类别_Php_Wordpress - Fatal编程技术网

Php 将产品添加到类别

Php 将产品添加到类别,php,wordpress,Php,Wordpress,我正在检索登录客户之前订购的所有产品,并将其显示在页面上。这个很好用 现在,我想将所有这些产品分配到一个临时类别,以显示在产品表上,该表通过类别检索产品 我有这个代码,它从客户那里获取所有产品。如何更新类别 add_shortcode( 'my_purchased_products', 'products_bought_by_curr_user' ); function products_bought_by_curr_user() { // GET CURR USER

我正在检索登录客户之前订购的所有产品,并将其显示在页面上。这个很好用

现在,我想将所有这些产品分配到一个临时类别,以显示在产品表上,该表通过类别检索产品

我有这个代码,它从客户那里获取所有产品。如何更新类别

add_shortcode( 'my_purchased_products', 'products_bought_by_curr_user' );
   
function products_bought_by_curr_user() {
   
    // GET CURR USER
    $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() ),
    ) );
   
    // LOOP THROUGH ORDERS AND GET PRODUCT IDS
    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 );
    
    // PASS PRODUCT IDS TO PRODUCTS SHORTCODE
    return do_shortcode("[products ids='$product_ids_str']");
    
}
我想我需要用像这样的东西

$term_ids = [ 819 ]; // 819 category ID
wp_set_object_terms( $product_ids, $term_ids, 'product_cat', true );

有人能帮忙吗?

确保用您正在使用的编程语言标记您的问题。