Last active
January 21, 2018 06:57
-
-
Save gregoirenoyelle/454838bb766d83a4a1414c8fa668a0df to your computer and use it in GitHub Desktop.
Boucle WooCommece sur mesure avec wp_get_products
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
<? // Template Name: Boutique avec options | |
/** | |
* Ajouter nouveau contenu | |
* @author Grégoire Noyelle | |
* @link https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query | |
* @link https://businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/ | |
*/ | |
add_action( 'genesis_entry_content', 'woomod_nouveau_contenu', 20 ); | |
function woomod_nouveau_contenu() { | |
// Variables | |
$output = ''; | |
$catObj = get_field('woocom_choisir_categorie'); | |
$cat = $catObj->slug; | |
$limit = get_field('woocom_nombre_de_produit'); | |
// aff_p($cat); exit; | |
// Options d'affichage des produit | |
$args = array( | |
'type' => 'simple', | |
'limit' => $limit, | |
'category' => array($cat) | |
); | |
$products = wc_get_products( $args ); | |
// aff_p($products); | |
$output .= '<section class="boutique-sup">'; | |
// Boucle début | |
foreach ($products as $product) : | |
// Variables utilisée plus d'une fois | |
$link = get_permalink( $product->get_id() ); | |
// Affichage | |
$output .= sprintf( '<h3><a href="%s">%s</a></h3>',$link, $product->get_name() ); | |
$output .= sprintf( '<figure><a href="%s">%s</a><figcaption>%s</figcaption></figure>',$link, wp_get_attachment_image( $product->get_image_id(), $size = 'thumbnail' ), $product->get_short_description() ); | |
$output .= sprintf( '<p><a href="%s">Voir le produit complet</a></p>', $link ); | |
endforeach; | |
// Boucle fin | |
$output .= '</section>'; | |
echo $output; | |
} | |
genesis(); |
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
<? // Template Name: Boutique avec options | |
/** | |
* Ajouter nouveau contenu | |
* @author Grégoire Noyelle | |
* @link https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query | |
* @link https://businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/ | |
*/ | |
add_action( 'genesis_entry_content', 'woomod_nouveau_contenu', 20 ); | |
function woomod_nouveau_contenu() { | |
$output = ''; | |
// Obtenir les 6 derniers produits | |
$args = array( | |
'limit' => 6, | |
); | |
$products = wc_get_products( $args ); | |
// aff_p($products); | |
$output .= '<section class="boutique-sup">'; | |
// Boucle | |
foreach ($products as $product) : | |
// variables | |
$link = get_permalink( $product->get_id() ); | |
// Affichage | |
$output .= sprintf( '<h3><a href="%s">%s</a></h3>',$link, $product->get_name() ); | |
$output .= sprintf( '<figure><a href="%s">%s</a><figcaption>%s€ (HT)</figcaption></figure>',$link, wp_get_attachment_image( $product->get_image_id(), $size = 'thumbnail' ), $product->get_price() ); | |
$output .= sprintf( '<p><a href="%s">Voir le produit complet</a></p>', $link ); | |
endforeach; | |
$output .= '</section>'; | |
echo $output; | |
} | |
genesis(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment