hi, em muốn lấy danh sách sản phẩm products list theo danh mục sản phẩm id truyền vào hoặc, danh sách các id sản phẩm truyền vào dưới dạng shortcode thì như thế nào? bạn nào có code shortcode get Prodcut list by category id or list id products woocommerce thì cho mình với. Mình đang cần lấy danh sách sản phẩm theo id category và product list id. Lấy danh sách gồm hình, tên và giá là được rồi, cảm ơn
Của bạn cần lấy đây nhé: Lấy danh sách sản phẩm theo id list products truyền vào PHP: /*lay danh sach san pham tại landingpage*/function itseovn_productlistbylistid($args, $content) { $idlistarr = explode (",", $args['idlist']); $query_args = array( 'post_type' => 'product', 'include' => $idlistarr // array( 123, 234, 233, 111 ) //danh sách arrary id list sản phẩm ); $products = wc_get_products( $query_args ); $contents .= '<div class="cs-prolistlist">'; foreach ( $products as $pro ) { $textsale = ""; if($pro->is_on_sale() && $pro->product_type == 'simple'){ $percentage = round( ( ( $pro->regular_price - $pro->sale_price ) / $pro->regular_price ) * 100 ); $textsale = '<span class="cs-flashsalenew">SALE<br/>'.$percentage . '%</span>'; } $contents .= '<div class="item-product col-md-3">'; $contents .= $textsale; $contents .= '<a href="'.$pro->get_permalink().'">'; $contents .= get_the_post_thumbnail($pro->get_id(), 'thumnail', array( 'class' =>'thumnail') ); $contents .= '</a>'; $contents .= '<p><a href="'.$pro->get_permalink().'">'.$pro->get_title().'</a></p>'; $contents .= '<div class="price-product"><span>Giá: </span>'.$pro->get_price_html().'</div>'; $contents .= '<div class="add-to-cart-button cs-addtocartbutton">'; //$contents .= '<a class="qn_btn cs-addtocart" href="/?add-to-cart='.$pro->get_id().'">Mua ngay</a>'; $contents .= '<a href="?add-to-cart='.$pro->get_id().'" data-quantity="1" class="primary is-small mb-0 button product_type_simple add_to_cart_button ajax_add_to_cart is-flat" data-product_id="'.$pro->get_id().'" data-product_sku="" aria-label="Thêm sản phẩm vào giỏ hàng" rel="nofollow">Mua Ngay</a>'; //$contents .= '<a class="qn_btn cs-buynownew" href="/cart/?add-to-cart='.$pro->get_id().'">Mua ngay</a>'; $contents .= '</div>'; $contents .= '</div>'; } $contents .= '</div>'; return $contents;}add_shortcode('shortcode_productlistbylistid', 'itseovn_productlistbylistid');//[shortcode_productlistbylistid idlist="123, 234, 233, 111"] Để sử dụng bạn chỉ cần chèn code sau vào nơi cần trong giao diện web nhé: HTML: [shortcode_productlistbylistid idlist="123, 234, 233, 111"] Gọi trong code .php thì gọi PHP: <?php echo do_shortcode('[shortcode_productlistbylistid idlist="123, 234, 233, 111"]') ?> Lấy danh sách sản phẩm theo id category chuyên mục truyền vào PHP: /*lay danh sach san pham tại landingpage*/function itseovn_productlistbycateid($args, $content) { $getposts = new WP_Query(array( 'post_type'=>'product', 'post_status'=>'publish', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $args['cateid'] //id danh mục sản phẩm ) ), 'orderby' => 'ID', 'order' => 'DESC', 'posts_per_page'=> '4')); $contents .= '<div class="cs-prolistbycateid">'; while ($getposts->have_posts()) : $getposts->the_post(); global $product; $contents .= '<div class="item-product col-md-3">'; $contents .= '<a href="'.$product->get_permalink().'">'; $contents .= get_the_post_thumbnail(get_the_ID(), 'thumnail', array( 'class' =>'thumnail') ); $contents .= '</a>'; $contents .= '<p><a href="'.$product->get_permalink().'">'.$product->get_title().'</a></p>'; $contents .= '<div class="price-product">'.$product->get_price_html().'</div>'; $contents .= '<div class="add-to-cart-button cs-addtocartbutton">'; $contents .= '<a class="qn_btn cs-addtocart" href="/?add-to-cart='.$product->get_id().'">Thêm vào giỏ</a>'; $contents .= '<a class="qn_btn cs-buynownew" href="/cart/?add-to-cart='.$product->get_id().'">Mua ngay</a>'; $contents .= '</div>'; $contents .= '</div>'; endwhile; $contents .= '</div>'; wp_reset_postdata(); return $contents;}add_shortcode('shortcode_productlistbycateid', 'itseovn_productlistbycateid');//[shortcode_productlistbycateid cateid="123"] Để sử dụng bạn chỉ cần chèn code sau vào nơi cần trong giao diện web nhé: HTML: [shortcode_productlistbycateid cateid="123"] Gọi trong code .php thì gọi PHP: <?php echo do_shortcode('[shortcode_productlistbycateid cateid="123"]') ?> Lấy id chuyên mục sản phẩm thì bạn tham khảo bài sau để lấy: https://itseovn.com/threads/lay-category-id-tu-id-cua-san-pham-get-category-id-by-product-id.322640/
Cảm ơn ad, code tuyệt vời ạ, em làm được rồi ạ, giờ muốn loại bỏ cái id của sản phẩm hiện tại ra thì làm như thế nào ad nhỉ?
Sửa đoạn code sau thành: PHP: $getposts = new WP_Query(array( 'post_type'=>'product', 'post_status'=>'publish', 'post__not_in' => array($post->ID), 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $args['cateid'] //id danh mục sản phẩm ) ), 'orderby' => 'ID', 'order' => 'DESC', 'posts_per_page'=> '4')); Với: $post->ID là cái id hiện tại, nó sẽ loại bỏ khi lấy