default.php
314 lines
| 1 | <?php |
| 2 | |
| 3 | use ShopEngine\Utils\Helper; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | // product query |
| 7 | $args = [ |
| 8 | 'post_type' => 'product', |
| 9 | 'posts_per_page' => isset($products_per_page) ? $products_per_page : 6, |
| 10 | 'order' => isset($product_order) ? $product_order : 'DESC' |
| 11 | ]; |
| 12 | if( $out_of_stock_product_visibility == 'hide'|| ( $out_of_stock_product_visibility == 'default' && 'yes' == get_option('woocommerce_hide_out_of_stock_items'))){ |
| 13 | $args['meta_query'] [] = array( |
| 14 | 'key' => '_stock_status', |
| 15 | 'value' => 'outofstock', |
| 16 | 'compare' => '!=' |
| 17 | ); |
| 18 | } |
| 19 | if (isset($product_orderby)) { |
| 20 | switch ($product_orderby) { |
| 21 | case 'price': |
| 22 | $args['meta_key'] = '_price'; |
| 23 | $args['orderby'] = 'meta_value_num'; |
| 24 | break; |
| 25 | case 'sales': |
| 26 | $args['meta_key'] = 'total_sales'; |
| 27 | $args['orderby'] = 'meta_value_num'; |
| 28 | break; |
| 29 | case 'rated': |
| 30 | $args['meta_key'] = '_wc_average_rating'; |
| 31 | $args['orderby'] = 'meta_value_num'; |
| 32 | break; |
| 33 | case 'sku': |
| 34 | $args['meta_key'] = '_sku'; |
| 35 | $args['orderby'] = 'meta_value'; |
| 36 | break; |
| 37 | case 'stock_status': |
| 38 | $args['meta_key'] = '_stock_status'; |
| 39 | $args['orderby'] = 'meta_value'; |
| 40 | break; |
| 41 | default: |
| 42 | $args['orderby'] = $product_orderby; |
| 43 | } |
| 44 | } else { |
| 45 | $args['orderby'] = 'date'; |
| 46 | } |
| 47 | |
| 48 | $product_visibility_term_ids = wc_get_product_visibility_term_ids(); |
| 49 | |
| 50 | switch ($product_by) { |
| 51 | case 'category': |
| 52 | $args['tax_query'] = [ |
| 53 | [ |
| 54 | 'taxonomy' => 'product_cat', |
| 55 | 'field' => 'term_id', |
| 56 | 'terms' => (isset($term_list) && !empty($term_list)) ? $term_list : [] |
| 57 | ] |
| 58 | ]; |
| 59 | break; |
| 60 | case 'tag': |
| 61 | $args['tax_query'] = [ |
| 62 | [ |
| 63 | 'taxonomy' => 'product_tag', |
| 64 | 'field' => 'term_id', |
| 65 | 'terms' => (isset($tag_lists) && !empty($tag_lists)) ? $tag_lists : [] |
| 66 | ] |
| 67 | ]; |
| 68 | break; |
| 69 | case 'rating': |
| 70 | $product_visibility_terms = wc_get_product_visibility_term_ids(); |
| 71 | $product_visibility_not_in = []; |
| 72 | $rating_terms = []; |
| 73 | |
| 74 | if ('yes' === get_option('woocommerce_hide_out_of_stock_items')) { |
| 75 | $product_visibility_not_in[] = $product_visibility_terms['outofstock']; |
| 76 | } |
| 77 | |
| 78 | for ($i = 1; $i <= 5; $i++) { |
| 79 | |
| 80 | $t_dx = 'rated-' . $i; |
| 81 | |
| 82 | if (in_array($i, $rating_list, false) && isset($product_visibility_terms[$t_dx])) { |
| 83 | $rating_terms[] = $product_visibility_terms[$t_dx]; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (!empty($rating_terms)) { |
| 88 | $tax_query[] = [ |
| 89 | 'taxonomy' => 'product_visibility', |
| 90 | 'field' => 'term_taxonomy_id', |
| 91 | 'terms' => $rating_terms, |
| 92 | 'operator' => 'IN', |
| 93 | 'rating_filter' => true |
| 94 | ]; |
| 95 | } |
| 96 | |
| 97 | if (!empty($product_visibility_not_in)) { |
| 98 | $tax_query[] = [ |
| 99 | 'taxonomy' => 'product_visibility', |
| 100 | 'field' => 'term_taxonomy_id', |
| 101 | 'terms' => $product_visibility_not_in, |
| 102 | 'operator' => 'NOT IN' |
| 103 | ]; |
| 104 | } |
| 105 | |
| 106 | if (!empty($tax_query)) { |
| 107 | $args['tax_query'] = $tax_query; |
| 108 | } |
| 109 | break; |
| 110 | case 'attribute': |
| 111 | if (!empty($pa_attribute_list)) { |
| 112 | |
| 113 | $tx_qry = [ |
| 114 | 'relation' => 'OR' |
| 115 | ]; |
| 116 | |
| 117 | foreach ($pa_attribute_list as $txn) { |
| 118 | |
| 119 | $terms = get_terms($txn); |
| 120 | $terms_slug = []; |
| 121 | |
| 122 | foreach($terms as $term) { |
| 123 | $terms_slug[] = $term->slug; |
| 124 | } |
| 125 | |
| 126 | $tx_qry[] = [ |
| 127 | 'taxonomy' => $txn, |
| 128 | 'field' => 'slug', |
| 129 | 'terms' => $terms_slug, |
| 130 | 'operator' => 'IN' |
| 131 | ]; |
| 132 | } |
| 133 | |
| 134 | $args['tax_query'] = $tx_qry; |
| 135 | } |
| 136 | break; |
| 137 | case 'author': |
| 138 | $args['author__in'] = (isset($author_list) && !empty($author_list)) ? $author_list : []; |
| 139 | break; |
| 140 | case 'product': |
| 141 | $args['post__in'] = (isset($product_list) && !empty($product_list)) ? $product_list : []; |
| 142 | break; |
| 143 | case 'featured': |
| 144 | $args['tax_query'][] = [ |
| 145 | 'taxonomy' => 'product_visibility', |
| 146 | 'field' => 'term_taxonomy_id', |
| 147 | 'terms' => $product_visibility_term_ids['featured'] |
| 148 | ]; |
| 149 | break; |
| 150 | case 'sale': |
| 151 | $args['post__in'] = wc_get_product_ids_on_sale(); |
| 152 | break; |
| 153 | case 'viewed': |
| 154 | $viewed_products = !empty($_COOKIE['woocommerce_recently_viewed']) ? (array) explode('|', sanitize_text_field(wp_unslash($_COOKIE['woocommerce_recently_viewed']))) : []; |
| 155 | $viewed_products = array_reverse(array_filter(array_map('absint', $viewed_products))); |
| 156 | $query_args['post__in'] = $viewed_products; |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | $productQuery = new \WP_Query($args); |
| 161 | |
| 162 | ?> |
| 163 | |
| 164 | <div class="shopengine-product-list"> |
| 165 | |
| 166 | <?php if ($productQuery->have_posts()): ?> |
| 167 | |
| 168 | <div class="<?php echo $product_view_style === "list" ? esc_attr("product-list-view") : esc_attr("product-list-grid"); ?>"> |
| 169 | |
| 170 | <?php while ($productQuery->have_posts()): $productQuery->the_post(); |
| 171 | $product = wc_get_product(get_the_ID());?> |
| 172 | |
| 173 | <div class="shopengine-single-product-item"> |
| 174 | |
| 175 | <!-- product thumb --> |
| 176 | <div class="product-thumb"> |
| 177 | |
| 178 | <!-- product thumb --> |
| 179 | <a title="<?php esc_attr_e('View Product Full Details','shopengine')?>" href="<?php echo esc_url(get_the_permalink()); ?>"> |
| 180 | <?php shopengine_content_render(woocommerce_get_product_thumbnail($product->get_id())); ?> |
| 181 | </a> |
| 182 | |
| 183 | <!-- add to cart --> |
| 184 | <div class="overlay-add-to-cart position-<?php echo isset($product_hover_overlay_position) ? esc_attr($product_hover_overlay_position) : 'bottom'; ?>"> |
| 185 | <?php woocommerce_template_loop_add_to_cart();?> |
| 186 | </div> |
| 187 | <!-- tag and sale badge --> |
| 188 | <?php |
| 189 | $product_tags = get_the_terms(get_the_ID(), 'product_tag'); |
| 190 | $badge_position = !empty($badge_position) ? $badge_position : 'top-right'; |
| 191 | $badge_align = !empty($badge_align) ? $badge_align : 'horizontal'; |
| 192 | ?> |
| 193 | <div class="product-tag-sale-badge position-<?php echo esc_attr($badge_position); ?> align-<?php echo esc_attr($badge_align); ?>"> |
| 194 | <ul> |
| 195 | <!-- percentage --> |
| 196 | <?php |
| 197 | if($show_off === "yes"){ |
| 198 | if (!empty($product->get_regular_price()) && !empty($product->get_sale_price()) && $product->get_regular_price() !== $product->get_sale_price() ): ?> |
| 199 | <li class="badge no-link off"> |
| 200 | <?php |
| 201 | $percentage = round((($product->get_regular_price() - $product->get_sale_price()) / $product->get_regular_price()) * 100); |
| 202 | echo esc_html(sprintf('%1$s%2$s%3$s', '-', $percentage, '%')); |
| 203 | ?> |
| 204 | </li> |
| 205 | <?php endif;} ?> |
| 206 | <!-- custom tags --> |
| 207 | <?php |
| 208 | if($show_tag === "yes"){ |
| 209 | if($product_tags && !is_wp_error($product_tags)): |
| 210 | $product_tag = $product_tags[0]; |
| 211 | $bg = get_term_meta($product_tag->term_id, 'devmonsta_bajaar_tag_bg_color', true); ?> |
| 212 | <li class="badge tag"> |
| 213 | <a title="<?php echo esc_html($product_tag->name, 'shopengine');?>" href="<?php echo esc_url(get_term_link($product_tag->term_id)); ?>"> |
| 214 | <?php esc_html_e($product_tag->name, 'shopengine'); ?></a> |
| 215 | </li> |
| 216 | <?php endif;} ?> |
| 217 | <!-- on sale --> |
| 218 | <?php |
| 219 | if($show_sale === "yes"){ |
| 220 | if ($product->is_on_sale()) :?> |
| 221 | <li class='badge no-link sale'><?php esc_html_e('Sale!', 'shopengine')?></li> |
| 222 | <?php endif;} ?> |
| 223 | <!-- out of stock --> |
| 224 | <?php |
| 225 | if($show_stock_out_badge === "yes"){ |
| 226 | if(!$product->is_in_stock()):?> |
| 227 | <li class="badge no-link out-of-stock"> |
| 228 | <?php echo esc_html__('Out of Stock', 'shopengine'); ?> |
| 229 | </li> |
| 230 | <?php endif;} ?> |
| 231 | </ul> |
| 232 | </div> |
| 233 | </div> |
| 234 | |
| 235 | <!-- product category --> |
| 236 | <?php if($product_view_style === "list"): ?> |
| 237 | <div class="list-view-wrapper-contents"> |
| 238 | <?php endif; ?> |
| 239 | <?php |
| 240 | $product_cats = get_the_terms(get_the_ID(), 'product_cat'); |
| 241 | $category_limit = (isset($category_limit) && !empty($category_limit)) ? esc_attr($category_limit) : 1; |
| 242 | |
| 243 | if ($product_cats && !is_wp_error($product_cats)): |
| 244 | $slice_product_cats = array_slice($product_cats, 0, $category_limit); |
| 245 | if (count($slice_product_cats) > 0): ?> |
| 246 | <div class='product-category'> |
| 247 | <ul> |
| 248 | <?php foreach ($slice_product_cats as $key => $product_cat): ?> |
| 249 | <li> |
| 250 | <a title="<?php esc_html_e('Product Category','shopengine')?>" href="<?php echo esc_url(get_term_link($product_cat)); ?>"> |
| 251 | <?php |
| 252 | echo esc_html($product_cat->name); |
| 253 | if ($key !== (count($slice_product_cats) - 1)) { |
| 254 | echo ','; |
| 255 | } |
| 256 | ?> |
| 257 | </a> |
| 258 | </li> |
| 259 | <?php endforeach;?> |
| 260 | </ul> |
| 261 | </div> |
| 262 | <?php endif; |
| 263 | endif; |
| 264 | ?> |
| 265 | |
| 266 | <!-- product title --> |
| 267 | <h3 class='product-title'> |
| 268 | <a title="<?php esc_html_e('View Product Full Details','shopengine')?>" href="<?php the_permalink();?>"> |
| 269 | <?php |
| 270 | if (isset($title_character) && !empty($title_character)): |
| 271 | echo esc_html(substr(get_the_title(), 0, $title_character)); |
| 272 | else: |
| 273 | echo esc_html(get_the_title()); |
| 274 | endif; |
| 275 | ?> |
| 276 | </a> |
| 277 | </h3> |
| 278 | |
| 279 | <!-- product rating --> |
| 280 | <div class="product-rating"> |
| 281 | <?php |
| 282 | if ($product->get_rating_count() > 0) { |
| 283 | woocommerce_template_loop_rating(); |
| 284 | } else { |
| 285 | shopengine_content_render(sprintf('<div class="star-rating">%1$s</div>', wc_get_star_rating_html(0, 0))); |
| 286 | } |
| 287 | |
| 288 | echo wp_kses(sprintf('<span class="rating-count">(%1$s)</span>', $product->get_review_count()), Helper::get_kses_array()); |
| 289 | ?> |
| 290 | </div> |
| 291 | |
| 292 | <!-- product price --> |
| 293 | <div class="product-price"> |
| 294 | <?php woocommerce_template_single_price(); ?> |
| 295 | </div> |
| 296 | <?php if($product_view_style === "list"): ?> |
| 297 | </div> |
| 298 | <?php endif; ?> |
| 299 | </div> <!-- end item --> |
| 300 | |
| 301 | <?php endwhile;?> |
| 302 | |
| 303 | </div> |
| 304 | |
| 305 | <?php else: |
| 306 | |
| 307 | esc_html_e('No product found.', 'shopengine'); |
| 308 | |
| 309 | endif; |
| 310 | |
| 311 | wp_reset_postdata();?> |
| 312 | |
| 313 | </div> |
| 314 |