Skip to content

Instantly share code, notes, and snippets.

View kloon's full-sized avatar

Gerhard Potgieter kloon

View GitHub Profile
@kloon
kloon / functions.php
Last active April 25, 2024 16:58
WooCommerce add "Save x%" next to sale prices
<?php
// Add save percent next to sale item prices.
add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}
?>
@kloon
kloon / gist:4541017
Last active September 13, 2024 22:41
WooCommerce Clear Cart via URL
// check for clear-cart get param to clear the cart, append ?clear-cart to any site url to trigger this
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
if ( isset( $_GET['clear-cart'] ) ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}
@kloon
kloon / gist:4545677
Last active May 18, 2020 18:15
WooCommerce limit checkout to quantities in multiples of a number
<?php
// check that cart items quantities totals are in multiples of 6
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
$multiples = 6;
$total_products = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$total_products += $values['quantity'];
}
if ( ( $total_products % $multiples ) > 0 )
@kloon
kloon / gist:4553982
Created January 17, 2013 05:46
WooCommerce add custom field to invoice
<?php
// add custom field to invoice email
add_action( 'woocommerce_email_after_order_table', 'woocommerce_custom_invoice_fields' );
function woocommerce_custom_invoice_fields( $order ) {
?>
<p><strong><?php _e('Free Book:', 'woocommerce'); ?></strong> <?php echo get_post_meta( $order->id, 'Free Book', true ); ?></p>
<p><strong><?php _e('Free DVD:', 'woocommerce'); ?></strong> <?php echo get_post_meta( $order->id, 'Free DVD', true ); ?></p>
<p><strong><?php _e('Gift:', 'woocommerce'); ?></strong> <?php echo get_post_meta( $order->id, 'Gift Order', true ); ?></p>
<p><strong><?php _e('Gift Message:', 'woocommerce'); ?></strong> <?php echo get_post_meta( $order->id, 'Special Gift Message', true ); ?></p>
<?php
@kloon
kloon / gist:4604038
Last active June 17, 2023 22:22
WooCommerce Sale Products shortcode
<?php
add_shortcode( 'sale_products', 'sale_products' );
function sale_products( $atts ){
global $woocommerce_loop, $woocommerce;
extract( shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'asc'
@kloon
kloon / gist:4604097
Created January 23, 2013 10:23
WooCommerce Custom Orderby
<?php
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
if( isset ( $_SESSION['orderby'] ) ) {
switch ( $_SESSION['orderby'] ) {
case 'sku_asc' :
$args['orderby'] = 'meta_value';
$args['order'] = 'asc';
$args['meta_key'] = '_sku';
break;
@kloon
kloon / gist:4633463
Last active December 27, 2019 15:04
WooCommerce Disable Coupons on Sale Items
<?php
// Exclude coupons from being applied when products on sale
add_filter( 'woocommerce_coupon_is_valid', 'woocommerce_coupon_check_sale_items', 10, 2 );
function woocommerce_coupon_check_sale_items( $valid, $coupon ) {
global $woocommerce;
$valid_for_cart = $valid;
if (sizeof($woocommerce->cart->get_cart())>0) : foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) :
if ( function_exists( 'get_product') )
$product = get_product( $cart_item['product_id'] );
@kloon
kloon / functions.php
Last active December 12, 2015 03:19
WooCommerce Mark Virtual Product Orders Completed
<?php
add_filter( 'woocommerce_payment_complete_order_status', 'virtual_order_payment_complete_order_status', 10, 2 );
function virtual_order_payment_complete_order_status( $order_status, $order_id ) {
$order = new WC_Order( $order_id );
if ( $order_status == 'processing' &&
( $order->status == 'on-hold' || $order->status == 'pending' || $order->status == 'failed' ) ) {
$virtual_order = true;
@kloon
kloon / gist:4752925
Created February 11, 2013 05:54
Zues WordPress URL rewrites ( Working for WP MU )
#Zeus webserver version of basic Wordpress mod_rewrite rules
map path into SCRATCH:path from %{URL}
look for file at %{SCRATCH:path}
if exists then goto END
look for dir at %{SCRATCH:path}
if exists then goto END
##### FIX FOR LOGIN/FORGOTTEN PASSWORD/ADMIN ETC #####
match URL into $ with ^/wp-.*$
if matched then goto END
set URL = /index.php
@kloon
kloon / functions.php
Created February 11, 2013 09:49
WooCommerce Canvas Move Cart to Top Nav
<?php
remove_action( 'woo_nav_inside', 'woo_add_nav_cart_link' );
add_filter( 'wp_nav_menu_items', 'woo_move_cart_to_top_nav', 10, 2 );
function woo_move_cart_to_top_nav( $items, $args ) {
global $woocommerce;
if ( $args->menu_id == 'top-nav' ) {
$items .= '<li><a class="cart-contents" href="'.esc_url( $woocommerce->cart->get_cart_url() ).'" title="'.esc_attr_e( 'View your shopping cart', 'woothemes' ).'">'.sprintf( _n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes' ), $woocommerce->cart->cart_contents_count ).' - '.$woocommerce->cart->get_cart_total().'</a></li>';
}
return $items;