I try to customize WooCommerce Storefront mini-cart header, so there are:
- when there are no products in cart, it gives result A text;
- when there are 1 product in cart, it gives result B text,
- when there are 2 product in cart, it gives result C text,
- and so on…
Example 1:
Example 2:
I have googled and found solution only for 0, 1 and 2 products, but this is not it. “artikkel” is “item” translated to my language.
Current “solution” removes header, when 0 items in cart; when one item, translates to 1 item text; and when 2 or more itemS, it translates to other text.
But I need also to have special text for zero, 1, 2, 3-10+ items in cart.
add_filter('gettext', 'translate_reply');
add_filter('ngettext', 'translate_reply');
function translate_reply($translated)
{
$translated = str_ireplace('artikkel', 'nope ostukorvis. Üks loeb, üheksa kuulevad.', $translated);
$translated = str_ireplace('artiklit', 'nopet osukorvis. Õige sõber kaalub enam kui kuld.', $translated);
return $translated;
}
add_action( 'wp_head', 'remove_empty_cart_from_menu' );
function remove_empty_cart_from_menu() {
if ( WC()->cart->get_cart_contents_count() == 0 ) {
remove_action( 'storefront_header', 'storefront_header_cart', 60 );
}
}
When I got to this point with testings, I 50% give up, decided to ask here, can you help me.
This code below is not working, site sort of crashes (some functions start to give error), cart is not working, and the counter goes buggy, but logic is something I need … Example:
add_filter('ngettext', 'translate_reply');
function translate_reply($translated) {
if ( WC()->cart->get_cart_contents_count() == 0 ) {
$translated = str_ireplace('artiklit', 'nopet osukorvis. Zero.', $translated);
$translated = str_ireplace('artikkel', 'nope ostukorvis. One.', $translated);
} else if ( WC()->cart->get_cart_contents_count() == 1 ) {
$translated = str_ireplace('artikkel', 'nope ostukorvis. One.', $translated);
$translated = str_ireplace('artiklit', 'nopet osukorvis. Two and more or zero.', $translated);
} else if ( WC()->cart->get_cart_contents_count() == 2 ) {
$translated = str_ireplace('artiklit', 'nopet osukorvis. Two.', $translated);
$translated = str_ireplace('artikkel', 'nope ostukorvis. One.', $translated);
} else if ( WC()->cart->get_cart_contents_count() > 2 ) {
$translated = str_ireplace('artiklit', 'nopet osukorvis. Two and more for testing.', $translated);
$translated = str_ireplace('artikkel', 'nope ostukorvis. One.', $translated);
}
return $translated;
}
Thanks in advance!
null is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.