Get all Products by Custom Field ACF Id and Value in Woocommerce ?

Thảo luận trong 'Mã nguồn mở Wordpress' bắt đầu bởi seolagi, 28/10/21.

  1. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,028
    Đã được thích:
    80
    Điểm thành tích:
    48
    Get all Products by Custom Field ACF Id and Value in Woocommerce ? mình muốn lấy danh sách tất cả các sản phẩm theo Field ACF nhập vào. VD: mình có cái nhóm sản phẩm: group-product = 12, mình nhập một số sản phẩm có group-product = 12 và lấy nó ra trong code ở funtion.php để xử lý thì làm ntn.
    Mình làm giống trên mạng mà nó không ra
    Mã:
    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'meta_key' => 'group-product', //Your Custom field name
        'meta_value' => '12 ', //Custom field value
        'meta_compare' => '='
    );
    
    $the_query = new WP_Query( $args );
    $count = $the_query->post_count;
    if ( $the_query->have_posts() ) :
        while ( $the_query->have_posts() ) : $the_query->the_post();
            $the_query->the_post();
            echo '<li>' . get_the_title() . '</li>';
        endwhile;
    endif;
     
    Cảm ơn đã xem bài:

    Get all Products by Custom Field ACF Id and Value in Woocommerce ?

  2. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,895
    Đã được thích:
    1,198
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Code trên mình thấy đúng, sao không ra kết quả được, hoặc bạn sử dụng code sau nhé, mình đã sử dụng qua và lấy được giá trị bình thường.
    PHP:
    $args = array(
      
    'post_type' => 'product',
      
    'meta_key' => 'group-product'//feild name
      
    'meta_value' => array('nhaxaydung'), //'meta_value' => array('yes'), feild value
      
    'meta_compare' => 'IN' //'meta_compare' => 'NOT IN'
    );
    $productslist wc_get_products($args);//lấy danh sách sản phẩm
    foreach ($productslist as $pro) {
      
    //xử lý sản phẩm lấy được ở đây
      // $pro->get_id();
      //$pro->get_permalink()
      //get_the_post_thumbnail($pro->get_id(), 'thumnail', array( 'class' =>'thumnail') )
      //$pro->get_title()
      //$pro->get_price_html()
    }
     
    seolagi thích bài này.
  3. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,028
    Đã được thích:
    80
    Điểm thành tích:
    48
    Em đã lấy được danh sách tất cả sản phẩm như admin chỉ rồi, cảm ơn ad rất nhiều