Untitled
webwizards
plain_text
a month ago
1.5 kB
12
Indexable
add_shortcode( 'b2bking_on_sale', function( $atts ) {
$atts = shortcode_atts( [
'limit' => -1,
'columns' => 3,
'orderby' => 'date',
'order' => 'DESC',
], $atts, 'b2bking_on_sale' );
// B2BKing pricing is per customer group, so cache per group.
$group_key = is_user_logged_in() ? (int) get_user_meta( get_current_user_id(), 'b2bking_customergroup', true ) : 0;
$product_ids_cache = 'b2bking_on_sale_ids_' . $group_key;
$ids = get_transient( $product_ids_cache );
if ( false === $ids ) {
$ids = [];
$product_ids = wc_get_products( [
'status' => 'publish',
'limit' => -1,
'return' => 'ids',
] );
foreach ( $product_ids as $pid ) {
$product = wc_get_product( $pid );
if ( $product && $product->is_on_sale() ) {
$ids[] = $pid;
}
}
set_transient( $product_ids_cache, $ids, HOUR_IN_SECONDS );
}
if ( empty( $ids ) ) {
return '<p>' . esc_html__( 'No products on sale right now.', 'b2bking' ) . '</p>';
}
return do_shortcode( sprintf(
'[products ids="%s" limit="%d" columns="%d" orderby="%s" order="%s"]',
esc_attr( implode( ',', array_map( 'absint', $ids ) ) ),
absint( $atts['limit'] ),
absint( $atts['columns'] ),
esc_attr( $atts['orderby'] ),
esc_attr( $atts['order'] )
) );
} );Editor is loading...
Leave a Comment