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_remove_description_tab_if_empty', 20, 1 ); function goya_custom_remove_description_tab_if_empty( $tabs ) { global $product; if ($product && get_the_content() == '') { unset($tabs['description']); } return $tabs; }
Code language: PHP (php)

Remove Reviews tab if empty

add_filter( 'woocommerce_product_tabs', 'goya_custom_remove_reviews_tab_if_empty', 20, 1 ); function goya_custom_remove_reviews_tab_if_empty( $tabs ) { global $product; if ($product && $product->get_review_count() === 0) { unset($tabs['reviews']); } return $tabs; };
Code language: PHP (php)

Remove short description

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20);
Code language: JavaScript (javascript)

With the accordion layout the short description is moved to the first section of the accordion. In that case use the following code

add_filter( 'woocommerce_product_tabs', 'goya_custom_remove_short_description_accordion', 98 ); function goya_custom_remove_short_description_accordion( $tabs ) { unset( $tabs['desc_tab'] ); return $tabs; }
Code language: PHP (php)

Show lowest price instead of range

By default WooCommerce shows the range on products with different prices per variant. This script will display the lowest price on both the loop and single product page.

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

Remove product title from breadcrumbs

add_filter( 'woocommerce_get_breadcrumb', 'goya_custom_remove_title_from_breadcrumb', 10, 2 ); function goya_custom_remove_title_from_breadcrumb( $crumbs, $breadcrumb ) { if ( is_product() ) { array_pop( $crumbs ); } return $crumbs; }
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', 'custom_woocommerce_product_loop_categories', 5 ); function custom_woocommerce_product_loop_categories() { global $product; echo wc_get_product_category_list( $product->get_id(), ', ', '<span class="posted_in">' . _n( '', '', count( $product->get_category_ids() ), 'woocommerce' ) . ' ', '</span>' ); }
Code language: PHP (php)

Show product Tags in archive

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

add_action( 'woocommerce_shop_loop_item_title', 'custom_woocommerce_product_loop_tags', 5 ); function custom_woocommerce_product_loop_tags() { global $product; echo wc_get_product_tag_list( $product->get_id(), ', ', '<span class="tagged_as">' . _n( '', '', count( $product->get_tag_ids() ), 'woocommerce' ) . ' ', '</span>' ); }
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

This code is no longer needed

It has been added to theme and can be enabled from Appearance > Customize > Shop > Cart/Checkout > Auto update Cart

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)

Checkout

Terms and conditions page with shortcodes (WP Bakery)

Usually that page only has text and WooCommerce doesn’t process shortcodes out of the box.

To process shortcodes from a page built with WP Bakery use this function

remove_action( 'woocommerce_checkout_terms_and_conditions', 'wc_terms_and_conditions_page_content', 30 ); add_action( 'woocommerce_checkout_terms_and_conditions', 'wc_terms_and_conditions_page_content_custom', 30 ); function wc_terms_and_conditions_page_content_custom() { $terms_page_id = wc_terms_and_conditions_page_id(); if ( ! $terms_page_id ) { return; } $page = get_post( $terms_page_id ); if ( $page && 'publish' === $page->post_status && $page->post_content && ! has_shortcode( $page->post_content, 'woocommerce_checkout' )) { echo '<div class="woocommerce-terms-and-conditions" style="display: none; max-height: 200px; overflow: auto;">' . wp_kses_post( wc_format_content(preg_replace( "~(?:\[/?)[^/\]]+/?\]~s", '', $page->post_content ) ) ) . '</div>'; } }
Code language: PHP (php)

Account

Uncomment the line (delete // ) to remove that element from the menu. The following example will remove the Downloads link.

function custom_my_account_menu_items( $items ) { //unset( $items['dashboard'] ); // Remove Dashboard //unset( $items['payment-methods'] ); // Remove Payment Methods //unset( $items['orders'] ); // Remove Orders //unset( $items['edit-account'] ); // Remove Account Details //unset( $menu_links['edit-address'] ); // Addresses //unset( $items['customer-logout'] ); // Remove Logout link unset( $items['downloads'] ); // Disable Downloads return $items; } add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );
Code language: PHP (php)

Miscellaneous

Add custom icons to social media

This method will add the new icon to the social media section on the header and the social media widget

add_filter( 'social_icons_items', 'custom_additional_social_icons' ); function custom_additional_social_icons($output) { $new_icons = '<li><a href="#" title="Spotify"><span class="fa fa-spotify"></a></li>'; $output .= $new_icons; return $output; }
Code language: PHP (php)

That’s an example with font awesome icons. You can use any icon but the library must be already enqueued.

The Goya theme icons classes are et-icon et-spotify. Check /wp-content/themes/goya/assets/icons/theme-icons/style.css for the full list of icons included with the theme.

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.

Was this article helpful?

Related Articles