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)

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

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)

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

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