Code snippets

You are here:
Estimated reading time: 3 min

Most of these codes should be pasted into wp-content/themes/rey-child/functions.php .

Show Wishlist icon on products when in Catalogue Mode

Catalogue mode* is when Ecommerce functionality is disabled.

add_action('wp', function(){
	if( class_exists('WooCommerce') && class_exists('TInvWL_Public_AddToWishlist') && is_product() ) {
		$product = wc_get_product();
		if( ! $product->is_purchasable() && ($product->get_regular_price() || $product->get_sale_price()) ) {
			add_action( 'woocommerce_single_product_summary', function(){
				echo do_shortcode("[ti_wishlists_addtowishlist]");
			}, 20 );
		}
	}
});

Force custom height & cropping in product page images

Replace 400 height with another size. It might be needed to regenerate, so you can try accessing WooCommerce > Status > Tools > Regenerate eg: https://d.pr/i/aKwJ8K .

add_filter('woocommerce_get_image_size_single', function($size){
  $size['width']  = absint( wc_get_theme_support( 'single_image_width', get_option( 'woocommerce_single_image_width', 600 ) ) );
  $size['height'] = 400;
  $size['crop']   = 1;
  return $size;
});

Replace 180 with another size. It might be needed to regenerate, so you can try accessing WooCommerce > Status > Tools > Regenerate eg: https://d.pr/i/aKwJ8K .

add_filter('woocommerce_get_image_size_gallery_thumbnail', function ($size){
	$cropping['width'] = max( 1, get_option( 'woocommerce_thumbnail_cropping_custom_width', '4' ) );
	$cropping['height'] = max( 1, get_option( 'woocommerce_thumbnail_cropping_custom_height', '3' ) );
	$size['width']  = 180;
	$size['height'] = ($cropping['height'] / $cropping['width']) * $size['width'];
	$size['crop']   = 1;
    return $size;
}, 10);

add_filter( 'woocommerce_breadcrumb_defaults', function($args){
	if( is_product() ){
		$args['home'] = _x( 'Home', 'breadcrumb', 'rey-core' );
		return $args;
	}
},20);

Slow Customizer? Disable the preview iframe.

add_action('customize_controls_print_footer_scripts', function(){
	?>
	<script>
		document.addEventListener('DOMContentLoaded', function(){
			document.getElementById('customize-preview').remove();
		});
	</script>
	<?php
});

Remove price from loop & product page

add_action('wp', function(){
    remove_action('reycore/woocommerce/after_shop_loop_item', 'woocommerce_template_loop_price', 10);
    remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_price', 60);
    remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
});

Add WooCommerce Multilingual Currency Switcher into Default Header


// Adds the currency switcher in the header
add_action('rey/header/row', function(){

    // This shortcode adds the Currency switcher
    // but too much text is shown
    //echo do_shortcode('[currency_switcher]');

    // instead show only what matters
    do_action('wcml_currency_switcher', array(
        'format' => '%code% (%symbol%)'
    ));

}, 70);

And also some fine tuning CSS in Customizer > Additional CSS

.wcml_currency_switcher {
    margin-left: 1.5rem;
    width: auto;
}

These codes apply for the Default header, when it’s non Global Section. If you’re using a Header Global Section, simply add a Shortcode element into the Header section and paste the shortcode eg: [currency_switcher] .

Prevent opening cart panel on single product page, on mobiles

Add this code into rey-child/functions.php

add_action('wp_footer', function(){
	?>
	<script>
		jQuery(document).on('ready', function(){

			var $atcBtn = jQuery(".single_add_to_cart_button.button");

			var addPreventClass = function(){
				if( ! jQuery.reyHelpers.is_desktop ){
					$atcBtn.addClass('--prevent-open-cart');
				}
			};

			addPreventClass();

			jQuery(document).on("woocommerce_variation_has_changed", ".variations_form", function (e) {
				addPreventClass();
			});
		});
	</script>
	<?php
}, 999);

Change Add To Cart button bg-color in single product pages

.woocommerce div.product .rey-cartBtnQty {
  --accent-color: #cf2929;
}

add_action('wp_footer', function(){ ?>
	<script>
		(function($){
			$(document).on('ready', function(){
				$(document).on("check_variations", ".variations_form", function (e) {
					if( typeof $.fn.wc_variations_image_reset === 'function' ){
						$(this).wc_variations_image_reset();
					}
				});
			});
		})(jQuery);
	</script>
	<?php
});
add_filter('woocommerce_output_related_products_args', function($args){
	$args['posts_per_page'] = 3;
	$args['columns'] = 3;
	return $args;
}, 10);

Change “New” badge text

add_filter('reycore/woocommerce/loop/new_text', function(){
  return 'NEW ARRIVAL';
});

Account header panel: Disable Ajax submitting forms in registration & login forms

add_filter('reycore/header/account/ajax_forms', '__return_false');
add_filter('reycore/woocommerce/account_links/register_btn_attributes', function(){
	return sprintf('href="%s"', esc_attr( get_permalink(wc_get_page_id('myaccount')) ));
});
add_filter('reycore/woocommerce/account_links/forget_btn_attributes', function(){
	return sprintf('href="%s"', esc_attr( get_permalink(wc_get_page_id('myaccount')) ));
});

Sticky Add to cart bar: Hide on specific products/categories

add_filter('theme_mod_product_page_sticky_add_to_cart', function( $status ){
	global $post;

	if( is_product() ) {

		/**
		 * This condition checks for product page having a specific ID.
		 * Replace "446" with a specific product ID you want to disable, or add more into the array, separated by comma
		 */
		if( in_array( $post->ID, [ 446, 407 ], true ) ){
			return false;
		}

		/**
		 * This condition checks for product having specific categories.
		 * Replace "t-shirts-tops" with a specific category slug you want to disable, or add more into the array, separated by comma
		 */
		if( has_term( [ 't-shirts-tops', 'some-other-category-slug' ], 'product_cat', $post ) ){
			return false;
		}

	}

	return $status;
});

ACF fields: Place custom data over the product thumbnail in product catalog

function custom__load_acf_field(){

	// bail if ACF doesn't exist
	if( ! class_exists('ACF') ){
		return;
	}

	// bail if not a product tax, or shop page
	if( ! (is_tax(get_object_taxonomies('product')) || is_shop()) ) {
		return;
	}

	global $post;

	if( $my_field = get_field('my_custom_field', $post) ){
		echo $my_field;
	}
}

add_action('reycore/loop_inside_thumbnail/top-left', 'custom__load_acf_field');

// Other hooks inside the thumbnail, in various corners:
// add_action('reycore/loop_inside_thumbnail/top-right', 'custom__load_acf_field');
// add_action('reycore/loop_inside_thumbnail/bottom-left', 'custom__load_acf_field');
// add_action('reycore/loop_inside_thumbnail/bottom-right', 'custom__load_acf_field');
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20);
add_filter('reycore/woocommerce/allow_mobile_gallery', '__return_false');

Fix for discounted price going off, on mobiles

@media (max-width: 767px) {
	.woocommerce ul.products li.product .price {
		flex-wrap: wrap;
	}
	.woocommerce ul.products li.product .price ins {
		flex-basis: 100%;
		margin-left: 0;
		margin-top: 10px;
	}
}

Apply “zero-px” hack for outdated minifiers in caching plugins

Some caching plugin css minifiers are trimming “0px” values and remove the unit, leaving it as “0” only. This breaks CSS variables and calc() functions in CSS. To prevent this, i made a “hack” until these minifiers will get updated to the latest browser standards. To enable it, use this code below.

add_action('wp_head', function(){
	?>
	<script>
		document.documentElement.style.setProperty('--zero-px', '0px');
	</script>
	<?php
}, 0);

Disable products from being purchased, but still show price

// Disable product with ID = 10, 11 from being purchased no matter what
add_filter('woocommerce_is_purchasable', function( $is_purchasable, $product ) {

	$products_ids = [ 10, 11 ];

	if( in_array($product->get_id(), $products_ids, true) ) {
		return false;
	}

	return $is_purchasable;
}, 10, 2 );

// Disable products from category "Uncategorized" from being purchased no matter what
add_filter('woocommerce_is_purchasable', function ( $is_purchasable, $product ) {

	$products_categories = [ 'uncategorized' ];

	if( has_term( $products_categories, 'product_cat', $product->get_id() ) ) {
		return false;
	}

	return $is_purchasable;
}, 10, 2 );

Add price suffix per products belonging to certain categories

add_filter( 'woocommerce_get_price_suffix', function ( $html, $product, $price, $qty ){

	$category_slugs = [
		'down-coats-vests',
		'sweatshirts-hoodies'
	];

	if( has_term( $category_slugs, 'product_cat', $product->get_id() ) ){
		$html .= '<span>per square</span>';
	}

    return $html;
}, 99, 4 );

Show a generic global section under Place order button in Checkout page

add_action('woocommerce_review_order_after_payment', function(){
	// replace 2700 with the global section id you want
	echo do_shortcode('[rey_global_section id="2700"]');
} );
Was this article helpful?
Dislike 0
Views: 836

Join the conversation