Last active
December 15, 2021 15:12
-
-
Save jsongerber/0dd2e208e9b9a49136c0a8c8180b699e to your computer and use it in GitHub Desktop.
Nomades WooCommerce code snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Declare WooCommerce support | |
add_action( 'after_setup_theme', 'woocommerce_support' ); | |
function woocommerce_support() { | |
add_theme_support( 'woocommerce' ); | |
add_theme_support( 'wc-product-gallery-zoom' ); | |
add_theme_support( 'wc-product-gallery-lightbox' ); | |
add_theme_support( 'wc-product-gallery-slider' ); | |
} | |
// Changer le nom Produit par Événement | |
add_filter( 'woocommerce_register_post_type_product', 'nomades_wc_custom_label_names' ); | |
function nomades_wc_custom_label_names( $args ){ | |
$labels = array( | |
'name' => __( 'Événements', 'nomades' ), | |
'singular_name' => __( 'Événement', 'nomades' ), | |
'menu_name' => __( 'Événements', 'Admin menu name', 'nomades' ), | |
'add_new' => __( 'Ajouter un événement', 'nomades' ), | |
'add_new_item' => __( 'Ajouter un événement', 'nomades' ), | |
'edit' => __( 'Modifier un événement', 'nomades' ), | |
'edit_item' => __( 'Modifier l\'événement', 'nomades' ), | |
'new_item' => __( 'Ajouter un événement', 'nomades' ), | |
'view' => __( 'Voir un événement', 'nomades' ), | |
'view_item' => __( 'Voir l\'événement', 'nomades' ), | |
'search_items' => __( 'Rechercher des événements', 'nomades' ), | |
'not_found' => __( 'Aucun événement trouvé', 'nomades' ), | |
'not_found_in_trash' => __( 'Aucun événement trouvé dans la corbeille', 'nomades' ), | |
'parent' => __( 'Événement parent', 'nomades' ) | |
); | |
$args['labels'] = $labels; | |
$args['description'] = __( 'Vous pouvez ici ajouter des événements.', 'nomades' ); | |
return $args; | |
} | |
// Modifier les champs de la page "Validation de la commande" | |
add_filter( 'woocommerce_checkout_fields' , 'nomades_checkout_fields' ); | |
function nomades_checkout_fields( $fields ) { | |
/* | |
Structure de la variables $fields (n'hésitez pas à utiliser var_dump($fields) pour la visualiser) | |
billing | |
billing_first_name | |
billing_last_name | |
billing_company | |
billing_address_1 | |
billing_address_2 | |
billing_city | |
billing_postcode | |
billing_country | |
billing_state | |
billing_email | |
billing_phone | |
shipping | |
shipping_first_name | |
shipping_last_name | |
shipping_company | |
shipping_address_1 | |
shipping_address_2 | |
shipping_city | |
shipping_postcode | |
shipping_country | |
shipping_state | |
account | |
account_username | |
account_password | |
account_password-2 | |
order | |
order_comments | |
type – type du champ (text, textarea, password, select) | |
label – texte du label | |
placeholder – attribut placeholder du champ | |
class – classe du tag parent du champ | |
input_class – classes du champ | |
label_class – classes du label | |
required – si le champ est obligatoire | |
clear – permet de "clear" le float du champ | |
options – pour les champs select (key => value) | |
*/ | |
$fields['billing']['billing_first_name']['input_class'] = array('red', 'bold'); // Ajoute les classes red et bold | |
$fields['billing']['billing_email']['label'] = 'Email'; // Change le <label> à "Email" | |
$fields['billing']['billing_first_name']['required'] = false; // Rend le champ "Prénom" non obligatoire | |
$fields['billing']['billing_address_1']['placeholder'] = 'Rue et numéro'; // Change l'attribut "placeholder" | |
unset($fields['billing']['billing_address_2']); // Supprime le champ "Adresse 2" | |
return $fields; | |
} | |
// Redirige l'utilisateur vers la page du paiement directement après avoir ajouter un produit au panier | |
// Utile si vous ne vendez qu'un seul produit | |
add_filter( 'woocommerce_add_to_cart_redirect', 'nomades_redirect_add_cart_to_checkout' ); | |
function nomades_redirect_add_cart_to_checkout() { | |
return wc_get_checkout_url(); | |
} | |
// Enlever le fil d'ariane | |
remove_action('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0); | |
// Changer le texte "Choix des options" du bouton des produits variables | |
add_filter('woocommerce_product_add_to_cart_text', 'nomades_change_add_to_cart_text', 10); | |
function nomades_change_add_to_cart_text($text) { | |
global $product; | |
if ($product instanceof WC_Product && $product->is_type('variable')) { | |
if ($product->is_purchasable()) { | |
$text = 'Ajouter au panier'; | |
} else { | |
$text = 'Découvrir le produit'; | |
} | |
} | |
return $text; | |
} | |
// Ajoute une zone de widgets | |
// la fonction dynamic_sidebar( 'sidebar' ) permet de l'afficher, par exemple: | |
/* | |
<?php if ( is_active_sidebar( 'sidebar' ) ) : ?> | |
<div id="primary-sidebar" class="primary-sidebar widget-area" role="complementary"> | |
<?php dynamic_sidebar( 'sidebar' ); ?> | |
</div><!-- #primary-sidebar --> | |
<?php endif; ?> | |
*/ | |
add_action( 'widgets_init', 'nomades_widgets_init' ); | |
function nomades_widgets_init() { | |
register_sidebar( array( | |
'name' => 'Sidebar', | |
'id' => 'sidebar', | |
'before_widget' => '<div>', | |
'after_widget' => '</div>', | |
'before_title' => '<h2 class="widget-title">', | |
'after_title' => '</h2>', | |
) ); | |
} | |
// Si la livraison gratuite est disponible, cacher les autres méthodes de livraison | |
add_filter( 'woocommerce_package_rates', 'nomades_hide_shipping_when_free_is_available', 100 ); | |
function nomades_hide_shipping_when_free_is_available( $rates ) { | |
$free = array(); | |
foreach ( $rates as $rate_id => $rate ) { | |
if ( 'free_shipping' === $rate->method_id ) { | |
$free[ $rate_id ] = $rate; | |
break; | |
} | |
} | |
return ! empty( $free ) ? $free : $rates; | |
} | |
// Cacher l'onglet "Informations complémentaires" | |
add_filter( 'woocommerce_product_tabs', 'remove_product_tabs', 99 ); | |
function remove_product_tabs( $tabs ) { | |
unset( $tabs['additional_information'] ); | |
// unset( $tabs['reviews'] ); | |
return $tabs; | |
} | |
// Remplacer le texte "Rupture de stock" | |
add_filter('woocommerce_get_availability', 'nomades_availability_label'); | |
function nomades_availability_label($availability) { | |
$availability['availability'] = str_replace('Rupture de stock', 'Vendu', $availability['availability']); | |
return $availability; | |
} | |
// Empêcher l'achat en dessous d'un certain montant | |
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' ); | |
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' ); | |
function wc_minimum_order_amount() { | |
$minimum = 50; | |
if ( WC()->cart->total < $minimum ) { | |
wc_print_notice( | |
sprintf( 'Votre total est de %s — vous devez avoir un minimum de %s pour commander' , | |
wc_price( WC()->cart->total ), | |
wc_price( $minimum ) | |
), 'error' | |
); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment