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.
Related
I'm working on a Woocommerce theme and I need to grab just the number of items in my cart without any totals or other markup. I've tried using
<?php echo WC()->cart->get_cart_contents_count(); ?>
But it returns two spans span.amount and span.count and inside span.count it has the string "items" as well
Is there a way to return just the number of items in my cart, preferably as an int?
UPDATE:
So when using the following code:
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>">
<?php echo WC()->cart->get_cart_contents_count(); ?>
</a>
The extra markup above was returned but, by commenting out the anchor tags like so:
<!-- <a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"> -->
<?php echo WC()->cart->get_cart_contents_count(); ?>
<!-- </a> -->
Only an int was returned.
Why is that?
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
}
}
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>
I am using the snippet from the woothemes that can be found here.
Here's the code for front-end:
<?php global $woocommerce; ?>
<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>
and for backend (functions.php)
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;
}
I want to get the order total that can be found after adding coupon code.
<?php echo $woocommerce->cart->get_cart_total(); ?>
The code above will just display the total amount of cart when adding a product, but not the total order. How can I display the total order with discount when adding product to cart?
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 .