Created
March 20, 2024 09:48
-
-
Save PechenkiUA/788a4fd8f1c2efc4eab958c535edd7bd to your computer and use it in GitHub Desktop.
Шорткод для виведення товарів з часом очікування 1 день та більше
This file contains 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 | |
/** | |
* Шорткод для виведення товарів з часом очікування 1 день та більше | |
*/ | |
function shortcode_products_one_day_left($atts) | |
{ | |
$atts = shortcode_atts(array( | |
'per_page' => 12, | |
), $atts, 'products_one_day_left'); | |
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; | |
$args = array( | |
'post_type' => 'product', | |
'posts_per_page' => $atts['per_page'], | |
'paged' => $paged, | |
'meta_key' => 'data_postavki', | |
'orderby' => 'meta_value', | |
'order' => 'ASC', | |
'meta_query' => array( | |
array( | |
'key' => 'data_postavki', | |
'value' => date('Y-m-d', strtotime('+1 day')), | |
'compare' => '>=', | |
'type' => 'DATE', | |
), | |
), | |
); | |
$products = new WP_Query($args); | |
ob_start(); | |
if ($products->have_posts()) { | |
woocommerce_product_loop_start(); | |
while ($products->have_posts()) { | |
$products->the_post(); | |
wc_get_template_part('content', 'product'); | |
} | |
woocommerce_product_loop_end(); | |
echo '<nav class="woocommerce-pagination">'; | |
echo paginate_links( apply_filters( 'woocommerce_pagination_args', array( | |
'base' => str_replace( 999999999, '%#%', get_pagenum_link( 999999999 ) ), | |
'format' => '', | |
'current' => max( 1, get_query_var('paged') ), | |
'total' => $products->max_num_pages, | |
'prev_text' => '←', | |
'next_text' => '→', | |
'type' => 'list', | |
'end_size' => 3, | |
'mid_size' => 3 | |
) ) ); | |
echo '</nav>'; | |
} else { | |
echo '<p>' . __('No products found', 'woocommerce') . '</p>'; | |
} | |
wp_reset_postdata(); | |
return ob_get_clean(); | |
} | |
add_shortcode('products_one_day_left', 'shortcode_products_one_day_left'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment