Woocommerce plugin some useful codes to use.

Replaces the placeholder image

/*
* goes in theme functions.php or a custom plugin. Replace the image filename/path with your own <img src="http://coding.youmarketing.com.au/wp-includes/images/smilies/simple-smile.png" alt=":)" class="wp-smiley" style="height: 1em; max-height: 1em;">
*
**/
add_action( 'init', 'custom_fix_thumbnail' );
function custom_fix_thumbnail() {
add_filter('woocommerce_placeholder_img_src', 'custom_woocommerce_placeholder_img_src');
function custom_woocommerce_placeholder_img_src( $src ) {
$upload_dir = wp_upload_dir();
$uploads = untrailingslashit( $upload_dir['baseurl'] );
$src = $uploads . '/2012/07/thumb1.jpg';
return $src;
}
}

Changes Shipping & Handling Text to “Delivery Charges”

// change shipping and handling text to "Delivery Charges"
// Add this to your functions.php
 
add_filter('gettext', 'translate_reply');
add_filter('ngettext', 'translate_reply');
 
function translate_reply($translated) {
$translated = str_ireplace('Shipping and Handling', 'Delivery Cost', $translated);
return $translated;
}

Change Products Per Page

// Display 24 products per page. Goes in functions.php
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 24;' ), 20 );

Change Products Per Row

// 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
    }
}

Product Search

Method 1.

<?php get_product_search_form(); ?>

Method 2.

<form role="search" method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<div>
<label class="screen-reader-text" for="s"><?php _e( 'Search for:', 'woocommerce' ); ?></label>
<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" placeholder="<?php _e( 'Search for products', 'woocommerce' ); ?>" />
<input type="submit" id="searchsubmit" value="<?php echo esc_attr__( 'Search' ); ?>" />
<input type="hidden" name="post_type" value="product" />
</div>
</form>

Change Order Date for WooCommerce Admin DD-MM-YY

// Code to change date format in admin to d/m/y
 
add_filter( 'post_date_column_time', 'custom_post_date_column_time', 10, 2 );
function custom_post_date_column_time( $h_time, $post ) {
return get_the_time( __( 'd/m/Y', 'woocommerce' ), $post );
}

Hides all shipping methods if free shipping is available

// Hides all shipping methods if free shipping available - woocommerce
 
/**
* woocommerce_package_rates is a 2.1+ hook
*/
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
/**
* Hide shipping rates when free shipping is available
*
* @param array $rates Array of rates found for the package
* @param array $package The package array/object being shipped
* @return array of modified rates
*/
function hide_shipping_when_free_is_available( $rates, $package ) {
// Only modify rates if free_shipping is present
if ( isset( $rates['free_shipping'] ) ) {
// To unset a single rate/method, do the following. This example unsets flat_rate shipping
unset( $rates['australia_post:AUS_PARCEL_REGULAR'] );
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
return $rates;
}

Show Payment Method within Woocommerce Admin Email

// Display Payment Method on Admin Email
add_action( 'woocommerce_email_after_order_table', 'add_payment_method_to_admin_new_order', 15, 2 );
 
function add_payment_method_to_admin_new_order( $order, $is_admin_email ) {
if ( $is_admin_email ) {
echo '<p><strong>Payment Method:</strong> ' . $order->payment_method_title . '</p>';
}
}

Get product category list

<?php
$terms = get_terms( 'product_cat' );
 
echo '<ul class="shop-by-category-list">';
 
foreach ( $terms as $term ) {
 
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
 
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
 
// We successfully got a link. Print it out.
echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
}
?>

Changes position of the tabs on single product page to be underneath the cart

// Removes tabs from their original loaction
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
 
// Inserts tabs under the main right product content
add_action( 'woocommerce_single_product_summary', 'woocommerce_output_product_data_tabs', 60 );

Change orders of the tabs on the single product page

// WooCommerce tabs reorder on single product page
add_filter( 'woocommerce_product_tabs', 'woo_reorder_tabs', 98 );
function woo_reorder_tabs( $tabs ) {
  
    $tabs['description']['priority'] = 5; // Reviews first
    $tabs['features']['priority'] = 10; // Description second
    $tabs['tech_data']['priority'] = 15; // Additional information third
  
    return $tabs;
     
}

Removes meta data on single product page

The code goes into the function file.

// Removes meta data from single product page
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );

Removes single product summary

The code goes into the function file.

// Removes single product summary
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20);

Remove the result count on the main product page

The code goes into the function file.

// Removes archive result count
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20);

Removes catalog ordering on archive/main product page

The code goes into the function file.

// Removes archive catalog ordering
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30);

Change woocommerce excerpt length

// Changes the excerpt length
add_filter( 'excerpt_length', 'woo_custom_excerpt_length', 10 );
 
function woo_custom_excerpt_length ( $length ) {
    $length = 10;
    return $length;
} // End woo_custom_excerpt_length()

Remove “Add to Cart” from the main page

/* Removes the add to cart button from the main shop/category pages
* ----------------------------------------------------------------- */
function remove_loop_button(){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
add_action('init','remove_loop_button');

Change “Sale” text

/* Changes the sale! text for woocommerce
* ----------------------------------------------------------------- */
 
add_filter('woocommerce_sale_flash', 'my_custom_sale_flash', 10, 3);
function my_custom_sale_flash($text, $post, $_product) {
return '<p class="onsale"><span>On</span><br>Sale</p>';
}

Change breadcrumb delimiter

/* Change the breadcrumb delimeter from '/' to '>'
* ----------------------------------------------------------------- */
add_filter( 'woocommerce_breadcrumb_defaults', 'jk_change_breadcrumb_delimiter' );
function jk_change_breadcrumb_delimiter( $defaults ) {
 
    $defaults['delimiter'] = ' <span>&#47;</span> ';
    return $defaults;
}

Change breadcrumb text to “shop”

// Changes breadcrumb home text to shop
add_filter( 'woocommerce_breadcrumb_defaults', 'jk_change_breadcrumb_home_text' );
function jk_change_breadcrumb_home_text( $defaults ) {
// Change the breadcrumb home text from 'Home' to 'Appartment'
$defaults['home'] = 'Shop';
return $defaults;
}

Hide all shipping options if free shipping is available

/**
* woocommerce_package_rates is a 2.1+ hook
*/
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
/**
* Hide shipping rates when free shipping is available
*
* @param array $rates Array of rates found for the package
* @param array $package The package array/object being shipped
* @return array of modified rates
*/
function hide_shipping_when_free_is_available( $rates, $package ) {
// Only modify rates if free_shipping is present
if ( isset( $rates['free_shipping'] ) ) {
// To unset a single rate/method, do the following. This example unsets flat_rate shipping
unset( $rates['flat_rate'] );
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
return $rates;
}

Edit Checkout Fields

<?php
// WooCommerce Checkout Fields Hook
add_filter( 'woocommerce_checkout_fields' , 'custom_wc_checkout_fields' );
  
// Change order comments placeholder and label, and set billing phone number to not required.
function custom_wc_checkout_fields( $fields ) {
$fields['order']['order_comments']['placeholder'] = 'Enter your placeholder text here.';
$fields['order']['order_comments']['label'] = 'Enter your label here.';
$fields['billing']['billing_phone']['required'] = false;
return $fields;
}
?>

Contact me.

I would love to hear from you, send me a message using the form below.

    ×