ACF field value with SVG icons on single product pages

le 26/10/2021
JAG

In WPDevDesign’s Facebook group a user asks:
Hi all! Been trying to get my head around a requirement I need on a single product page. Using ACF to add custom fields to products, the custom fields are basically a yes/no i.e. Yes = “All items included”, and No = “Parts missing”.Pretty straight forward, but I’m wanting to add an icon for each answer. Yes icon would be a ‘check’ and the No answer would be the ‘times’ icon.I’ve been able to get the fields to display in the backend product pages and in the Oxygen template with the text appearing, but not the icons.I tried using the ACF ‘True/False’ field and in each ‘On text’ or ‘Off text’ field adding the text and icon, but it appears that these fields strip the html?I even tried using a PNG in case I didn’t added the icon correctly but that didn’t show either. Any ideas? Cheers!
This members-only tutorial provides the steps to add a field group using Advanced Custom Fields plugin having a Select type of custom field and outputting it with different SVG icons depending on the value selected for each singular product.
When “Pre-owned condition” custom field has been set to “All pieces included”:

When “Pre-owned condition” custom field has been set to “Pieces are missing”:

Step 1
Set up your field group.

Content of Choices:
-- Select --
All pieces included : All pieces included
Pieces are missing : Pieces are missing
You can find the sample field group from my test site here.
Step 2
Add/edit your WooCommerce products and select a value for the custom field. Save/update.
Step 3
Install and activate Code Snippets plugin.
Go to Snippets > Add New.
Title: Output a ACF select field’s value above Add to Cart on single product pages
Code:
add_action( 'woocommerce_single_product_summary', 'wpdd_template_single_pre_owned_condition', 25 );
/**
* Output ACF field value above Add to Cart button on single product pages.
*/
function wpdd_template_single_pre_owned_condition() {
$pre_owned_checklist_all_pieces = get_field( 'pre-owned_checklist_-_all_pieces' );

// if the field is empty, abort.
if ( ! $pre_owned_checklist_all_pieces ) {
return;
}

switch ( $pre_owned_checklist_all_pieces ) {
case 'All pieces included':
printf( '

%s

', $pre_owned_checklist_all_pieces );
break;

case 'Pieces are missing':
printf( '

%s

', $pre_owned_checklist_all_pieces );
break;

default:
# code...
break;
}
}
Change as needed.
Set the snippet to run everywhere. Save changes and activate.
Step 4
Create/edit a Stylesheet at Manage > Stylesheets and add this sample CSS:
.pre-owned-condition {
display: flex;
align-items: center;
}

.pre-owned-condition svg {
margin-right: 5px;
}

.pre-owned-condition.included {
color: #38A169;
}

.pre-owned-condition.missing {
color: #E53E3E;
}
Reference
/wp-content/plugins/woocommerce/templates/content-single-product.php.

Nos experts se tiennent à votre disposition
pour répondre à vos questions et vous conseiller
dans votre projet digital.

Vous souhaitez développer une application ?

Envie d’échanger avec l’un de nos experts ? Besoin d’en savoir plus ?
En savoir plus

Recevez notre newsletter

La protection des données nous tient à cœur. JAG utilise les informations que vous fournissez afin de vous proposer des informations et du contenu pertinent sur nos produits et services. Vous pouvez vous désinscrire de ce type de communications à tout moment. Pour plus d'informations, consultez notre politique de confidentialité
© 2022 JAG Consulting, sas. Tous droits réservés.
© 2022 JAG Consulting, sas. Tous droits réservés.
4.8
Lire les 51 avis
google
Brieuc Pauly
08/09/2022

Créative, dynamique et très professionnelle, c'est à mon sens les piliers qui soutiennent cette équipe talentueuse ! Une agence à taille humaine qui privilégie la qualité et le résultat. Alors si vous souhaitez le meilleur pour votre communication adressez vous les yeux fermés à ces passionnés, plus que compétents ! (Translated by Google) Creative, dynamic and very professional, these are in my opinion the pillars that support this talented team! A human-sized agency that favors quality and results. So if you want the best for your communication, turn your eyes closed to these enthusiasts, who are more than competent!

google
Amaury Tardier
08/09/2022

Merci à Julien et son équipe, plus que satisfait par le travail fourni ! Je recommande vivement ! (Translated by Google) Thanks to Julien and his team, more than satisfied with the work provided! I highly recommend !

Ravi de travailler avec l'équipe JAG pour la gestion de mon site de e-Commerce. Equipe réactive et toujours pleine de bonnes idées pour améliorer les performances du site (Translated by Google) Glad to work with the JAG team for the management of my e-Commerce site. Responsive team always full of good ideas to improve the performance of the site

Merci à Julien pour son accompagnement dans mon projet, je recommande à 200% c est le Top (Translated by Google) Thanks to Julien for his support in my project, I recommend 200% it is the top

Excellente agence. Equipe est très pro, à l'écoute et de bon conseils. (Translated by Google) Excellent agency. The team is very professional, attentive and gives good advice.

Temps de réponse hors pair quand il y a eu un pb sur mon site, je vous recommande chaleureusement Julien :-) (Translated by Google) Unparalleled response time when there was a problem on my site, I warmly recommend Julien :-)

Charger plus
listarrow-right
Julien

Julien

Votre conseiller

I will be back soon

Une question ?

Julien
Bonjour ! Je suis Julien.
Comment puis-je vous aider?
Démarrer la conversation:
chat Live Chat