Untitled
unknown
plain_text
19 days ago
7.0 kB
3
Indexable
add_action('wp_head', function(){ ?> <style> .b2bkingonsale ul.page-numbers { display: flex !important; clear: both; padding: 55px; } .b2bkingonsale ul.page-numbers li { background: #f1f1f1 !important; padding: 15px !important; } .b2bkingonsale .wtbx-product-entry { width: 33% !important; } .b2bkingonsale .wtbx-product-inner div{ position: relative !important; } .b2bkingonsale .wtbx-product-entry .wtbx-product-inner .wtbx-product-title { bottom: 50px !important; } </style> <?php }); function b2bking_register_onsale_shortcode() { add_shortcode( 'b2bking_onsale', 'b2bking_onsale_shortcode' ); } add_action( 'init', 'b2bking_register_onsale_shortcode' ); function b2bking_onsale_shortcode( $atts ) { // Parse attributes $atts = shortcode_atts( array( 'limit' => '12', // Number of products per page 'columns' => '4', // Number of columns 'orderby' => 'date', // Order by parameter 'order' => 'DESC', // Order direction 'category' => '', // Filter by category slug 'cat_operator' => 'IN', // Category operator (IN, AND, NOT IN) 'paginate' => 'true', // Enable pagination 'cache' => 'false', // Disable cache for accurate sale status 'class' => '', // Additional CSS classes ), $atts, 'b2bking_onsale' ); // Sanitize attributes $limit = absint( $atts['limit'] ); $columns = absint( $atts['columns'] ); $orderby = sanitize_key( $atts['orderby'] ); $order = sanitize_key( $atts['order'] ); $paginate = wc_string_to_bool( $atts['paginate'] ); $cache = wc_string_to_bool( $atts['cache'] ); $class = sanitize_text_field( $atts['class'] ); // Handle pagination $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; // First get total count of all products to correctly handle pagination $count_args = array( 'status' => 'publish', 'limit' => -1, // Get all products 'return' => 'ids', ); // Add category filter if specified if ( ! empty( $atts['category'] ) ) { $count_args['category'] = array_map( 'sanitize_title', explode( ',', $atts['category'] ) ); $count_args['category_operator'] = $atts['cat_operator']; } // Get all product IDs first $all_product_ids = wc_get_products( $count_args ); // Create array to store IDs of products that are actually on sale $on_sale_product_ids = array(); // For each product, check if it's on sale using the filter foreach ( $all_product_ids as $product_id ) { $product = wc_get_product( $product_id ); if ( $product && $product->is_on_sale() ) { $on_sale_product_ids[] = $product_id; } } // Calculate total number of on-sale products and pages $filtered_count = count( $on_sale_product_ids ); $total_filtered_pages = ceil( $filtered_count / $limit ); // Apply sorting to the filtered products if ( $orderby === 'price' ) { usort( $on_sale_product_ids, function( $a, $b ) use ( $order ) { $product_a = wc_get_product( $a ); $product_b = wc_get_product( $b ); $price_a = $product_a->get_price(); $price_b = $product_b->get_price(); return ( $order === 'DESC' ) ? $price_b - $price_a : $price_a - $price_b; }); } elseif ( $orderby === 'date' ) { usort( $on_sale_product_ids, function( $a, $b ) use ( $order ) { $product_a = wc_get_product( $a ); $product_b = wc_get_product( $b ); $date_a = strtotime( $product_a->get_date_created() ); $date_b = strtotime( $product_b->get_date_created() ); return ( $order === 'DESC' ) ? $date_b - $date_a : $date_a - $date_b; }); } elseif ( $orderby === 'title' ) { usort( $on_sale_product_ids, function( $a, $b ) use ( $order ) { $product_a = wc_get_product( $a ); $product_b = wc_get_product( $b ); $title_a = $product_a->get_name(); $title_b = $product_b->get_name(); return ( $order === 'DESC' ) ? strcmp( $title_b, $title_a ) : strcmp( $title_a, $title_b ); }); } // Set up output buffer ob_start(); echo '<br><br><br><br><br>'; // If no on-sale products, show message if ( empty( $on_sale_product_ids ) ) { echo '<p class="woocommerce-info">' . esc_html__( 'No sale products found.', 'woocommerce' ) . '</p>'; return ob_get_clean(); } // Get the subset of products for current page $offset = ( $paged - 1 ) * $limit; $current_page_ids = array_slice( $on_sale_product_ids, $offset, $limit ); // If we're on a page with no products, redirect to last page with products if ( $paginate && $paged > 1 && empty( $current_page_ids ) && $filtered_count > 0 ) { wp_redirect( add_query_arg( 'paged', $total_filtered_pages, get_pagenum_link( 1 ) ) ); exit; } // Set up classes $classes = array( 'b2bkingonsale' ); if ( $paginate ) { $classes[] = 'b2bkingonsale-pagination'; } if ( ! empty( $class ) ) { $classes[] = $class; } echo '<div class="' . esc_attr( implode( ' ', $classes ) ) . '">'; // Add a products class for styling echo '<ul class="products columns-' . esc_attr( $columns ) . '">'; // Loop through the current page of products foreach ( $current_page_ids as $product_id ) { $product = wc_get_product( $product_id ); // Set up post data for template global $post; $post = get_post( $product_id ); setup_postdata( $post ); // Load the product template wc_get_template_part( 'content', 'product' ); } echo '</ul>'; // Add pagination if enabled if ( $paginate && $total_filtered_pages > 1 ) { echo '<nav class="woocommerce-pagination">'; echo paginate_links( array( 'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ), 'format' => '', 'current' => max( 1, $paged ), 'total' => $total_filtered_pages, 'prev_text' => '←', 'next_text' => '→', 'type' => 'list', 'end_size' => 3, 'mid_size' => 3, ) ); echo '</nav>'; } echo '</div>'; // Reset post data wp_reset_postdata(); // Return the buffered output return ob_get_clean(); } function b2bking_refresh_onsale_cache() { // Delete any transients or cached data related to on-sale products delete_transient( 'wc_products_onsale' ); } // Refresh the cache when products are saved or when sales start/end add_action( 'woocommerce_product_object_save_data', 'b2bking_refresh_onsale_cache' );
Editor is loading...
Leave a Comment