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 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
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
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 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 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 January 22, 2014 13:16
WooCommerce add order notes to completed emails
<?php
// Add an order notes section to customers' completed emails
add_action( 'woocommerce_email_after_order_table', 'wc_add_order_notes_to_completed_emails', 10, 1 );
function wc_add_order_notes_to_completed_emails( $order ) {
if ( 'completed' == $order->status ) {
echo '<h2>' . __( 'Order Notes', 'woocommerce' ) . '</h2>';
$order_notes = $order->get_customer_order_notes();
foreach ( $order_notes as $order_note ) {
echo '<p>' . $order_note->comment_content . '<p>';
}
@kloon
kloon / functions.php
Created January 24, 2014 05:45
WooCommerce Remove Product Description from single product pages
<?php
// remove product description from single product pages
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_filter( 'woocommerce_product_tabs', 'wc_remove_description_tab', 11, 1 );
function wc_remove_description_tab( $tabs ) {
if ( isset( $tabs['description'] ) ) {
unset( $tabs['description'] );
}
}
?>
@kloon
kloon / functions.php
Last active December 18, 2021 18:55
WooCommerce Checkout Field Editor, add date range to datepicker field
<?php
// Add a date range to a datepicker field, replace #date with the id of the date field.
add_filter( 'wp_footer' , 'woo_add_checkout_field_date_range_limit' );
function woo_add_checkout_field_date_range_limit() {
if ( is_checkout() ) {
$js = 'jQuery( "#date" ).datepicker({ minDate: -5, maxDate: "+1M +10D" });';
// Check if WC 2.1+
if ( defined( 'WC_VERSION' ) && WC_VERSION ) {
wc_enqueue_js( $js );
} else {
@kloon
kloon / functions.php
Created January 28, 2014 10:41
WooCommerce add product to cart on visit
<?php
// Place the following code in your theme's functions.php file
add_action( 'init', 'wc_add_product_to_cart_on_visit' );
function wc_add_product_to_cart_on_visit() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 64; // The ID of the product to add to cart
$found = false;
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
@kloon
kloon / functions.php
Created January 30, 2014 08:23
WooCommerce import Points & Rewards using Customer Order CSV Importer
<?php
add_action( 'woocommerce_api_points_rewards_custom_generator', 'wc_points_rewards_generate_custom_points' );
function wc_points_rewards_generate_custom_points() {
$customers = get_users( array(
'meta_key' => 'wc_points_balance'
) );
foreach ( $customers as $customer ) {
$points = get_user_meta( $customer->ID, 'wc_points_balance', true );
if ( $points > 0 ) {
if ( WC_Points_Rewards_Manager::increase_points( $customer->ID, $points, 'IMPORT' ) ) {