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
Created January 22, 2014 07:37
Limit the width and height values customers can enter using Measurement Price calculator
<?php
// Limit the values customer can enter using measurement price calculator area ( LxW ) option
add_action( 'wp_head', 'limit_measurement_values', 99 );
function limit_measurement_values() {
if ( is_product() ) {
?>
<script>
jQuery( document ).ready(function() {
jQuery( '#length_needed' ).change( function() {
if ( jQuery( this ).val() > 100 ) jQuery( this ).val( 100 ).keyup();
@kloon
kloon / functions.php
Created December 11, 2013 09:07
WooCommerce Was, Now, Save % price
<?php
// Add Now: R12, Was: R15, Save 20% price for items on sale
add_action( 'woocommerce_sale_price_html', 'wc_custom_sale_price', 10, 2 );
function wc_custom_sale_price( $price, $product ) {
$percentage = round( ( ( $product->regular_price - $product->get_price() ) / $product->regular_price ) * 100 );
return sprintf( __( 'Now: %s, Was: %s, Save %s', 'woocommerce' ), woocommerce_price( $product->get_price() ), woocommerce_price( $product->regular_price ), $percentage . '%' );
}
?>
@kloon
kloon / functions.php
Created December 11, 2013 08:21
WooCommerce apply a fee when certain products are in the cart
<?php
// Add a once-of fee to the cart when certain items are in the cart
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
$apply_fee = false;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$product_ids = array( 1, 2, 3, 4 ); // Coma separated list of product id's that must be in cart for fee to apply
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
@kloon
kloon / functions.php
Created December 5, 2013 09:36
WooCommerce add shipping method to emails
<?php
// Place the following code in your theme's functions.php file to add the shipping method to all emails
add_action( 'woocommerce_email_after_order_table', 'wc_add_shipping_method_to_emails', 15, 2 );
function wc_add_shipping_method_to_emails( $order, $is_admin_email ) {
echo '<p><strong>Shipping Method:</strong> ' . $order->get_shipping_method() . '</p>';
}
// Place the following code in your theme's functions.php file to add the shipping methid to admin emails only
add_action( 'woocommerce_email_after_order_table', 'wc_add_shipping_method_to_admin_emails', 15, 2 );
function wc_add_shipping_method_to_admin_emails( $order, $is_admin_email ) {
@kloon
kloon / functions.php
Last active February 28, 2021 16:21
WooCommerce Sort by sales - add an option to your WooCommerce store for sorting products by sales
<?php
// Place the code below in your theme's functions.php file
add_filter( 'woocommerce_get_catalog_ordering_args', 'wc_get_catalog_ordering_args' );
function wc_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'sales' == $orderby_value ) {
$args['orderby'] = 'meta_value_num';
$args['order'] = 'DESC';
$args['meta_key'] = 'total_sales';
}
@kloon
kloon / functions.php
Last active July 4, 2017 11:27
WooCommerce add payment type to emails
<?php
// Place the following code in your theme's functions.php file to add the payment type to all emails
add_action( 'woocommerce_email_after_order_table', 'wc_add_payment_type_to_emails', 15, 2 );
function wc_add_payment_type_to_emails( $order, $is_admin_email ) {
echo '<p><strong>Payment Type:</strong> ' . $order->payment_method_title . '</p>';
}
// Place the following code in your theme's functions.php file to add the payment type to admin emails only
add_action( 'woocommerce_email_after_order_table', 'wc_add_payment_type_to_admin_emails', 15, 2 );
function wc_add_payment_type_to_admin_emails( $order, $is_admin_email ) {
@kloon
kloon / functions.php
Last active September 20, 2018 10:49
WooCommerce hide free shipping based on total cart weight
<?php
add_filter( 'woocommerce_available_shipping_methods', 'wc_hide_free_shipping_based_on_weight' , 10, 1 );
function wc_hide_free_shipping_based_on_weight( $available_methods ) {
if ( isset( $available_methods['free_shipping'] ) {
global $woocommerce;
$weight = 0;
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $item_id => $values ) {
$_product = $values['data'];
if ( $_product->exists() && $values['quantity'] > 0 ) {
@kloon
kloon / functions.php
Last active March 22, 2016 01:22
WordPress Control Auto Update Features
//Allow auto updates of development releases ie alpha, beta and RC
add_filter( 'allow_dev_auto_core_updates', 'wp_control_dev_auto_updates' );
function wp_control_dev_auto_updates( $value ) {
// return true to enable and false to disable
return true;
}
//Allow auto updates of minor releases ie 3.7.1, 3.7.2
add_filter( 'allow_minor_auto_core_updates', 'wp_control_minor_auto_updates' );
function wp_control_minor_auto_updates( $value ) {
@kloon
kloon / wp-config.php
Created October 25, 2013 06:49
WordPress 3.7 Enable/Disable Automatic Updates
// Enable Automatic Updates for all updates
define( 'WP_AUTO_UPDATE_CORE', true );
// Disable Automatic Updates for all updates
define( 'WP_AUTO_UPDATE_CORE', false );
// Only allow Automatic Updates for minor updates
define( 'WP_AUTO_UPDATE_CORE', 'minor' );
@kloon
kloon / functions.php
Created October 17, 2013 07:10
List all vendors shortcode
add_shortcode( 'list_vendors', 'wc_list_vendors_shortcode' );
function wc_list_vendors_shortcode( $atts ) {
$vendors = '';
$terms = get_terms( 'shop_vendor' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'shop_vendor' );
if ( is_wp_error( $term_link ) )
continue;
$vendors .= '<h2><a href="' . $term_link . '">' . $term->name . '</a></h2>';
}