Last active
February 23, 2020 02:01
-
-
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)
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 | |
/** | |
* @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