Skip to content

Instantly share code, notes, and snippets.

@shirokoweb
Last active February 23, 2020 02:01
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 shirokoweb/b0991b0d27c9a020b318305edee3d04f to your computer and use it in GitHub Desktop.
Save shirokoweb/b0991b0d27c9a020b318305edee3d04f to your computer and use it in GitHub Desktop.
Affiche uniquement les produits en stock WooCommerce (la case masquer les produits hors stock doit être décochée)
<?php
/**
* @snippet Shortcode pour exclure les produits hors stock - WooCommerce
* @author-url https://webplus.agency
* @author Agence WebPlus
* @compatible WooCommerce 3.8
* @usage [in_stock_products]
* @wc-shortcodes https://github.com/woocommerce/woocommerce/blob/dc7aa3069466f1b7e23fd7ef468760a6a9ddd241/includes/class-wc-shortcodes.php
*/
add_shortcode( 'in_stock_products', 'awp_in_stock_products_shortcode' );
function awp_in_stock_products_shortcode() {
$args = array(
'post_type' => 'product',
'posts_per_page' => '4',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock',
)
),
'fields' => 'ids',
);
$product_ids = get_posts( $args );
$product_ids = implode( ",", $product_ids );
return do_shortcode("[products ids='$product_ids']");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment