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)
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
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)
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
Remove Downloads link
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)