Avec ce nouveau snippet, nous allons vous montrer comment ajouter un champ personnalisé au profil des utilisateurs sur la page détails de leur compte. Dans cet exemple, nous allons ajouter le champ anniversaire. Cependant, vous pouvez ajouter n’importe quel type de champ.
1|Information
Before using this snippet, make sure you have installed the Code Snippets plugin (free) on your WordPress site. If not, you can download it via the link below. If you have never added a specific feature to your WordPress site, we recommend you start by reading our dedicated guide (link below).
2|Prérequis
Here is the list of extensions required for this snippet to work properly. Make sure to install them before activating the snippet.
WooCommerce | Required to have a My Account page on the site |
3|Snippet
Here is the snippet to use. The comments inside the code will guide you on how this snippet works.
Le snippet doit fonctionner sur la partie frontend de votre site web. Assurez-vous de bien cocher la case “Only run on site front-end” avant d’activer le snippet.
/** For the my account details page, we need 2 functions, one to display the new field and one to save the data */
\add_action('woocommerce_edit_account_form', 'myaccountDetailForm');
\add_action('woocommerce_save_account_details', 'myaccountDetailSave');
function myaccountDetailForm()
{
$userId = \get_current_user_id();
$key = "billing_birth_date";
$label = __("Date of birth", 'woocommerce');
// If the user already filled this value, we need to read it to display it
$value = \esc_attr(\get_user_meta($userId, $key, true));
echo "<p class='woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide'>";
echo "<label for='{$key}'>{$label}</label>";
echo "<input type='date' class='woocommerce-Input woocommerce-Input--text input-text' name='{$key}' id='{$key}' value='{$value}' /></p>";
// If you want, you can also add some custom description under your custom field
echo "<p class='woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide'>If you fill your date of birth, you'll earn a gift for your birthday</p>";
echo "<div class='clear'></div>";
}
function myaccountDetailSave($userId)
{
$key = "billing_birth_date";
// This part is very important. When reading a field entered by a user, you need to sanitize it to make sure it's safe
$birthday = !empty($_POST[$key]) ? \wc_clean($_POST[$key]): '';
if( !empty($birthday) )
{
$date = \date_create($birthday);
if (empty($date)) {
\wc_add_notice(__("Invalid date format for date of birth",'woocommerce'), 'error');
$birthday = false;
}
$today = \date_create();
if ($date > $today) {
\wc_add_notice(__("You must enter your date of birth, not your next birthday", 'woocommerce'), 'error');
$birthday = false;
}
}
if( $birthday !== false ) {
\update_user_meta($userId, "billing_birth_date", $birthday);
} else {
\wc_add_notice(__("Invalid date format for date of birth", 'woocommerce'), 'error');
}
}
Our Plugins
We have created powerful and widely acclaimed plugins for WooCommerce. Boost your sales with our solutions
WooRewards
Discover the most powerful loyalty plugin for WooCommerce. Simple or tiered systems, referrals, social networks, badges and achievements, you will find all the tools to build YOUR loyalty program
Learn MoreVIP Memberships
VIP Memberships is a complete membership management tool for your WooCommerce site. Sell subscriptions to your customers and offer them benefits such as preferential prices or exclusive products.
Learn MoreVirtual Wallet
Offer your customers a virtual wallet on your website. Let them save money by purchasing your products and use this credit on future purchases. This extension also offers a complete gift card tool
Learn MoreReferral Codes
Win new customers with this complete SEO tool. Whether through influencers or simple referrers, reward them and the new customers they bring
Learn More