Hi im using this code from woocommerce to insert a cart content on my site. Does somebody know how i can modify this code so if it shows 0 on the cart it will direct me to the shop page instead of the cart page?
<?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>
// Ensure cart contents update when products are added to the cart via AJAX (place the following in 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;
}
If you want to redirect to shop when cart is empty, you can use action hook 'template_redirect' as follows :
add_action( 'template_redirect', 'my_template_redirect' );
function my_template_redirect()
{
if ( is_page( wc_get_page_id( 'cart' ) ) && sizeof( WC()->cart->get_cart() ) == 0 )
{
wp_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit;
}
}
If you are using some other page to show cart content, then you have to modify first condition of if statement, to check if the page is the one that shows cart content.
Related
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.
I have a pretty strange problem with a custom build I am doing in Wordpress. I am using hooks to overwrite a starter theme's 'add-to-cart' button on a custom page showing products. The weird thing is that when I loop through the add-to-cart button to add quantity options on my products, the original Ajax function disappears. I then implemented another function to add it back in (and cause my custom 'view cart' button's items-in-cart number to update) but although it works in the cart, it doesn't seem to be working for my custom shop page.
I am using this snippet in my header to handle the cart contents:
<?php if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
$count = WC()->cart->cart_contents_count;
?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php
if ( $count > 0 ) {
?>
<span class="cart-contents-count"><?php echo esc_html( $count ); ?></span>
<?php
}
?></a>
And here are my two functions in my child-theme functions.php:
/**
* Ensure cart contents update when products are added to the cart via AJAX
*/
function my_header_add_to_cart_fragment( $fragments ) {
ob_start();
$count = WC()->cart->cart_contents_count;
?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php
if ( $count > 0 ) {
?>
<span class="cart-contents-count"><?php echo esc_html( $count ); ?></span>
<?php
}
?></a><?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'my_header_add_to_cart_fragment' );
/**
* Add quantity to products in Products Page
*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}
I think that my second function adding a new add-to-cart button is overwriting the initial Ajax functionality, but everything I try to do to add this functionality back in is not working. I'm not the best at JS/jQuery so it's probably that I'm not implementing my code properly.
Any help with this would be much appreciated.
Need to add another classes to the button i.e add_to_cart_button & ajax_add_to_cart.
Hope this will do for you.
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' );
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 .