CrĂ©ez un produit appelĂ© Assurance expĂ©dition qui coĂ»te 3 $, assurez-vous qu’il s’agit d’un produit cachĂ© (visibilitĂ© du catalogue sur le cĂ´tĂ© droit)
Utilisez le code suivant dans le fichier function.php de votre thème :
add_action('woocommerce_cart_totals_after_shipping', 'wc_shipping_insurance_note_after_cart');
function wc_shipping_insurance_note_after_cart() {
global $woocommerce;
$product_id = 669;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found ):
?>
<tr class="shipping">
<th><?php _e( 'Shipping Insurance', 'woocommerce' ); ?></th>
<td><a href="<?php echo do_shortcode('[add_to_cart_url id="669"]'); ?>"><?php _e( 'Add shipping insurance (+$3)' ); ?> </a></td>
</tr>
<?php else: ?>
<tr class="shipping">
<th><?php _e( 'Shipping Insurance', 'woocommerce' ); ?></th>
<td>$3</td>
</tr>
<?php endif;
}
Base du plugin de passerelle de paiement WooCommerce
<?php
/*
Plugin Name: WooCommerce <enter name> Gateway
Plugin URI: http://woothemes.com/woocommerce
Description: Extends WooCommerce with an <enter name> gateway.
Version: 1.0
Author: WooThemes
Author URI: http://woothemes.com/
Copyright: © 2009-2011 WooThemes.
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
add_action('plugins_loaded', 'woocommerce_gateway_name_init', 0);
function woocommerce_gateway_name_init() {
if ( !class_exists( 'WC_Payment_Gateway' ) ) return;
/**
* Localisation
*/
load_plugin_textdomain('wc-gateway-name', false, dirname( plugin_basename( __FILE__ ) ) . '/languages');
/**
* Gateway class
*/
class WC_Gateway_Name extends WC_Payment_Gateway {
// Go wild in here
}
/**
* Add the Gateway to WooCommerce
**/
function woocommerce_add_gateway_name_gateway($methods) {
$methods[] = 'WC_Gateway_Name';
return $methods;
}
add_filter('woocommerce_payment_gateways', 'woocommerce_add_gateway_name_gateway' );
}
Ajouter un champ personnalisé (dans une commande) aux emails
Vous pouvez ajouter n’importe quel champ personnalisĂ© Ă vos e-mails de commande en vous connectant et en spĂ©cifiant le nom du champ personnalisĂ©. Cela peut ĂŞtre utile si, par exemple, vous souhaitez inclure des Ă©lĂ©ments tels que la « clĂ© de transaction » des commandes PayPal.
Vous pouvez également ajouter l’extrait de code à l’aide du plugin Code Snippet .
Voici un exemple utilisant un champ personnalisĂ© qui a Ă©tĂ© ajoutĂ© avec l’ extension Checkout Field Editor . Depuis la version 1.1.8, le code n’est plus requis pour les champs propres Ă l’extension, mais le code est toujours valide pour les champs personnalisĂ©s créés par d’autres moyens.
Dans cet exemple, un champ personnalisé avec le libellé hear_about_usa été ajouté :

Pour ajouter le champ personnalisĂ© nouvellement ajoutĂ© hear_about_usĂ l’e-mail de commande, le code ressemblerait Ă ceci :
/**
* Add a custom field (in an order) to the emails
*/
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['hear_about_us'] = array(
'label' => __( 'Hear About Us' ),
'value' => get_post_meta( $order->id, 'hear_about_us', true ),
);
return $fields;
}
Autoriser les shortcodes dans les extraits de produits
Ajoutez du code dans le fichier de votre thème enfant functions.phpou via un plugin permettant d’ajouter des fonctions personnalisĂ©es, comme le plugin Code snippets . Évitez d’ajouter du code personnalisĂ© directement au fichier de votre thème parent, functions.phpcar celui-ci sera entièrement effacĂ© lorsque vous mettrez Ă jour le thème.
/**
* Allow shortcodes in product excerpts
*/
if (!function_exists('woocommerce_template_single_excerpt')) {
function woocommerce_template_single_excerpt( $post ) {
global $post;
if ($post->post_excerpt) echo '<div itemprop="description">' . do_shortcode(wpautop(wptexturize($post->post_excerpt))) . '</div>';
}
}
Ajouter automatiquement le produit au panier lors de la visite
/**
* Automatically add product to cart on visit
*/
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 64; //replace with your own product id
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->get_id() == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}
Si vous souhaitez ajouter automatiquement un produit au panier en fonction du total du panier utilisez le code suivant :
/**
* Add another product depending on the cart total
*/
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 2831; //replace with your product id
$found = false;
$cart_total = 30; //replace with your cart total needed to add above item
if( $woocommerce->cart->total >= $cart_total ) {
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->get_id() == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
$woocommerce->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
$woocommerce->cart->add_to_cart( $product_id );
}
}
}
}
Appliquer un coupon pour le total minimum du panier
L’extrait de code ci-dessous vous permet de :
- Affichez un avis sur la page de panier et de paiement, rappelant aux clients qu’ils bĂ©nĂ©ficient d’une rĂ©duction s’ils dĂ©pensent plus qu’un montant minimum.
- Appliquez automatiquement une remise et affichez un avis indiquant que la remise a été appliquée lorsque le total du panier est supérieur à un montant minimum.
Exigences:
- Un coupon appelé
COUPONcréé dans WooCommerce > Coupons sans montant minimum. - Le
$minimum_amountvariable s’ajuste au montant minimum selon vos besoins. - Avis modifiĂ©s pour reflĂ©ter la remise souhaitĂ©e.
Ajoutez ce code au fichier de votre thème enfant functions.phpou via un plugin permettant d’ajouter des fonctions personnalisĂ©es, comme le plugin Code snippets . Veuillez ne pas ajouter de code personnalisĂ© directement au functions.phpfichier de votre thème parent, car celui-ci sera entièrement effacĂ© lorsque vous mettrez Ă jour le thème.
/**
* Appliquer un coupon pour le total minimum du panier
*/
add_action( 'woocommerce_before_cart' , 'add_coupon_notice' );
add_action( 'woocommerce_before_checkout_form' , 'add_coupon_notice' );
fonction add_coupon_notice() {
$cart_total = WC()->cart->get_subtotal();
$montant_minimum = 50 ;
$currency_code = get_woocommerce_currency();
wc_clear_notices();
si ( $cart_total < $minimum_amount ) {
WC()->cart->remove_coupon( 'COUPON' );
wc_print_notice( "Obtenez 50 % de réduction si vous dépensez plus de $minimum_amount $currency_code !", 'notice' );
} autre {
WC()->cart->apply_coupon( 'COUPON' );
wc_print_notice( 'Vous venez de bénéficier de 50 % de réduction sur votre commande !', 'notice' );
}
wc_clear_notices();
}
Code de suivi personnalisé pour la page de remerciement
/**
* Add custom tracking code to the thank-you page
*/
add_action( 'woocommerce_thankyou', 'my_custom_tracking' );
function my_custom_tracking( $order_id ) {
// Lets grab the order
$order = wc_get_order( $order_id );
/**
* Put your tracking code here
* You can get the order total etc e.g. $order->get_total();
*/
// This is the order total
$order->get_total();
// This is how to grab line items from the order
$line_items = $order->get_items();
// This loops over line items
foreach ( $line_items as $item ) {
// This will be a product
$product = $order->get_product_from_item( $item );
// This is the products SKU
$sku = $product->get_sku();
// This is the qty purchased
$qty = $item['qty'];
// Line item total cost including taxes and rounded
$total = $order->get_line_total( $item, true, true );
// Line item subtotal (before discounts)
$subtotal = $order->get_line_subtotal( $item, true, true );
}
}
