Skip to content

Instantly share code, notes, and snippets.

View kloon's full-sized avatar

Gerhard Potgieter kloon

View GitHub Profile
@kloon
kloon / gist:2376300
Last active March 5, 2020 08:28
WooCommerce Automatically add product to cart on site visit
/*
* goes in theme functions.php or a custom plugin
**/
// add item to cart on visit
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 64;
$found = false;
//check if product already in cart
@kloon
kloon / gist:3952058
Created October 25, 2012 11:18
WooCommerce Product Count Shortcode
function product_count_shortcode( ) {
$count_posts = wp_count_posts( 'product' );
return $count_posts->publish;
}
add_shortcode( 'product_count', 'product_count_shortcode' );
@kloon
kloon / gist:3972435
Created October 29, 2012 08:47
WooCommerce disable payment gateways based on customer country
// Disable gateway based on country
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['ccavenue'] ) && $woocommerce->customer->get_country() <> 'IN' ) {
unset( $available_gateways['ccavenue'] );
} else if ( isset( $available_gateways['paypal'] ) && $woocommerce->customer->get_country() == 'IN' ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
@kloon
kloon / gist:3993329
Created November 1, 2012 12:22
WooCommerce Change Free price text
// Change free price text
function custom_free_price( $price, $product ) {
return '';
}
add_filter( 'woocommerce_free_sale_price_html', 'custom_free_price', 10, 2 );
add_filter( 'woocommerce_free_price_html', 'custom_free_price', 10, 2 );
add_filter( 'woocommerce_variable_free_price_html', 'custom_free_price', 10, 2 );
add_filter( 'woocommerce_variable_free_sale_price_html', 'custom_free_price', 10, 2 );
@kloon
kloon / gist:4015657
Created November 5, 2012 06:24
WooCommerce add login/logout buttons to wordpress menu
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
if (is_user_logged_in() && $args->theme_location == 'primary') {
$items .= '<li><a href="'. wp_logout_url( get_permalink( woocommerce_get_page_id( 'myaccount' ) ) ) .'">Log Out</a></li>';
}
elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
$items .= '<li><a href="' . get_permalink( woocommerce_get_page_id( 'myaccount' ) ) . '">Log In</a></li>';
}
return $items;
}
@kloon
kloon / gist:4015723
Last active October 12, 2015 10:48
WooCommerce Change empty price text
// Change empty price
function custom_empty_price( $price, $product ) {
return __( 'Call or Email for price', 'WooCommerce' ) ;
}
add_filter( 'woocommerce_variable_empty_price_html', 'custom_empty_price', 10, 2 );
add_filter( 'woocommerce_empty_price_html', 'custom_empty_price', 10, 2 );
@kloon
kloon / gist:4015808
Created November 5, 2012 07:32
WooCommerce Products Attributes shortcode
// Product Attributes shortcode
function custom_product_attr_shortcode( $atts ) {
if ( empty( $atts ) ) return;
if ( ! isset( $atts['id'] ) ) return;
$product = new WC_Product( $atts['id'] );
$product->list_attributes();
}
add_shortcode('product_attributes', 'custom_product_attr_shortcode');
@kloon
kloon / gist:4030229
Created November 7, 2012 08:39
WooCommerce Currency Convert Widget add currency code as class to <li>
jQuery( '.currency_switcher li a' ).each( function() {
jQuery( this ).parent().addClass( jQuery( this ).attr( 'data-currencycode' ) );
});
@kloon
kloon / gist:4037251
Created November 8, 2012 06:38
WooCommerce Currency Converter in non widget area
//Display currency converter widget in non widget area
function custom_ccw_display_meta_end() {
$instance = array();
$instance['title'] = 'Currency Converter';
$instance['show_reset'] = 'yes'; // leave empty to disable
$instance['message'] = 'See the prices in your currency';
$instance['currency_codes'] = "USD"."\n"."ZAR"."\n"."EUR"; // Must use \n between currencies and between ""
$args = array();
the_widget( 'WooCommerce_Widget_Currency_Converter', $instance, $args);
}
@kloon
kloon / gist:4040746
Created November 8, 2012 18:54
SSL Logo Checkout Page
// SSL Logo
function custom_ssl_logo_head() {
$str = <<<EOD
<script language="javascript" type="text/javascript">
//<![CDATA[
var tl_loc0=(window.location.protocol == "https:")? "https://secure.comodo.net/trustlogo/javascript/trustlogo.js" : "http://www.trustlogo.com/trustlogo/javascript/trustlogo.js";
document.writeln('<scr' + 'ipt language="JavaScript" src="'+tl_loc0+'" type="text\/javascript">' + '<\/scr' + 'ipt>');
//]]>
</script>
EOD;