Присвоить все категории продуктов всем программным продуктам - PullRequest
0 голосов
/ 04 июня 2019

У меня есть 32 категории и 1000 товаров на моем сайте ... но мой клиент хочет добавить категорию Все товары, и в этой категории он хочет показать все товары, поэтому, когда пользователь нажимает на эту категорию, все товары показывают

i have try this like get product category and them get the all prodcut category term_id and then use the
wp_set_object_terms( $product_id, $term_ids, 'product_cat' ); 
but does not work
this is the code 
$product_id = 5558; 
$term_ids = [ 130, 12, 18 ];
wp_set_object_terms( $product_id, $term_ids, 'product_cat' );

no error that code is not add that category to this product

1 Ответ

0 голосов
/ 04 июня 2019

Вы можете попробовать это

<?php 

//Get all Terms
$args_cat = array(
    'taxonomy'   => "product_cat",
     'hide_empty' => false,
);
$product_categories = get_terms( $args_cat );

$term_ids = array();
foreach ( $product_categories as $product_categories_key => $product_categories_value ) {
   $term_ids[] = $product_categories_value->term_id;
}


//Get all Products
$args_product = array(
    'post_type'      => 'product',
    'posts_per_page' => -1,
);

$loop_product = new WP_Query( $args_product );

while ( $loop_product->have_posts() ) : $loop_product->the_post();

    global $product;
    $id = $product->get_id();

    wp_set_object_terms( $id, $term_ids, 'product_cat' );

    echo get_the_title();

endwhile;

wp_reset_query();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...