Remove word from Woocommerce count span in cart header - php

Does somebody know, where I can find the .php file in woocommerce, where I can remove the word "item" or "items" from the last span?
I've tried it with some jQuery Code but it only works when I load the page completely. When I click add to cart or remove from cart an item, the cart only reload in woocommerce without my .js file to remove the two words.
Can anybody help me?
Thank you
$('.count').html($('.count').html().replace(' items',''));
$('.count').html($('.count').html().replace(' item',''));
<a class="cart-contents" href="http://*****.de/warenkorb/" title="View your shopping cart">
<span class="amount">0,00 €</span>
<span class="count">0 items</span><!--Here I want to remove the Word items to show just the number-->
</a>

After a few days of breaking my head about this i've found a solution (I'm so happy and angry too because when you know the answer the solution is so easy).
First you have to find the file woocommerce/templates/cart/mini-cart.php to overwrite our function.
When you've found it you have to find following line:
<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s × %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); ?></li>
After you've found it you have to insert following code under the line:
<?php
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
ob_start();
?>
<a class="cart-contents" href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" title="<?php _e( 'View your shopping cart', 'storefront' ); ?>">
<span class="count"><?php echo sprintf (_n( '%d', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?></span>
</a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
?>
Now you have to save the file and reload the page and put something in your cart (or remove) to update your cart. Know it should be done! :-)
If you want to add your price to the header too you also have to add above <span class="count"> following lines of code:
<span class="amount"><?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?></span>
If you have any questions you can always comment me…

To avoid the risk of these adjustments being overwritten if you update Storefront, you can also rewrite the function in your functions.php file like this:
if ( ! function_exists( 'storefront_cart_link' ) ) {
function storefront_cart_link() {
?>
<a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'storefront' ); ?>">
<?php /* translators: %d: number of items in cart */ ?>
<?php echo wp_kses_post( WC()->cart->get_cart_subtotal() ); ?> <span class="count"><?php echo WC()->cart->get_cart_contents_count(); ?></span>
</a>
<?php
}
}

Related

Woocommerce - header cart icon customization

I'm creating a custom Woocommrce theme for a first time. I need help with displaying cart link in header. It looks like this https://imgur.com/AjARqhX and need to be like this https://imgur.com/QvYUPgU
Here is my code
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<a class="cart-customlocation" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?>
<img class="immm" src="<?php echo home_url(); ?>/wp-content/uploads/icons/header/cart.png">
<span><?php _e('Košík',''); ?></span>
</a>
<?php
$fragments['a.cart-customlocation'] = ob_get_clean();
return $fragments;
}
Now I need to show product count added in cart in "green bubble" and remove word "item".
And also after click on arrow-down icon, it shows a mini-cart. Can anynoe help with this?
Many thanks.

Show cart and chekout in the header woocomerece

Hi all maybe someone is aware on how to add cart page and checkout page content into the header.php so it shows on every page? tried looking but haven't found a proper answer. any help is appreciated.
No need to write a single piece of code ;)
A free plugin is already available in Wordpress and it will let you add minicart as a widget to any of your sidebar (here in top bar).
Hi, the below code may help you
<?php
global $woocommerce;
$qty = $woocommerce->cart->get_cart_contents_count();
$total = $woocommerce->cart->get_cart_total();
$cart_url = $woocommerce->cart->get_cart_url();
echo $loginout = wp_loginout($_SERVER['REQUEST_URI'], false ); // show wp dynamic login / logout link
?>
<a href="<?php echo $woocommerce->cart->get_checkout_url() ?>" title="<?php _e( 'Checkout' ) ?>">
<?php _e( 'Checkout' ) ?>
</a> |
<a href="<?php echo $cart_url ?>" title="<?php _e( 'Cart' ) ?>">
<?php _e( 'Cart ( '. $qty .' ) -'. $total ) ?>
</a>

Woocommerce Storefront template-tags.php changes to storefront_cart_link() not possible

I am using WordPress 4.3.1, Woocommerce 2.4.7 and the theme storefront 1.5.1.
I want to change the "site-header-cart" in the header, that displays the current price of the cart along the amount of items in the cart, to only show the number of items:
<span class="amount">463,33 €</span>
<span class="count">7 items</span>
Should be:
<span class="count">7</span>
Whenever I make changes to template-tags.php only changes outside of the
<a class="cart-contents" ...>
...
</a>
are being displayed. Whenever I try to change something inside the href the unchanged original will show up:
if ( ! function_exists( 'storefront_cart_link' ) ) {
function storefront_cart_link() {
?>
<a class="cart-contents" href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" title="<?php _e( 'View your shopping cart', 'storefront' ); ?>">
<span class="count"><?php echo wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'storefront' ), WC()->cart->get_cart_contents_count() ) );?></span>
</a>
<?php
}
}
Whats going on, can anyone help me ?
I had the same Issue. The fact is, that you have to make a operation on the cart like add a item or empty the cart to get your changes visible. Another option is clearing the sessionStorage like Loque described.
1. Add code to your custom functions.php
if ( ! function_exists( 'storefront_cart_link' ) ) {
function storefront_cart_link() {
?>
<a class="cart-contents" href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" title="<?php _e( 'View your shopping cart', 'storefront' ); ?>">
<span class="count"><?php echo WC()->cart->get_cart_contents_count();?></span>
</a>
<?php
}
}
2. Do a operation on the cart or clear the sessionStorage in Console:
sessionStorage.removeItem('wc_fragments')
3. Refresh your Browser --> very Important
Before:
<span class="count">1 items</span>
After:
<span class="count">1</span>

Wordpress End If Statements

Apologies, PHP is not my strongest area so this might seem super easy to others.
I am trying to implement a statement to say, when there is something in my WooCommerce Cart to show the cart. If there's nothing in the cart then show nothing.
The code I have so far is:
<?php if ( sizeof( $woocommerce->cart->cart_contents ) == 0 ) {
// The cart is empty
} else {
<div class="header-cart-inner">
<a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
</div>
} ?>
The code doesn't work and keeps giving me syntax errors.
<?php if ( sizeof( $woocommerce->cart->cart_contents ) == 0 ) {
// The cart is empty
} else { ?>
<div class="header-cart-inner">
<a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
</div>
<?php } ?>
That should do!
Explanation :
All PHP code is going between <?php #php code# ?> everything between these two tags is going to be compiled by the PHP engine. You didn't close your PHP after else { because of this your next piece of script <div clas... is going to be interpreted as PHP code. Now the PHP engine is going to read this and doesn't know what to make of it since it's not PHP and will throw an error.
First-of-all you need to use "woocommerce_check_cart_items" hook to check cart item. And also you didn't close the php tag before starting to the HTML.
Here is an example :
<?php
function E_Coding_Hub_Coder() {
if ( WC()->cart->get_cart_contents_count() == 0 ) {
wc_print_notice( __( '(E-Coding Hub Message) Your Cart is Empty', 'woocommerce' ), 'notice');
} else {
?>
<div class="header-cart-inner">
<a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
</div>
<?php
}
}
// Add to cart page
add_action( 'woocommerce_check_cart_items', 'E_Coding_Hub_Coder' );
// Add to shop archives & product pages
add_action( 'woocommerce_before_main_content', 'E_Coding_Hub_Coder' );

How to add the Cart button on top of the page in Woocommerce?

I am developing a site using Wordpress+Woocommerce
I am having a problem here, how to add the cart on top of the page that will dynamically change the number of products and price, each time a user adds a product to cart?
Currently I am trying with this code, but no luck yet:
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
Try this code at top of your page header.
you have to declar this first :
<?php global $woocommerce; ?> //required !!!!
And then put this wherever you want the total number of items to appear :
<?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?>
if someone want to print the total ( $ ) like me you can put this code :
<?php echo $woocommerce->cart->get_cart_total(); ?>
and here is the cart url too :
<?php echo $woocommerce->cart->get_cart_url(); ?>
That's all .

Categories