Get WooCommerce Product using different type single product and multiple products

 Get WooCommerce Product using a different type of single product with left side text and right side image, Right side image and left side image, and multiple products in grids. 


<?php

add_shortcode("custom_product", "wps_soqopslider");

function wps_soqopslider($atts){

?>

<div class="vc_row wpb_row vc_inner vc_row-fluid hme-prduct">


  <?php

  $args = [

      "post_type" => "product",

      "posts_per_page" => 1,

  "p"=>$atts['product_id'],

      "tax_query" => [

          [

              "taxonomy" => "product_visibility",

              "field" => "name",

              "terms" => "featured",

          ],

      ],

  ];

  $loop = new WP_Query($args);

  if ($loop->have_posts()) {

      while ($loop->have_posts()):


          $loop->the_post();

          $product = wc_get_product($product_id);

          ?>

    <div class="wpb_column vc_column_container vc_col-sm-4">

      <div class="vc_column-inner">

         <div class="wpb_wrapper">

            <div class="wpb_single_image wpb_content_element vc_align_center">

               <figure class="wpb_wrapper vc_figure">

                  <div class="vc_single_image-wrapper   vc_box_border_grey">

                    <?php $image = wp_get_attachment_image_src(

                        get_post_thumbnail_id($product_id),

                        "single-post-thumbnail"

                    ); ?>

       <img src="<?php echo $image[0]; ?>" data-id="<?php echo $loop->post

    ->ID; ?>">

</div>

               </figure>

            </div>

         </div>

      </div>

   </div>

   <div class="wpb_column vc_column_container vc_col-sm-8">

      <div class="vc_column-inner">

         <div class="wpb_wrapper">

            <h2  class="vc_custom_heading"><?php echo get_the_title(); ?> </h2>

            <ul class="ingredients">

              <li> <?php

              if (get_field("ingredients_4")): ?>

                        <img src="<?php the_field("ingredients_4"); ?>" />

                    <?php endif;

              echo esc_html(get_field("ingredients_1"));

              ?></li>  <li> <?php

                     if (get_field("ingredients_6")): ?>

                        <img src="<?php the_field("ingredients_6"); ?>" />

                    <?php endif;

                     echo esc_html(get_field("ingredients_3"));

                     ?></li>

                     <li> <?php

                     if (get_field("ingredients_5")): ?>

                        <img src="<?php the_field("ingredients_5"); ?>" />

                    <?php endif;

                     echo esc_html(get_field("ingredients_2"));

                     ?></li>

                   

<li> <?php

                     if (get_field("ingredients_7")): ?>

                        <img src="<?php the_field("ingredients_7"); ?>" />

                    <?php endif;

                     echo esc_html(get_field("ingredients_8"));

                     ?></li>

            </ul>

            <div class="wpb_text_column wpb_content_element ">

               <div class="wpb_wrapper">

                 <?php

                 echo the_excerpt();

                 woocommerce_template_loop_rating();

                 $post = $loop->post->ID;

                 $product = wc_get_product($post);

                 echo $product->get_price_html();

                 woocommerce_template_loop_add_to_cart();

                 ?>  

               </div>

            </div>

            

         </div>

      </div>

   </div>

    <?php

      endwhile;

  } else {

      echo __("No products found");

  }

  wp_reset_postdata();?>

</div>


<?php



add_shortcode("custom_product_display", "wps_custom_product_display");


function wps_custom_product_display($atts){

     ob_start();

?>

<div class="vc_row wpb_row vc_inner vc_row-fluid hme-prduct">


  <?php


  $args = [

      "post_type" => "product",

      "posts_per_page" => 1,

      "p"=>$atts['product_id'],

      "tax_query" => [

          [

              "taxonomy" => "product_visibility",

              "field" => "name",

              "terms" => "featured",

          ],

      ],

  ];

  $loop = new WP_Query($args);

  if ($loop->have_posts()) {

      while ($loop->have_posts()):


          $loop->the_post();

          $product = wc_get_product($product_id);

          ?>

    

   <div class="wpb_column vc_column_container vc_col-sm-8">

      <div class="vc_column-inner">

         <div class="wpb_wrapper">

            <h2  class="vc_custom_heading"><?php echo get_the_title(); ?> </h2>

            <ul class="ingredients">

              <li> <?php

              if (get_field("ingredients_4")): ?>

                        <img src="<?php the_field("ingredients_4"); ?>" />

                    <?php endif;

              echo esc_html(get_field("ingredients_1"));

              ?></li>  <li> <?php

                     if (get_field("ingredients_6")): ?>

                        <img src="<?php the_field("ingredients_6"); ?>" />

                    <?php endif;

                     echo esc_html(get_field("ingredients_3"));

                     ?></li>

                     <li> <?php

                     if (get_field("ingredients_5")): ?>

                        <img src="<?php the_field("ingredients_5"); ?>" />

                    <?php endif;

                     echo esc_html(get_field("ingredients_2"));

                     ?></li>

                   

                 <li> <?php

                     if (get_field("ingredients_7")): ?>

                        <img src="<?php the_field("ingredients_7"); ?>" />

                    <?php endif;

                     echo esc_html(get_field("ingredients_8"));

                     ?></li>

            </ul>

            <div class="wpb_text_column wpb_content_element ">

               <div class="wpb_wrapper">

                 <?php

                 echo the_excerpt();

                 woocommerce_template_loop_rating();

                 $post = $loop->post->ID;

                 $product = wc_get_product($post);

                 echo $product->get_price_html();

                 woocommerce_template_loop_add_to_cart();

                 ?>  

               </div>

            </div>

            

         </div>

      </div>

   </div>

   <div class="wpb_column vc_column_container vc_col-sm-4">

      <div class="vc_column-inner">

         <div class="wpb_wrapper">

            <div class="wpb_single_image wpb_content_element vc_align_center">

               <figure class="wpb_wrapper vc_figure">

                  <div class="vc_single_image-wrapper   vc_box_border_grey">

                    <?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($product_id),"single-post-thumbnail"); ?>

                        <img src="<?php echo $image[0]; ?>" data-id="<?php echo $loop->post->ID; ?>">

                  </div>

               </figure>

            </div>

         </div>

      </div>

   </div>

    <?php

      endwhile;

  } else {

      echo __("No products found");

  }

  wp_reset_postdata();?>

</div>


<?php

return ob_get_clean();

}



add_shortcode("custom_product_display_all", "wps_custom_product_display_all");


function wps_custom_product_display_all($atts){

    ob_start();

    ?>

    <div class="vc_row wpb_row vc_inner vc_row-fluid three_product_section">


    <?php

    $args = [

    "post_type"      => "product",

    "posts_per_page" => -1,

    "post_status"    => "publish",

    "tax_query"      => [

        [

            "taxonomy" => "product_visibility",

            "field"    => "name",

            "terms"    => "exclude-from-catalog",

            "operator" => "NOT IN"

        ],

    ],

];


$loop = new WP_Query($args);


    if ($loop->have_posts()) {

        while ($loop->have_posts()) {

            $loop->the_post();

            $product_id = get_the_ID();

            $product    = wc_get_product($product_id);

            $image      = wp_get_attachment_image_src(get_post_thumbnail_id($product_id), "single-post-thumbnail");

            ?>

            <div class="wpb_column vc_column_container vc_col-sm-4">

              <div class="vc_column-inner">

                <div class="wpb_wrapper">

                  <div class="vc_row wpb_row vc_inner vc_row-fluid">

                    <div class="wpb_column vc_column_container vc_col-sm-12">

                      <div class="vc_column-inner custom_border">

                        <div class="wpb_wrapper">

                        <div class="wpb_single_image wpb_content_element vc_align_center">

                            <figure class="wpb_wrapper vc_figure">

                                <div class="vc_single_image-wrapper vc_box_border_grey">

                                    <?php if ($image) : ?>

                                        <img src="<?php echo esc_url($image[0]); ?>" alt="<?php echo esc_attr(get_the_title()); ?>">

                                    <?php endif; ?>

                                </div>

                            </figure>

                        </div>

                        <h2 class="vc_custom_heading"><?php echo get_the_title(); ?> </h2>

                        <div class="wpb_text_column wpb_content_element">

                            <div class="wpb_wrapper">

                               <?php echo wp_trim_words(get_the_excerpt(), 15, '...'); ?>

                                <?php woocommerce_template_loop_rating(); ?>

                                <div class="product-price"><?php echo $product->get_price_html(); ?></div>

                                <?php woocommerce_template_loop_add_to_cart(); ?>

                            </div>

                        </div>

                    </div>

                  </div>

                </div>

              </div>

            </div>

          </div>

        </div>

        <?php

        }

    } else {

        echo __("No products found");

    }

    wp_reset_postdata();

    ?>

    </div>

    <?php

    return ob_get_clean();

}





Comments

Popular posts from this blog

Get Post Data From Simple Post custom shortcode

Top 20 Beautiful Pen For Writing

Fatal error: Maximum execution time of 300 seconds exceeded in C:\xampp\phpMyAdmin\libraries\classes\Dbi\DbiMysqli.php on line 199