Skip to content

Instantly share code, notes, and snippets.

@gregoirenoyelle
Last active January 21, 2018 06:57
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 gregoirenoyelle/454838bb766d83a4a1414c8fa668a0df to your computer and use it in GitHub Desktop.
Save gregoirenoyelle/454838bb766d83a4a1414c8fa668a0df to your computer and use it in GitHub Desktop.
Boucle WooCommece sur mesure avec wp_get_products
<? // 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();
<? // 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