Skip to content

Instantly share code, notes, and snippets.

@PechenkiUA
Created March 20, 2024 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PechenkiUA/788a4fd8f1c2efc4eab958c535edd7bd to your computer and use it in GitHub Desktop.
Save PechenkiUA/788a4fd8f1c2efc4eab958c535edd7bd to your computer and use it in GitHub Desktop.
Шорткод для виведення товарів з часом очікування 1 день та більше
<?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' => '&larr;',
'next_text' => '&rarr;',
'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