Skip to content

Instantly share code, notes, and snippets.

@ThomasLarge
Last active May 1, 2018 09:37
Show Gist options
  • Save ThomasLarge/bc174d72c00264ad2323820cfb755749 to your computer and use it in GitHub Desktop.
Save ThomasLarge/bc174d72c00264ad2323820cfb755749 to your computer and use it in GitHub Desktop.
WooCommeres Functions
// Woocommeres theme support
add_action( 'after_setup_theme', 'woocommerce_support' );
function woocommerce_support() {
add_theme_support( 'woocommerce' );
}
// Add custom placeholder to notes on checkout
add_filter('woocommerce_checkout_fields', 'custom_woocommerce_checkout_fields');
function custom_woocommerce_checkout_fields( $fields ) {
$fields['order']['order_comments']['placeholder'] = 'Your custom placeholder';
return $fields;
//Add a stylesheet after default style.css
wp_enqueue_style( 'my-css', get_template_directory_uri() . 'my-css.css', array('themename-style'));
//WooCommerce - Sort products by SKU
add_filter('woocommerce_get_catalog_ordering_args', 'custom_woocommerce_catalog_orderby');
function custom_woocommerce_catalog_orderby( $args ) {
$args['meta_key'] = '_sku';
$args['orderby'] = 'meta_value';
$args['order'] = 'asc';
return $args;
}
//WooCommerce - Remove Variation Price from Single Product Page
add_filter('woocommerce_variable_price_html','custom_from',10);
add_filter('woocommerce_grouped_price_html','custom_from',10);
add_filter('woocommerce_variable_sale_price_html','custom_from',10);
function custom_from($price){
return false;
}
// WooCommerce - add SKU in category page before price
add_action( 'woocommerce_after_shop_loop_item_title', 'prima_custom_shop_item', 1);
function prima_custom_shop_item() {
global $post, $product;
/* product sku */
echo '<p>SKU: '.$product->get_sku().'</p>';
}
//show empty product categories
add_filter( 'woocommerce_product_subcategories_hide_empty', 'show_empty_categories', 10, 1 );
function show_empty_categories ( $show_empty ) {
$show_empty = true;
// You can add other logic here too
return $show_empty;
}
//Hide subcategory count in woocommerce
add_filter( 'woocommerce_subcategory_count_html', 'custom_hide_category_count' );
function custom_hide_category_count() {
// No count
}
//Remove Related Products
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
//Remove Product Sort Dropdown on Category Page
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
//Remove Result Count on Category Page
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
//Disable Search Results Redirect to Product Page
add_filter( 'woocommerce_redirect_single_search_result', '__return_false' );
//force display empty subcategories in category pages
add_filter( 'woocommerce_product_subcategories_hide_empty', 'so_28060317', 10, 1 );
function so_28060317 ( $show_empty ) {
$show_empty = true;
return $show_empty;
}
// Change number or products per row to 3
add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')) {
function loop_columns() {
return 3; // 3 products per row
}
}
// Display 24 products per page. Goes in functions.php
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 24;' ), 20 );
//display category image on category page
add_action( 'woocommerce_archive_description', 'woocommerce_category_image', 2 );
function woocommerce_category_image() {
if ( is_product_category() ){
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
if ( $image ) {
echo '<img class="cat-thumbnail" src="' . $image . '" alt="" />';
}
}
}
//remove related products from single product page
remove_action ( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
//remove price from single product page
remove_action ( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
// hide product price on category page
function woo_product_listing_remove_price(){
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
}
add_action( 'init', 'woo_product_listing_remove_price' );
remove_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10);
add_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10);
// WooCommerce Loop Subcategory Thumbs - Add Image Wrapper
if ( ! function_exists( 'woocommerce_subcategory_thumbnail' ) ) {
function woocommerce_subcategory_thumbnail( $category ) {
$small_thumbnail_size = apply_filters( 'single_product_small_thumbnail_size', 'shop_catalog' );
$dimensions = wc_get_image_size( $small_thumbnail_size );
$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
if ( $thumbnail_id ) {
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
$image = $image[0];
} else {
$image = wc_placeholder_img_src();
}
if ( $image ) {
// Prevent esc_url from breaking spaces in urls for image embeds
// Ref: http://core.trac.wordpress.org/ticket/23605
$image = str_replace( ' ', '%20', $image );
echo '<div class="imagewrapper"><img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" /></div>';
}
}
}
remove_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10);
add_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10);
//Minimum Order Dollar Amount for a specific product (this example: Curtain Wall must be $100 or more)
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' );
function spyr_set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set minimum Curtain Wall total
$minimum_cart_total = 100;
// Total we are going to be using for the Math
// This is before taxes and shipping charges
$total = 0;
//foreach products in the cart ()
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
//curtain wall product id == 3288
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if (strcmp($cart_item['product_id'], "3288") == 0 ) {
$subtotal = $_product->get_price() * (float)$cart_item['quantity'];
$total = $total + $subtotal;
}
}
// Will display a message along the lines of
// A Minimum of 100 USD is required before checking out. (Cont. below)
// Current cart total: 6 USD
if( $total <= $minimum_cart_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Minimum subtotal of $%s %s is required for Curtain Walls before checking out.</strong>'
.'<br />Current cart\'s total: $%s %s',
$minimum_cart_total,
get_option( 'woocommerce_currency'),
$total,
get_option( 'woocommerce_currency') ),
'error' );
}
}
}
// Change sale to something else
add_filter( 'woocommerce_sale_flash', 'wc_custom_replace_sale_text' );
function wc_custom_replace_sale_text( $html ) {
return str_replace( __( 'Sale!', 'woocommerce' ), __( 'CUSTOM TEXT', 'woocommerce' ), $html );
}
// Adds text when the price is left empty
add_filter('woocommerce_empty_price_html', 'custom_call_for_price');
function custom_call_for_price() {
return 'CUSTOM TEXT OR HTML HERE';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment