Woocommerce Custom Category with Shortcode

PHOTO EMBED

Thu Aug 22 2024 00:09:28 GMT+0000 (Coordinated Universal Time)

Saved by @wasim_mm1

<?php 
// ADD SHORTCODE [latest_products category='banner' type='' taxonomy='product_cat' posts_per_page="1"];
add_shortcode('latest_products', 'codex_latest_products');
function codex_latest_products($atts)
{

  ob_start();
  extract(shortcode_atts(
    array(
      'category' => '',
      'taxonomy' => '',
      'type' => 'product',
      'posts_per_page' => '12',
  ), $atts));
  ?>
<section id="home">
   <div class="container-fluid">
      <div class="row ser-content product-slider">
         <?php
           // WP_Query arguments;
           $category = explode(',', $category);  
           $args = array(
             'post_type'             => $type,
             'post_status'           => 'publish',
             'order'                 => 'DESC',
             'posts_per_page'        =>  $posts_per_page,
             'tax_query' => array(
               array(
                 'taxonomy'      => $taxonomy,
                 'field'         => 'slug',
                 'terms'         => $category,
               )
             )
           );

           // echo "<pre>";
           // print_r($args);

         $po = new WP_Query($args);
         ?>
         <?php if ($po->have_posts()) : ?>
             <?php while ($po->have_posts()) : ?>
                 <?php $po->the_post(); ?>
         <div class="col-md-3">
            <div class="product_col">
               <div class="product_inner">
                  <div class="product_img">
                     <?php the_post_thumbnail('full'); ?>
                  </div>
                  <div class="product_title">
                     <h4><?php echo esc_html(get_the_title()); ?></h4>
                  </div>
                  <div class="product_bottom">
                     <div class="detail-btn"><a class="prCart" href="<?php echo esc_url( get_site_url() . '/cart/?add-to-cart=' . $product->get_id() ); ?>">See Detail<span class="list-icon wd-icon far fa-eye"></span></a></div>
                     <div class="price_product">
                        <?php 
                           if ($product->is_type( 'simple' )) { ?>
                        <?php echo $product->get_price_html(); ?>
                        <?php } ?>
                        <?php 
                           if($product->get_type()=='variable') {
                               $available_variations = $product->get_available_variations();
                               $variation_id=$available_variations[0]['variation_id'];
                               $variable_product1= new WC_Product_Variation( $variation_id );
                               $regular_price = $variable_product1 ->regular_price;
                               $sales_price = $variable_product1 -> sale_price;
                               if (empty($sales_price)) {
                               $sales_price = 0;
                                 }
                               }
                           ?>
                     </div>
                  </div>
               </div>
            </div>
         </div>
         <?php
            endwhile;
            endif;
            ?>
      </div>
   </div>
</section>
<?php
wp_reset_postdata();
return ob_get_clean();
}
content_copyCOPY