product with Variation
Thu Oct 05 2023 09:27:30 GMT+0000 (Coordinated Universal Time)
Saved by @hamzakhan123
function product_loop() { ob_start(); ?> <div class="woocommerceSlider"> <?php $arg = array( 'post_type' => 'product', 'posts_per_page' => -1, ); $pro = new WP_Query($arg); ?> <?php if ($pro->have_posts()) : ?> <?php while ($pro->have_posts()) : ?> <?php $pro->the_post(); global $product; $eid = get_the_ID(); ?> <div class="woocommerceWrapper"> <div class="woocommerceImg"> <a href="<?php echo get_permalink($product->get_id()); ?>"> <?php the_post_thumbnail('full'); ?> </a> </div> <div class="woocommerceContent"> <a href="<?php echo get_permalink($product->get_id()); ?>"> <h2><?php echo $product->get_title(); ?></h2> </a> <?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; } echo "$" . ($regular_price + $sales_price); } ?> </div> <a class="addToCartBtn" href="<?php echo get_site_url(); ?>/cart/?add-to-cart=<?php echo $product->get_ID(); ?>">Add To Cart</a> </div> <?php endwhile; ?> <?php endif; ?> </div> <?php wp_reset_postdata(); return ob_get_clean(); } add_shortcode('product_code', 'product_loop'); function products_loop() { ob_start(); ?> <div class="woocommerceSliders"> <?php $arg = array( 'post_type' => 'product', 'posts_per_page' => -1, ); $pro = new WP_Query($arg); ?> <?php if ($pro->have_posts()) : ?> <?php while ($pro->have_posts()) : ?> <?php $pro->the_post(); global $product; $eid = get_the_ID(); ?> <div class="woocommerceWrapper"> <div class="woocommerceImg"> <a href="<?php echo get_permalink($product->get_id()); ?>"> <?php the_post_thumbnail('full'); ?> </a> </div> <div class="woocommerceContent"> <a href="<?php echo get_permalink($product->get_id()); ?>"> <h2><?php echo $product->get_title(); ?></h2> </a> <?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; } echo "$" . ($regular_price + $sales_price); } ?> </div> <a class="addToCartBtn" href="<?php echo get_site_url(); ?>/cart/?add-to-cart=<?php echo $product->get_ID(); ?>">Add To Cart</a> </div> <?php endwhile; ?> <?php endif; ?> </div> <?php wp_reset_postdata(); return ob_get_clean(); } add_shortcode('product_codes', 'products_loop');
Comments