WooFrance Dépannage et support WordPress WooCommerce › Forums › Les plugins WordPress WooCommerce › Extension pour boutique de vêtement.
- Ce sujet contient 1 réponse, 2 participants et a été mis à jour pour la dernière fois par
El Condorito, le il y a 4 mois et 3 semaines.
-
AuteurMessages
-
5 décembre 2024 à 17 h 25 min #526929
Salut,
Tu n’as pas besoin d’un plugin pour cela, ou plutôt tu peux créer ton propre plugin :
/**
* @snippet Add input field to products – WooCommerce
* @author Condorito
* @compatible WooCommerce 8
* @community https://condorito.fr/
*/
// —————————————–
// 1. Show custom input field above Add to Cart
add_action( ‘woocommerce_before_add_to_cart_button’, ‘condorito_product_add_on’, 9 );
function condorito_product_add_on() {
$value = isset( $_POST[‘custom_text_add_on’] ) ? sanitize_text_field( $_POST[‘custom_text_add_on’] ) : »;
echo ‘
<label>Custom Text Add-On <abbr class= »required » title= »required »>*</label><input name= »custom_text_add_on » value= »‘ . $value . ‘ »>
‘;
}
// —————————————–
// 2. Throw error if custom input field empty
add_filter( ‘woocommerce_add_to_cart_validation’, ‘condorito_product_add_on_validation’, 10, 3 );
function condorito_product_add_on_validation( $passed, $product_id, $qty ) {
if ( isset( $_POST[‘custom_text_add_on’] ) && sanitize_text_field( $_POST[‘custom_text_add_on’] ) == » ) {
wc_add_notice( ‘Custom Text Add-On is a required field’, ‘error’ );
$passed = false;
}
return $passed;
}
// —————————————–
// 3. Save custom input field value into cart item data
add_filter( ‘woocommerce_add_cart_item_data’, ‘condorito_product_add_on_cart_item_data’, 10, 2 );
function condorito_product_add_on_cart_item_data( $cart_item, $product_id ){
if ( isset( $_POST[‘custom_text_add_on’] ) ) {
$cart_item[‘custom_text_add_on’] = sanitize_text_field( $_POST[‘custom_text_add_on’] );
}
return $cart_item;
}
// —————————————–
// 4. Display custom input field value @ Cart
add_filter( ‘woocommerce_get_item_data’, ‘condorito_product_add_on_display_cart’, 10, 2 );
function condorito_product_add_on_display_cart( $data, $cart_item ) {
if ( isset( $cart_item[‘custom_text_add_on’] ) ) {
$data[] = array(
‘name’ => ‘Custom Text Add-On’,
‘value’ => sanitize_text_field( $cart_item[‘custom_text_add_on’] )
);
}
return $data;
}
// —————————————–
// 5. Save custom input field value into order item meta
add_action( ‘woocommerce_checkout_create_order_line_item’, ‘condorito_product_add_on_order_item_meta’, 10, 4 );
function condorito_product_add_on_order_item_meta( $item, $cart_item_key, $values, $order ) {
if ( ! empty( $values[‘custom_text_add_on’] ) ) {
$item->add_meta_data( ‘Custom Text Add-On’, $values[‘custom_text_add_on’] );
}
}
En espérant que cela puisse t’aider.
====================
-
AuteurMessages
- Vous devez être connecté pour répondre à ce sujet.