1. Home
  2. Help Center
  3. Customization
  4. Common PHP Snippets

Common adjustments. All custom php code should be placed in /wp-content/themes/goya-child/functions.php

Never modify the parent theme or your changes will be lost on the next theme update.

Single Product

Redirect to Checkout on add to cart

First in WooCommerce > Product Settings > Product tab uncheck these options:

  • Redirect to the cart page after successful addition
  • Enable AJAX add to cart buttons on archives

Then use this code:

add_filter( 'woocommerce_add_to_cart_redirect', 'goya_custom_redirect_checkout_add_cart' ); function goya_custom_redirect_checkout_add_cart() { return wc_get_checkout_url(); }
Code language: JavaScript (javascript)

Remove Additional Information tab

add_filter( 'woocommerce_product_tabs', 'goya_custom_remove_product_tabs', 9999 ); function goya_custom_remove_product_tabs( $tabs ) { unset( $tabs['additional_information'] ); return $tabs; }
Code language: PHP (php)

Remove Description tab if empty

add_filter( 'woocommerce_product_tabs', 'goya_custom_product_remove_empty_tabs', 20, 1 ); function goya_custom_product_remove_empty_tabs( $tabs ) { if ( ! empty( $tabs ) ) { foreach ( $tabs as $title => $tab ) { if ( $title == 'description' && empty( $tab['content'] ) ) { unset( $tabs[ $title ] ); } } } return $tabs; }
Code language: PHP (php)

Change the Description tab title

See Translate the Description tab title

Show lowest price instead of range

By default WooCommerce shows the range on products with different prices per variant. This script will display only the lowest price

// Show lowest price only function goya_wc_lowest_price_no_range( $wcv_price, $product ) { $prefix = sprintf('%s: ', __('From', 'wcvp_range')); $wcv_reg_min_price = $product->get_variation_regular_price( 'min', true ); $wcv_min_sale_price = $product->get_variation_sale_price( 'min', true ); $wcv_max_price = $product->get_variation_price( 'max', true ); $wcv_min_price = $product->get_variation_price( 'min', true ); $wcv_price = ( $wcv_min_sale_price == $wcv_reg_min_price ) ? wc_price( $wcv_reg_min_price ) : '<del>' . wc_price( $wcv_reg_min_price ) . '</del>' . '<ins>' . wc_price( $wcv_min_sale_price ) . '</ins>'; return ( $wcv_min_price == $wcv_max_price ) ? $wcv_price : sprintf('%s%s', $prefix, $wcv_price); } add_filter( 'woocommerce_variable_sale_price_html', 'goya_wc_lowest_price_no_range', 10, 2 ); add_filter( 'woocommerce_variable_price_html', 'goya_wc_lowest_price_no_range', 10, 2 );
Code language: PHP (php)

Shop/Catalog pages

Sort products in catalog by stock status

add_filter( 'woocommerce_get_catalog_ordering_args', 'goya_custom_sort_by_stock_status', 9999 ); function goya_custom_sort_by_stock_status( $args ) { $args['orderby'] = 'meta_value'; $args['order'] = 'ASC'; $args['meta_key'] = '_stock_status'; return $args; }
Code language: PHP (php)

Limit the product title on catalog pages

If your products have very long titles you can make them short for catalog pages only. The product page will show the full name.

add_filter( 'the_title', 'goya_custom_shorten_woo_product_title', 10, 2 ); function goya_custom_shorten_woo_product_title( $title, $id ) { if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' ) { return wp_trim_words( $title, 3, '...' ); // change last number to the number of words you want } else { return $title; } }
Code language: PHP (php)

Show product categories in archive

This snippet will display all the categories the product belongs to.

add_action( 'woocommerce_shop_loop_item_title', 'goya_wc_loop_product_categories', 10 ); function goya_wc_loop_product_categories() { global $post; $terms = get_the_terms( $post->ID, 'product_cat' ); if ( $terms && ! is_wp_error( $terms ) ) { echo '<div class="product-category-list">'; foreach ( $terms as $term ) { // Only text echo '<span>'. esc_attr($term->name) .' </span>'; // Show links instead //$link = get_term_link( $term->slug, $term->taxonomy ); //echo '<span><a href="'. esc_url($link) .'">'. esc_attr($term->name) .'</a> </span>'; } echo '</div>'; } }
Code language: PHP (php)

Move category description to the bottom

This snippet will move all category/taxonomies descriptions to the bottom of the page.

function goya_move_product_archive_description() { remove_action( 'woocommerce_archive_description', 'woocommerce_taxonomy_archive_description', 10 ); remove_action( 'woocommerce_archive_description', 'woocommerce_product_archive_description', 10 ); add_action( 'woocommerce_after_main_content', 'woocommerce_taxonomy_archive_description', 9 ); add_action( 'woocommerce_after_main_content', 'woocommerce_product_archive_description', 9 ); } add_action( 'after_setup_theme', 'goya_move_product_archive_description' );
Code language: JavaScript (javascript)

Shopping Cart

Auto refresh totals on quantity change

The cart page doesn’t refresh automatically on cart page. However, it’s possible to make it automatic.

add_action( 'wp_footer', 'goya_custom_cart_update_on_quantity_change' ); function goya_custom_cart_update_on_quantity_change() { if (is_cart()) : ?> <script> jQuery('div.woocommerce').on('change', '.woocommerce-cart-form .qty', function(){ jQuery("[name='update_cart']").prop("disabled", false); jQuery("[name='update_cart']").trigger("click"); }); </script> <?php endif; }
Code language: JavaScript (javascript)

Orders

Show images in order details page

add_filter( 'woocommerce_order_item_name', 'goya_custom_order_display_product_image', 20, 3 ); function goya_custom_order_display_product_image( $item_name, $item, $is_visible ) { // Targeting view order pages only if( is_wc_endpoint_url( 'view-order' ) ) { $product = $item->get_product(); $thumbnail = $product->get_image('thumbnail'); if( $product->get_image_id() > 0 ) { $item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>' . $item_name; } } return $item_name; }
Code language: PHP (php)

Account

function custom_my_account_menu_items( $items ) { unset($items['downloads']); return $items; } add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );
Code language: PHP (php)

Miscellaneous

Change all ‘Cart’ strings to ‘Bag’

To change all instances of cart to bag in Woocommerce use the following snippet

function goya_change_cart_string($translated_text, $text, $domain) { $translated_text = str_replace('cart', 'bag', $translated_text); $translated_text = str_replace('Cart', 'Bag', $translated_text); return $translated_text; } add_filter('gettext', 'goya_change_cart_string', 100, 3);
Code language: PHP (php)

This is the ‘nuclear’ method. It may not be compatible when used in combination with translation plugins.

Otherwise, you have to use the plugin to create a new translation and override the WooCommerce official translation file.

Shortcode to display current year

Useful for keeping the copyright year automatically updated. Use the shortcode in text areas in the customizer this way: [current_year]

function goya_current_year_shortcode () { $year = date_i18n ('Y'); return $year; } add_shortcode ('current_year', 'goya_current_year_shortcode');
Code language: PHP (php)

Was this article helpful?

Related Articles