Help Center
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)
Disable Ajax Add to Cart for specific products
The Ajax Add to Cart on the single product page is a custom feature defined in Appearance > Customize > Single Product > Product Page Elements.
The following function will disable the Ajax ATC functionality for one product identified by its ID. All other products will work with the Ajax ATC feature.
add_filter( 'goya_ajax_atc_single_product', 'custom_disable_ajax_atc');
function custom_disable_ajax_atc($atc) {
if ( ! is_product() ) return; // exit
$product_id = get_the_id();
if( $product_id == 333 ) { // Replace 333 with your product ID.
$atc = false;
}
return $atc;
}
Code language: PHP (php)
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)
Add custom HTML under the add to cart/wishlist buttons
With this snippet you can add custom HTML below the add to cart/wishlist buttons. It’s useful to add extra product descriptions, trust badges, etc.
add_action('woocommerce_single_product_summary', 'goya_content_after_addtocart', 35 );
function goya_content_after_addtocart() { ?>
<div>my custom html</div>
<?php }
Code language: JavaScript (javascript)
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_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)
Replace WooCommerce breadcrumbs with shortcode
Some SEO plugins provide a custom breadcrumbs feature but they don’t replace the default one generated by WooCommerce.
This is an example for Yoast SEO:
add_action( 'woocommerce_before_main_content', 'custom_seo_breadcrumbs' );
function custom_seo_breadcrumbs() {
remove_action( 'goya_breadcrumbs', 'woocommerce_breadcrumb', 20);
add_action('goya_breadcrumbs', function(){
// Yoast SEO
yoast_breadcrumb('<nav class="woocommerce-breadcrumb">','</nav>');
}, 20 );
}
Code language: JavaScript (javascript)
For Rank Math replace the yoast_breadcrumb(...);
line with
echo do_shortcode( '[rank_math_breadcrumb]' );
Code language: PHP (php)
The actual shortcode or fuction to call may be different for your plugin. Check the plugin documentation.
Move size guide after variations
Move the size guide under the variation selection
add_action( 'after_setup_theme', 'custom_move_size_guide' );
function custom_move_size_guide() {
remove_action('woocommerce_single_product_summary', 'goya_sizing_guide_link', 29);
add_action('woocommerce_before_add_to_cart_button', 'goya_sizing_guide_link', 1);
}
Code language: JavaScript (javascript)
Move breadcrumbs above gallery
add_action( 'after_setup_theme', 'custom_move_breadcrumbs' );
function custom_move_breadcrumbs() {
remove_action('woocommerce_single_product_summary','goya_show_breadcrumbs', 1 );
add_action('woocommerce_before_single_product_summary','woocommerce_breadcrumb', 1 );
}
Code language: JavaScript (javascript)
Add category before single product title
add_action( 'woocommerce_single_product_summary', 'goya_custom_display_product_categories', 4 );
function goya_wc_display_product_categories() {
global $product;
echo wc_get_product_category_list( $product->get_id(), ', ', '<div class="category-label terms">' . _n( '', '', count( $product->get_category_ids() ), 'woocommerce' ) . ' ', '</div>' );
}
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_custom_woocommerce_product_loop_categories', 5 );
function goya_custom_woocommerce_product_loop_categories() {
global $product;
echo wc_get_product_category_list( $product->get_id(), ', ', '<div class="category-label terms">' . _n( '', '', count( $product->get_category_ids() ), 'woocommerce' ) . ' ', '</div>' );
}
Code language: PHP (php)
The list of categories will appear under the product title. If you want to place it over the title then also add this CSS snippet to Appearance > Customize > Additional CSS
.products .product .caption {
display: flex;
flex-direction: column;
}
.products .product .category-label,
.products .product .tagged_as {
order: -1;
}
Code language: CSS (css)
The same CSS applies to the tags in the next example.
Show product Tags in archive
This snippet will display all the tags the product belongs to.
add_action( 'woocommerce_shop_loop_item_title', 'goya_custom_woocommerce_product_loop_tags', 5 );
function goya_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
Remove links from My Account menu
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
Insert full page above footer
Sometimes it’s useful to have a section visible on every page. There’s already the section Appearance > Customize > Footer > Footer Extra but the editor is limited.
Instead, you can append a full page to your footer. That will make it easier to edit the content.
// Insert full page above footer
function goya_insert_page_above_footer() {
// The page slug is: global-above-footer
$your_query = new WP_Query( 'pagename=global-above-footer' );
while ( $your_query->have_posts() ) : $your_query->the_post();
the_content();
endwhile;
wp_reset_postdata();
}
add_action( 'goya_footer', 'goya_insert_page_above_footer', 10 );
Code language: PHP (php)
In this example the page slug is global-above-footer
, Adjust the code to match your own page.
Move Top Bar or Campaign Bar inside header to make it sticky
To make the top or the campaign bar sticky they must be placed inside the header container. Move only one element to avoid conflicts.
Top Bar:
function goya_move_topbar() {
remove_action( 'goya_before_header', 'goya_topbar' );
add_action( 'goya_header_inner', 'goya_topbar',1 );
}
add_action( 'after_setup_theme', 'goya_move_topbar' );
Code language: JavaScript (javascript)
Or Campaign Bar:
function goya_move_campaign_bar() {
remove_action( 'goya_before_site', 'goya_campaigns', 99 );
add_action( 'goya_header_inner', 'goya_campaigns', 1 );
}
add_action( 'after_setup_theme', 'goya_move_campaign_bar' );
Code language: JavaScript (javascript)
However, then you have to manually adjust the header height with CSS. This is an example:
@media screen and (min-width: 992px) {
.theme-goya .header .header-main,
.theme-goya.header_on_scroll:not(.megamenu-active) .header .header-main {
height: calc(100% - 40px);
}
.theme-goya.header_on_scroll:not(.megamenu-active) .header,
.theme-goya .header,
.theme-goya .header-spacer,
.theme-goya .product-header-spacer {
height: 120px;
}
}
Code language: CSS (css)
In its original position, the campaign bar height is fluid. But now it must fit inside the defined header height to keep the header sticky. Check on all screen sizes and adjust the height if necessary.
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 icons from other font icons libraries if library is already enqueued.
Or you can use embedded <svg> too.
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.