Je m’occupe du site Passion Aquarelle. C’est celui de ma soeur, une artiste dans l’âme, qui expose ses aquarelles, les travaux de ses élèves (oui, elle donne aussi des cours sur la région lyonnaise).
Ce site utilise bien sûr le CMS WordPress et le plugin WP e-Commerce, le premier plugin e-commerce que j’ai testé.
Pour la page d’accueil, j’ai dû créer un shortcode pour afficher ses derniers tableaux, avec une contrainte : ne pas afficher certaines catégories de produits.
Voici comment afficher vos derniers produits WP e-Commerce
Une requête pour aller chercher les produits
[pastacode manual= »%24reqProducts%20%3D%20%24wpdb-%3Eget_results(%20%22%0D%0A%20%20%20%20%20%20%20%20SELECT%20post_title%2C%20ID%2C%20post_date%2C%20post_name%20FROM%20wp_posts%20p%0D%0A%20%20%20%20%20%20%20%20JOIN%20wp_term_relationships%20r%20ON%20r.object_id%20%3D%20p.ID%0D%0A%20%20%20%20%20%20%20%20JOIN%20wp_term_taxonomy%20t%20ON%20r.term_taxonomy_id%20%3D%20t.term_taxonomy_id%0D%0A%20%20%20%20%20%20%20%20JOIN%20wp_terms%20terms%20ON%20terms.term_id%20%3D%20t.term_id%0D%0A%20%20%20%20%20%20%20%20WHERE%20t.taxonomy%20%3D%20’wpsc_product_category’%0D%0A%20%20%20%20%20%20%20%20%24exclude_cat%0D%0A%20%20%20%20%20%20%20%20AND%20p.post_type%20%3D%20’wpsc-product’%20AND%20p.post_status%20%3D%20’publish’%0D%0A%20%20%20%20%20%20%20%20ORDER%20BY%20p.post_date%20DESC%0D%0A%20%20%20%20%20%20%20%20LIMIT%20%24limit%0D%0A%20%20%20%20%22)%3B » provider= »manual » lang= »php »/]
$exlude_cat et $limit viennent des paramètres du shortcode.
Par exemple avec [wpscthelastproducts excludecat= »25,28,27″ limit= »3″] les catégories 25, 28 et 27 seront exclues de la requête et n’affichera que les 3 derniers produits.
On fait ensuite une boucle pour afficher cela :
[pastacode manual= »foreach%20(%20%24reqProducts%20as%20%24lastproduct%20)%20%7B%0D%0A%0D%0A%20%20%20%20%20%20%20%20%20%2F*%20va%20chercher%20la%20cat%C3%A9gorie%20du%20produit%20*%2F%0D%0A%20%20%20%20%20%20%20%20%24prod_ID%20%3D%20wpsc_the_product_id()%3B%0D%0A%20%20%20%20%20%20%20%20%24category%20%3D%20get_the_product_category(%24lastproduct-%3EID)%3B%0D%0A%20%20%20%20%20%20%20%20%2F*%20Fin%20categorie%20produit%20*%2F%0D%0A%0D%0A%20%20%20%20%20%20%20%20%24output%20.%3D%20’%3Cdiv%20class%3D%22latest-products%22%3E’%3B%0D%0A%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%24output%20.%3D%20’%3Cdiv%20class%3D%22last-new-prod%22%3E%3Ca%20href%3D%22’%20.%20wpsc_product_url(%20%24lastproduct-%3EID%2C%20null%20)%20.%20’%22%20class%3D%22last-product-title%22%3E’.%24titleOfProduct.’%3C%2Fa%3E%3Cbr%20%2F%3E%3Ci%3E%3Ca%20href%3D%22%2Fgalerie%2F’.%24category%5B0%5D-%3Eslug.’%2F%22%20class%3D%22last-product-title%22%3E’.%24category%5B0%5D-%3Ename.’%3C%2Fa%3E%3C%2Fi%3E%3C%2Fdiv%3E’%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Thumbnails%2C%20if%20required%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(%20%24image%20%3D%3D%201%20)%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24output%20.%3D%20’%3Cdiv%20class%3D%22imagecol%22%3E’%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24output%20.%3D%20’%3Ca%20href%3D%22’%20.%20wpsc_product_url(%20%24lastproduct-%3EID%2C%20null%20)%20.%20’%22%3E’%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24attached_images%20%3D%20(array)get_posts(%20array(%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20’post_type’%20%20%20%3D%3E%20’attachment’%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20’numberposts’%20%3D%3E%201%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20’post_status’%20%3D%3E%20null%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20’post_parent’%20%3D%3E%20%24lastproduct-%3EID%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20’orderby’%20%20%20%20%20%3D%3E%20’menu_order’%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20’order’%20%20%20%20%20%20%20%3D%3E%20’ASC’%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%20)%3B%0D%0A%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24attached_image%20%3D%20%24attached_images%5B0%5D%3B%20%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(%20%24attached_image-%3EID%20%3E%200)%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24output%20.%3D%20’%3Cimg%20src%3D%22’%20.%20wpsc_product_image(%20%24attached_image-%3EID%2C%20%24width%2C%20%24height%20)%20.%20’%22%20title%3D%22’%20.%20%24lastproduct-%3Epost_title%20.%20’%22%20alt%3D%22’%20.%20%24lastproduct-%3Epost_title%20.%20’%22%20%2F%3E’%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20else%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24output%20.%3D’%3Cimg%20class%3D%22no-image%22%20id%3D%22product_image_’.wpsc_the_product_id().’%22%20alt%3D%22No%20Image%22%20title%3D%22′.wpsc_the_product_title().’%22%20src%3D%22′.WPSC_URL.’%2Fwpsc-theme%2Fwpsc-images%2Fnoimage.png%22%20width%3D%22’%20.%20%24width%20.%20’%22%20height%3D%22’%20.%20%24height%20.%20’%22%20%2F%3E’%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24output%20.%3D%20’%3C%2Fa%3E’%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24output%20.%3D%20’%3C%2Fdiv%3E’%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%24output%20.%3D%20%22%3C%2Fdiv%3E%22%3B%0D%0A%20%20%20%20%7D » provider= »manual » lang= »php »/]
On peut aussi traiter le titre si il est trop long :
[pastacode manual= »%20%2F*%20Traitement%20du%20titre%20*%2F%0D%0A%20%24titleProduct%20%3D%20stripslashes(%20%24lastproduct-%3Epost_title%20)%3B%0D%0A%20if(%20strlen(%20%24titleProduct%20)%20%3C%3D%2030%20)%20%7B%20%0D%0A%20%20%20%20%20%24titleOfProduct%20%3D%20%24titleProduct%3B%20%0D%0A%20%7D%20else%20%7B%0D%0A%20%20%20%20%24str%20%3D%20mb_substr(%20%24titleProduct%2C%200%2C%2030%20-%20strlen(%20’…’%20)%20%2B%201%2C%20’UTF-8′)%3B%20%20%0D%0A%20%20%20%20%24titleOfProduct%20%3D%20substr(%20%24str%2C%200%2C%20strrpos(%20%24str%2C’%20′)%20).’…’%3B%0D%0A%20%7D » provider= »manual » lang= »php »/]
On a plus qu’à mettre notre shortcode là où l’on veut :
[pastacode manual= »%3C%3Fphp%20do_shortcode(‘%5Bwpscthelastproducts%5D’)%3B%20%3F%3E » provider= »manual » lang= »php »/]
Si vous voulez le shortcode en entier le voici en téléchargement ici :
Shortcode WP e-Commerce (3,4 KiB, 526 hits)
Et voilà maintenant vous pouvez afficher vos derniers produits WP e-Commerce sur votre boutique.
Commentaires