Skip to content

Instantly share code, notes, and snippets.

View kloon's full-sized avatar

Gerhard Potgieter kloon

View GitHub Profile
@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: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: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: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 / 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:4386516
Created December 27, 2012 08:16
WooCommerce theme wrapper
add_action('woocommerce_before_main_content', 'my_theme_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'my_theme_wrapper_end', 10);
function my_theme_wrapper_start() {
echo '<div class="fixed">';
echo '<div class="content-home-right">';
}
function my_theme_wrapper_end() {
echo '</div>';
@kloon
kloon / gist:4334989
Created December 19, 2012 07:03
WooCommerce remove Page from title of archive pages
function woocommerce_content() {
if ( is_singular( 'product' ) ) {
while ( have_posts() ) : the_post();
woocommerce_get_template_part( 'content', 'single-product' );
endwhile;
@kloon
kloon / gist:4331540
Created December 18, 2012 20:14
WooCommerce Change Bacs sort code text
add_filter( 'woocommerce_bacs_fields', 'woocommerce_bacs_custom_sort_code_text', 10, 1 );
function woocommerce_bacs_custom_sort_code_text( $fields ) {
$fields['sort_code'] = 'BSB';
return $fields;
}
@kloon
kloon / gist:4326708
Created December 18, 2012 09:46
WooCommerce Remove Quantity from Product Page
add_filter( 'woocommerce_is_sold_individually', 'wc_remove_all_quantity_fields', 10, 2 );
function wc_remove_all_quantity_fields( $return, $product ) {
if ( is_product() )
return true;
else return $return;
}
@kloon
kloon / gist:4239771
Created December 8, 2012 10:48
WooCommerce Name Your Price, show minimum price on archive pages
add_filter( 'woocommerce_get_price_html', 'woocommerce_nyp_min_price', 11, 2 );
function woocommerce_nyp_min_price( $price, $product ) {
if ( !$product->nyp )
return $price;
if( is_shop() || is_product_category() || is_product_tag() ) {
$price = woocommerce_price( $product->minimum );
}
return $price;