Empty Cart button in Mini Cart. woocommerce - php

Is it possible to add an empty cart button inside a mini cart? I managed to do that on a cart page but having issue with a mini cart.
add_action( 'woocommerce_widget_shopping_cart_buttons', 'add_clear_cart_button', 20 );
function add_clear_cart_button() {
echo "<a class='button' href='?empty-cart=true'>" . __( 'Empty Cart', 'woocommerce' ) . "</a>";
}
Am I using a wrong action?

add below function in function.php for empty cart button
<?php
// check for empty-cart get param to clear the cart
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
if ( isset( $_GET['empty-cart'] ) ) {
$woocommerce->cart->empty_cart();
}
}
add_action( 'woocommerce_widget_shopping_cart_buttons', 'add_clear_cart_button', 10, 2 );
function add_clear_cart_button() {
?>
<a class="button" href="<?php echo $woocommerce->cart->get_cart_url(); ?>?empty-cart"><?php _e( 'Empty Cart', 'woocommerce' ); ?></a>
<?php
}
?>

Related

Refresh a custom component on cart events in Woocommerce

I have a component called cart checkout that is displayed on a the woocommerce archive-product.php page. When someone clicks add to card on one of the products on this page I want this component to update to show the amount of items, the name and the price. I am currently using the following code to display this information, but you have to refresh the page to see it updated after hitting add to cart.
<div class="cart-wrap">
<p class="cart-title">Currently in Cart</p>
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = wc_get_product( $values['data']->get_id());
echo "<p>" . $_product->get_title().'<span class="amount"> x '.$values['quantity'];
$price = get_post_meta($values['product_id'] , '_price', true);
echo " $".$price."</span></p>";
}
?>
<a class="cart-checkout" href="<?php echo wc_get_cart_url(); ?>" >Checkout</a>
</div>
I want to use ajax I would assume but don't even know where to start to figure out how to get it to work.
Ok thanks ahead of time.
To ajaxify cart components (or called in Woocommerce fragments), you will use the following:
1) your revisited code component:
<div class="cart-wrap">
<p class="cart-title"><?php _e("Currently in Cart", "woocommerce"); ?></p>
<div id="cart-items-wrap">
<?php
foreach( WC()->cart->get_cart() as $cart_item ) {
printf( '<p>%s<span class="amount"> x %s %s</span></p>',
$cart_item['data']->get_title(),
$cart_item['quantity'],
wc_price( wc_get_price_to_display( $cart_item['data'] ) )
);
}
?>
</div>
<a class="cart-checkout" href="<?php echo wc_get_cart_url(); ?>"><?php _e("Checkout", "woocommerce"); ?></a>
</div>
2) The hooked function that will ajax refresh your component:
add_filter( 'woocommerce_add_to_cart_fragments', 'ajaxify_components', 10, 1 );
function ajaxify_components( $fragments ) {
ob_start();
?>
<div id="cart-items-wrap">
<?php
foreach( WC()->cart->get_cart() as $cart_item ) {
printf( '<p>%s<span class="amount"> x %s %s</span></p>',
$cart_item['data']->get_title(),
$cart_item['quantity'],
wc_price( wc_get_price_to_display( $cart_item['data'] ) )
);
}
?>
</div>
<?php
$fragments['#cart-items-wrap'] = ob_get_clean();
return $fragments;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Change payment methods location before order details table in Woocommerce

I need to alter the default Woocommerce checkout a little bit. I need to move the payment options ABOVE the order review table, while keeping the "Place Order" button at the bottom below the order review table. I currently have
remove_action('woocommerce_checkout_order_review','woocommerce_checkout_payment', 20 );
add_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 5 );
This moves the payment box above the table, but it also moves the button. How can I keep the button at the bottom?
Here below, you will find the necessary code to reorder checkout order review section. This code will put the payment methods and gateways before the checkout review order table and will keep the "Place Order" button at the end.
The code:
add_action( 'woocommerce_checkout_order_review', 'reordering_checkout_order_review', 1 );
function reordering_checkout_order_review(){
remove_action('woocommerce_checkout_order_review','woocommerce_checkout_payment', 20 );
add_action( 'woocommerce_checkout_order_review', 'custom_checkout_payment', 8 );
add_action( 'woocommerce_checkout_order_review', 'custom_checkout_place_order', 20 );
}
function custom_checkout_payment() {
$checkout = WC()->checkout();
if ( WC()->cart->needs_payment() ) {
$available_gateways = WC()->payment_gateways()->get_available_payment_gateways();
WC()->payment_gateways()->set_current_gateway( $available_gateways );
} else {
$available_gateways = array();
}
if ( ! is_ajax() ) {
// do_action( 'woocommerce_review_order_before_payment' );
}
?>
<div id="payment" class="woocommerce-checkout-payment-gateways">
<?php if ( WC()->cart->needs_payment() ) : ?>
<ul class="wc_payment_methods payment_methods methods">
<?php
if ( ! empty( $available_gateways ) ) {
foreach ( $available_gateways as $gateway ) {
wc_get_template( 'checkout/payment-method.php', array( 'gateway' => $gateway ) );
}
} else {
echo '<li class="woocommerce-notice woocommerce-notice--info woocommerce-info">';
echo apply_filters( 'woocommerce_no_available_payment_methods_message', WC()->customer->get_billing_country() ? esc_html__( 'Sorry, it seems that there are no available payment methods for your state. Please contact us if you require assistance or wish to make alternate arrangements.', 'woocommerce' ) : esc_html__( 'Please fill in your details above to see available payment methods.', 'woocommerce' ) ) . '</li>'; // #codingStandardsIgnoreLine
}
?>
</ul>
<?php endif; ?>
</div>
<?php
}
function custom_checkout_place_order() {
$checkout = WC()->checkout();
$order_button_text = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
?>
<div id="payment-place-order" class="woocommerce-checkout-place-order">
<div class="form-row place-order">
<noscript>
<?php esc_html_e( 'Since your browser does not support JavaScript, or it is disabled, please ensure you click the <em>Update Totals</em> button before placing your order. You may be charged more than the amount stated above if you fail to do so.', 'woocommerce' ); ?>
<br/><button type="submit" class="button alt" name="woocommerce_checkout_update_totals" value="<?php esc_attr_e( 'Update totals', 'woocommerce' ); ?>"><?php esc_html_e( 'Update totals', 'woocommerce' ); ?></button>
</noscript>
<?php wc_get_template( 'checkout/terms.php' ); ?>
<?php do_action( 'woocommerce_review_order_before_submit' ); ?>
<?php echo apply_filters( 'woocommerce_order_button_html', '<button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '">' . esc_html( $order_button_text ) . '</button>' ); // #codingStandardsIgnoreLine ?>
<?php do_action( 'woocommerce_review_order_after_submit' ); ?>
<?php wp_nonce_field( 'woocommerce-process_checkout', 'woocommerce-process-checkout-nonce' ); ?>
</div>
</div>
<?php
if ( ! is_ajax() ) {
do_action( 'woocommerce_review_order_after_payment' );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.

Adding Extra Add to cart button below product summary in Woocommerce

In WooCommerce, I am trying to add an extra add to cart button below product summary. I successfully added an extra button following this code which works for single products:
add_action( 'woocommerce_single_product_summary', 'custom_button_after_product_summary', 30 );
function custom_button_after_product_summary() {
global $product;
echo "<a href='".$product->add_to_cart_url()."'>add to cart</a>";
}
But if the product is a variation it doesn't work.
please suggest as what to do?
I have revisited your code a bit, and added a 2nd hooked function for variable products:
// For Simple products
add_action( 'woocommerce_single_product_summary', 'second_button_after_product_summary', 30 );
function second_button_after_product_summary() {
global $product;
if( ! $product->is_type( 'variable' ) )
echo '<button type="submit" name="add-to-cart" value="'. esc_attr( $product->get_id() ).'" class="single_add_to_cart_button button alt">'. esc_html( $product->single_add_to_cart_text() ).'</button>';
}
// For Variable products
add_action( 'woocommerce_single_variation', 'second_button_single_variation', 30 );
function second_button_single_variation() {
global $product;
echo '<br>
<button type="submit" class="single_add_to_cart_button button alt">'. esc_html( $product->single_add_to_cart_text() ).'</button>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
You will get this on variable products:
The answer #LoicTheAztec provided is correct but its missing the action attribute for the button click event. Here's the corrected code with the action attribute.
// For Simple products
add_action( 'woocommerce_single_product_summary', 'second_button_after_product_summary', 30 );
function second_button_after_product_summary() {
global $product;
if( ! $product->is_type( 'variable' ) )
echo '<button type="submit" name="add-to-cart" value="'. esc_attr( $product->get_id() ).'" action="'.esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ).'" class="single_add_to_cart_button button alt">'. esc_html( $product->single_add_to_cart_text() ).'</button>';
}
If you are using external products or variation items add the action attribute appropriately. Please refer "plugins/woocommerce/templates/single-product/add-to-cart"

Wordpress - using Ajax on Woocommerce add to cart button shortcodes

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.

how to direct woocommerce cart content to shop page?

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.

Categories