woocommerce logo

Five great Woocomerce code snippets

Five  of my   favourite Woocomerce  code snippets

Woo commerce is proving to be  a fantastic  tool  for my  business and projects that I’ve been working on so I thought I would share  some of my favourite woocommerce code Snippets that I have been using  these come from various sources round the internet such as
Woothemes repositorywww.wpexplorer.com and wordimpress.com these Snippets  have  helped me out not end in some of my current projects such as www.godfatherpizzawoodoven.co.uk the customer  had a lot  of custom requirments for their site  such as

Adding a fee to the cart

[php]
/**
* WooCommerce Extra Feature
* ————————–
*
* Add custom fee to cart automatically
*
*/

function woo_add_cart_fee() {
global $woocommerce;
if ( is_cart() ) {
$woocommerce->cart->add_fee( __(‘Custom’, ‘woocommerce’), 5 );
}
}
add_action( ‘woocommerce_before_cart_table’, ‘woo_add_cart_fee’ );
[/php]

The Site owner  wanted  to set a minimum order amount  for his site  so I used 

Set a minimum order amount

[php]
add_action( ‘woocommerce_checkout_process’, ‘wc_minimum_order_amount’ );
add_action( ‘woocommerce_before_cart’ , ‘wc_minimum_order_amount’ );

function wc_minimum_order_amount() {
// Set this variable to specify a minimum order value
$minimum = 12.5;

if ( WC()->cart->total < $minimum ) {

if( is_cart() ) {

wc_print_notice(
sprintf( ‘You must have an order with a minimum of %s to place your order, your current order total is %s.’ ,
woocommerce_price( $minimum ),
woocommerce_price( WC()->cart->total )
), ‘error’
);

} else {

wc_add_notice(
sprintf( ‘You must have an order with a minimum of %s to place your order, your current order total is %s.’ ,
woocommerce_price( $minimum ),
woocommerce_price( WC()->cart->total )
), ‘error’
);

}
}
<span style=”font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px;”>
[/php]

My customer wanted to automatically apply a 20% discount to his customers orders on his site so I used the bellow code to

Automatically add a coupon to your customers orders

[php]</pre>
add_action( ‘woocommerce_before_cart’, ‘apply_matched_coupons’ );

add_action( ‘woocommerce_before_checkout_form’, ‘apply_matched_coupons’ );
function apply_matched_coupons() {
global $woocommerce;

$coupon_code = ’20site'; // your coupon code here

if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;

if ( $woocommerce->cart->cart_contents_total >=12.5 ) {
$woocommerce->cart->add_discount( $coupon_code );
$woocommerce->show_messages();
}

}
<pre>[/php]

The client also wanted to have the payment type with in his admin emails when he received a order so I used

[php]</pre>
<div id=”file-functions-php-LC1″><?php</div>
<div id=”file-functions-php-LC2″>// Place the following code in your theme’s functions.php file to add the payment type to all emails</div>
<div id=”file-functions-php-LC3″>add_action( ‘woocommerce_email_after_order_table’, ‘wc_add_payment_type_to_emails’, 15, 2 );</div>
<div id=”file-functions-php-LC4″>function wc_add_payment_type_to_emails( $order, $is_admin_email ) {</div>
<div id=”file-functions-php-LC5″>echo ‘<p><strong>Payment Type:</strong> ‘ . $order->payment_method_title . ‘</p>';</div>
<div id=”file-functions-php-LC6″>}</div>
<div id=”file-functions-php-LC7″></div>
<div id=”file-functions-php-LC8″>// Place the following code in your theme’s functions.php file to add the payment type to admin emails only</div>
<div id=”file-functions-php-LC9″>add_action( ‘woocommerce_email_after_order_table’, ‘wc_add_payment_type_to_admin_emails’, 15, 2 );</div>
<div id=”file-functions-php-LC10″>function wc_add_payment_type_to_admin_emails( $order, $is_admin_email ) {</div>
<div id=”file-functions-php-LC11″>if ( $is_admin_email ) {</div>
<div id=”file-functions-php-LC12″>echo ‘<p><strong>Payment Type:</strong> ‘ . $order->payment_method_title . ‘</p>';</div>
<div id=”file-functions-php-LC13″>}</div>
<div id=”file-functions-php-LC14″>}</div>
<div id=”file-functions-php-LC15″>?>
[/php]

Finally one that I have used on the RGB  sites shop its self  this is great  for  auto completing  those pesky virtual products  this is great for if  your selling virtual products  that you don’t want to ship

 Auto Complete all WooCommerce orders.

[php]</pre>
</div>
<div>/**</div>
<div id=”crayon-535a5656e7887912165287-2″>* Auto Complete all WooCommerce orders.</div>
<div id=”crayon-535a5656e7887912165287-3″>* http://docs.woothemes.com/document/automaticaaly-complete-orders/</div>
<div id=”crayon-535a5656e7887912165287-4″>*/</div>
<div id=”crayon-535a5656e7887912165287-5″></div>
<div id=”crayon-535a5656e7887912165287-6″>add_action( ‘woocommerce_thankyou’, ‘custom_woocommerce_auto_complete_order’ );</div>
<div id=”crayon-535a5656e7887912165287-7″>function custom_woocommerce_auto_complete_order( $order_id ) {</div>
<div id=”crayon-535a5656e7887912165287-8″>    global $woocommerce;</div>
<div id=”crayon-535a5656e7887912165287-9″></div>
<div id=”crayon-535a5656e7887912165287-10″>    if ( ! $order_id ) {</div>
<div id=”crayon-535a5656e7887912165287-11″>        return;</div>
<div id=”crayon-535a5656e7887912165287-12″>    }</div>
<div id=”crayon-535a5656e7887912165287-13″>    $order = new WC_Order( $order_id );</div>
<div id=”crayon-535a5656e7887912165287-14″>    $order->update_status( ‘completed’ );</div>
<div id=”crayon-535a5656e7887912165287-15″>}</div>
<div>
<pre>[/php]

No comments yet.

Leave a Reply