<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WooCommerce : Astuces, bugs - Le MEMO du Web Développeur</title>
	<atom:link href="https://memo-web.fr/categorie/wordpress/woocommerce/feed/" rel="self" type="application/rss+xml" />
	<link>https://memo-web.fr</link>
	<description></description>
	<lastBuildDate>Mon, 20 Apr 2020 07:22:53 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>
	<item>
		<title>Ordonner les produits WooCommerce différemment selon la catégorie avec ACF</title>
		<link>https://memo-web.fr/ordonner-les-produits-woocommerce-differemment-selon-la-categorie-avec-acf/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=ordonner-les-produits-woocommerce-differemment-selon-la-categorie-avec-acf</link>
					<comments>https://memo-web.fr/ordonner-les-produits-woocommerce-differemment-selon-la-categorie-avec-acf/#respond</comments>
		
		<dc:creator><![CDATA[t@ra]]></dc:creator>
		<pubDate>Mon, 20 Apr 2020 07:01:05 +0000</pubDate>
				<category><![CDATA[ACF]]></category>
		<category><![CDATA[Enfold]]></category>
		<category><![CDATA[WooCommerce]]></category>
		<category><![CDATA[WordPress]]></category>
		<guid isPermaLink="false">http://memo-web.fr/?p=1155</guid>

					<description><![CDATA[<p>Si vous utilisez WooCommerce pour votre e-boutique et que vous souhaitez trier vos produits WooCommerce de façons spécifique selon la catégorie affichée, cela n&#8217;est pas possible. Heureusement cet article nous fournit une solution facile à mettre en oeuvre en utilisation le plugin ACF Advanced Custom Fields Pour les utilisateurs du thème WordPress Enfold j&#8217;ajouterai en [&#8230;]</p>
The post <a href="https://memo-web.fr/ordonner-les-produits-woocommerce-differemment-selon-la-categorie-avec-acf/">Ordonner les produits WooCommerce différemment selon la catégorie avec ACF</a> first appeared on <a href="https://memo-web.fr">Le MEMO du Web Développeur</a>.]]></description>
										<content:encoded><![CDATA[<p>Si vous utilisez WooCommerce pour votre e-boutique et que vous souhaitez trier vos produits WooCommerce de façons spécifique selon la catégorie affichée, cela n&rsquo;est pas possible.<br><br>Heureusement <a href="https://www.igoo.co.uk/2016/10/setting-a-custom-category-specific-product-sort-order-in-woocommerce-using-advanced-custom-fields/" class="aioseop-link">cet </a><a href="https://www.igoo.co.uk/2016/10/setting-a-custom-category-specific-product-sort-order-in-woocommerce-using-advanced-custom-fields/" target="_blank" aria-label="article (opens in a new tab)" rel="noreferrer noopener" class="aioseop-link">article</a><a href="https://www.igoo.co.uk/2016/10/setting-a-custom-category-specific-product-sort-order-in-woocommerce-using-advanced-custom-fields/" class="aioseop-link"> </a>nous fournit une solution facile à mettre en oeuvre en utilisation le plugin <a href="https://www.advancedcustomfields.com/" class="aioseop-link">ACF </a><a href="https://www.advancedcustomfields.com/" target="_blank" aria-label="Advanced (opens in a new tab)" rel="noreferrer noopener" class="aioseop-link">Advanced</a><a href="https://www.advancedcustomfields.com/" class="aioseop-link"> Custom Fields</a><br><br>Pour les utilisateurs du <a href="https://themeforest.net/item/enfold-responsive-multipurpose-theme/4519990" target="_blank" label="thème (opens in a new tab)" rel="noreferrer noopener" class="aioseop-link broken_link">thème</a><a href="https://themeforest.net/item/enfold-responsive-multipurpose-theme/4519990" class="aioseop-link broken_link"> WordPress Enfold</a> j&rsquo;ajouterai en fin  d&rsquo;article la prise en compte de ce tri pour un affichage avec Masonry</p>



<h2 class="wp-block-heading">Méthode pour modifier le tri des produits WooCommerce en fonction de la catégorie</h2>



<p>1 &#8211; Installez et activez le <a href="https://fr.wordpress.org/plugins/advanced-custom-fields/" target="_blank" aria-label=" (opens in a new tab)" rel="noreferrer noopener" class="aioseop-link">plugin ACF</a><br>2 &#8211; Créer un groupe de champs depuis le menu ACF avec un champs de type <strong>« Relation »</strong> que l&rsquo;on nommera <strong>product_order</strong> (nom arbitraire, utilisé dans le code ci dessous) on lui affectera un « filtre sur les types de publication »  <strong>produits</strong> et un « format dans le modèle » <strong>ID de l&rsquo;article</strong>.  Enfin  affichez ce groupe ACF lorsque le formulaire de taxonomie est égale à la catégorie (product_cat).<br>Ce champs apparaitra sur la page de modification de vos catégories WooCommerce, et permet pour chaque catégorie, de mettre un place un ordre spécifique d&rsquo;affichage des produits.</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="710" height="1030" src="http://memo-web.fr/wp-content/uploads/2020/04/ajouter-un-nouveau-groupe-de-champs-0wordpress-wordpress-710x1030.png" alt="" class="wp-image-1157" srcset="https://memo-web.fr/wp-content/uploads/2020/04/ajouter-un-nouveau-groupe-de-champs-0wordpress-wordpress-710x1030.png 710w, https://memo-web.fr/wp-content/uploads/2020/04/ajouter-un-nouveau-groupe-de-champs-0wordpress-wordpress-207x300.png 207w, https://memo-web.fr/wp-content/uploads/2020/04/ajouter-un-nouveau-groupe-de-champs-0wordpress-wordpress-768x1115.png 768w, https://memo-web.fr/wp-content/uploads/2020/04/ajouter-un-nouveau-groupe-de-champs-0wordpress-wordpress-1058x1536.png 1058w, https://memo-web.fr/wp-content/uploads/2020/04/ajouter-un-nouveau-groupe-de-champs-0wordpress-wordpress-1411x2048.png 1411w, https://memo-web.fr/wp-content/uploads/2020/04/ajouter-un-nouveau-groupe-de-champs-0wordpress-wordpress-1034x1500.png 1034w, https://memo-web.fr/wp-content/uploads/2020/04/ajouter-un-nouveau-groupe-de-champs-0wordpress-wordpress-486x705.png 486w" sizes="(max-width: 710px) 100vw, 710px" /></figure>



<p>3- Ajoutez le code suivant dans le fichier functions.php de votre thème WordPress enfant (ou dans un plugin). Ce code permet de modifier l&rsquo;ordre d&rsquo;affichage des produits sur vos pages de catégorie de produit WooCommerce.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">function my_custom_product_order($q)
{
	if(!is_admin())
	{
		// fetch current category id from active query
		$category_id = $q->get_queried_object_id();

		// get array of all product IDs in current category
		$product_ids = get_category_product_ids($category_id);

		// get preferred order from ACF field
		$product_ids_order_preferred = get_field('product_order', 'product_cat_' . $category_id);

		// if we have some product sort order set…
		if($product_ids_order_preferred)
		{
			// merge our preferred category ids array with the array of all products ids, and remove duplicates
			$product_ids = array_unique(array_merge($product_ids_order_preferred, $product_ids));
		}

		// set the 'posts__in' argument to the new array of post IDs (unfortunately wordpress doesn’t let you just pass an array of IDs straight in here)
		$q->set('post__in', $product_ids);

		// set the query orderby value to observe the posts__in field
		$q->set('orderby', 'post__in');
	}
	remove_action('woocommerce_product_query', 'custom_pre_get_posts_query');
}

add_action('woocommerce_product_query', ‘my_custom_product_order’);


// helper function to fetch all product IDs from a specific category ID
function get_category_product_ids($category_id)
{
	$args = array(
			'post_type' => 'product',
			'post_status' => 'publish',
			'fields' => 'ids',
			'posts_per_page' => -1,
			'tax_query' => array(
				array(
					'taxonomy' => 'product_cat',
					'field' => 'term_id',
					'terms' => $category_id,
					'operator' => 'IN'
				)
			)
	);

	$ids = get_posts($args);
	return $ids;
}</pre>



<h2 class="wp-block-heading">Modifier le tri des produits dans l&rsquo;élément Masonry de Enfold</h2>



<p>Dans mon cas, mon site était construit avec le thème Enfold et les produits WooCommerce d&rsquo;une catégorie étaient affichés via l&rsquo;élément Masonry. <br>Le code ci dessus ne fonctionnait pas pour prendre en compte le nouvel ordre d&rsquo;affichage des produits défini via le champs ACF.<br>Pour le prendre en compte, il faut utiliser le filtre Enfold  <strong>avia_masonry_entries_query</strong> tel qu&rsquo;indiqué sur <a href="https://kriesi.at/documentation/enfold/masonry/" class="aioseop-link">la documentation Enfold</a> et ajouter le bout de code à votre fichier functions.php</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">add_filter('avia_masonry_entries_query', 'avia_masonry_custom_query');

function avia_masonry_custom_query( $query ) {
	// On applique la modification de l'ordre unniquement si l'outil masonry est affecté aux categories de produit
	$type_cat    = $query['tax_query'][0]['taxonomy'];
	// On récupère l'identifiant de la catégorie sélectionnée
	$category_id = $query['tax_query'][0]['terms'][0];
  if ($type_cat == 'product_cat' &amp;&amp; $category_id) {
		// get array of all product IDs in current category
		$product_ids = get_category_product_ids($category_id);
		// get preferred order from ACF field
		$product_ids_order_preferred = get_field('product_order', 'product_cat_' . $category_id);
		// if we have some product sort order set…
		if($product_ids_order_preferred)
		{
			// merge our preferred category ids array with the array of all products ids, and remove duplicates
			$product_ids = array_unique(array_merge($product_ids_order_preferred, $product_ids));
			$query['post__in'] = $product_ids;
			$query['orderby'] = 'post__in';
		}
	}
  return $query;
}</pre>



<p></p>



<p></p>
<div style="margin-top: 0px; margin-bottom: 0px;" class="sharethis-inline-share-buttons" ></div>The post <a href="https://memo-web.fr/ordonner-les-produits-woocommerce-differemment-selon-la-categorie-avec-acf/">Ordonner les produits WooCommerce différemment selon la catégorie avec ACF</a> first appeared on <a href="https://memo-web.fr">Le MEMO du Web Développeur</a>.]]></content:encoded>
					
					<wfw:commentRss>https://memo-web.fr/ordonner-les-produits-woocommerce-differemment-selon-la-categorie-avec-acf/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
