Instantly share code, notes, and snippets.
Created
December 14, 2020 07:48
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
-
Save encoderit-arman/321c952f09349be50b4344f3ff465001 to your computer and use it in GitHub Desktop.
Get Woocommerce Related Products
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // get categories | |
| $terms = wp_get_post_terms($post->ID, 'product_cat'); | |
| foreach ($terms as $term) $cats_array[] = $term->term_id; | |
| $query_args = array('post__not_in' => array($post->ID), 'posts_per_page' => -1, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array( | |
| array( | |
| 'taxonomy' => 'product_cat', | |
| 'field' => 'id', | |
| 'terms' => $cats_array | |
| ) | |
| )); | |
| $product_data = new WP_Query($query_args); | |
| if ($product_data->have_posts()) { | |
| ?> | |
| <section class="p-5 Related-Products"> | |
| <div class="container"> | |
| <h3 class="text-center entry-summary-title mb-5">Related Products</h3> | |
| <div class="wrapper"> | |
| <div class="carousel"> | |
| <?php | |
| global $woocommerce; | |
| $added_product = []; | |
| $items = $woocommerce->cart->get_cart(); | |
| foreach ($items as $item => $values) { | |
| array_push($added_product, $values['product_id']); | |
| } | |
| // Standard loop | |
| while ($product_data->have_posts()) : $product_data->the_post(); | |
| // Your new HTML markup goes here | |
| // var_dump($product_data); | |
| $products = wc_get_product(get_the_ID()); | |
| $product_image = get_the_post_thumbnail_url($product_data->get_id()); | |
| $products_cat_check = $products->category_ids; | |
| $enquirenow = false; | |
| foreach ($products_cat_check as $category) { | |
| if ($category == 26) { | |
| $enquirenow = true; | |
| break; | |
| } | |
| } | |
| ?> | |
| <div class="mr-3"> | |
| <div class="product-item-hove card small-card card-item"> | |
| <div class="product-container product-container-featured"> | |
| <a href="<?php echo $products->slug; ?>" class="grid-link"> | |
| <div class="ImageOverlayCa"></div> | |
| <img src="<?php echo $product_image; ?>" class="featured-image"> | |
| </a> | |
| </div> | |
| <div class="card-body text-center"> | |
| <?php if ($enquirenow) { | |
| ?> | |
| <div class="product-name-title-enquire-now"> | |
| <a href="<?php echo $products->slug; ?>" class="title"><?php echo $products->name; ?></a> | |
| </div> | |
| <div class="product-action"> | |
| <a href="<?php echo site_url() . '/enquire-now/?product-name=' . $products->name; ?>" class="btn btn-add-cart">Enquire Now</a> | |
| </div> | |
| <?php | |
| } else { | |
| ?> | |
| <div class="product-name-title"> | |
| <a href="<?php echo $products->slug; ?>" class="title"><?php echo $products->name; ?></a> | |
| </div> | |
| <?php if (!empty($products->price)) { ?> | |
| <span class="price" data-price="<?php echo $products->price; ?>"> | |
| <?php echo get_woocommerce_currency_symbol(); ?><?php echo $products->price; ?> | |
| </span> | |
| <?php } ?> | |
| <div class="product-action"> | |
| <input type="hidden" class="total-quantity" value="1" name="quantity" title="Qty"> | |
| <?php if ($products->get_stock_quantity() > 0) { ?> | |
| <a style="<?php echo (in_array($products->post->ID, $added_product)) ? '' : 'display:none'; ?>" href="<?php echo site_url('cart'); ?>" class="btn bg-primary btn-add-cart add-cart-btn text-uppercase single-product-added-to-cart-<?php echo $products->post->ID; ?>"> | |
| View Cart | |
| </a> | |
| <button style="<?php echo (in_array($products->post->ID, $added_product)) ? 'display:none' : ''; ?>" name="add-to-cart" serialId="<?php echo $products->post->ID; ?>" data-qty="1" class="btn bg-primary btn-add-cart add-cart-btn add-to-cart single-product-add-to-cart-<?php echo $products->post->ID; ?>" id="cart"> | |
| <span class="add-to-cart-btn-hs" id="add-to-cart-btn-hs<?php echo $products->post->ID; ?>">ADD | |
| TO CART</span> <span class="added-to-cart text-success" id="added-to-cart<?php echo $products->post->ID; ?>"></span> | |
| <span class="add-spin-btn-hs" id="add-spin-btn-hs<?php echo $products->post->ID; ?>" style="display: none;"> | |
| <i class="fas fa-spinner fa-pulse"></i> | |
| </span> | |
| </button> | |
| <?php } else { ?> | |
| <a href="<?php echo site_url() . '/enquire-now/?product-name=' . $products->name; ?>" class="btn bg-primary btn-add-cart text-uppercase">Enquire Now</a> | |
| <?php } ?> | |
| </div> | |
| <?php } ?> | |
| </div> | |
| </div> | |
| </div> | |
| <?php | |
| endwhile; | |
| ?> | |
| </div> | |
| <a class="carouselnext buttom-slick"><i class="fas fa-angle-left"></i></a> | |
| <a class="carouselprev buttom-slick"><i class="fas fa-angle-right"></i></a> | |
| </div> | |
| </div> | |
| </section> | |
| <?php } ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment