Ajax remove coupon from mini-cart WooCommerce - php

I am using the mini-cart.php file and the cart widget to show cart content and I am using AJAX add to cart standard built in WooCommerce.
Adding products to my cart works fine. I added an area to show if any coupon is added (see my code). Works all fine but when deleting the coupon I want it to be done without loading a next page. It only needs to update the mini-cart.php. Can someone help me? Basically I need some function or something which updates mini-cart.php without reloading the page.
<?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
<p>Actieve kortingscode: <?php echo esc_attr( sanitize_title( $code ) ); ?></p>
<p><?php wc_cart_totals_coupon_html( $coupon ); ?></p>
<?php endforeach; ?>

You will find the answer to your question in WooCommerce documentation
https://docs.woocommerce.com/wc-apidocs/source-class-WC_Cart.html
The short answer is you need to make a call to WC()->cart->remove_coupons();
As of the "how", check this SO answer (different thing but same way to resolve it)
https://stackoverflow.com/a/41593818/10864482

Related

Woocommerce update payment methods based on shipping method

Im doing some changes on my eshop, I have moved shipping method and payment method choices from checkout page to cart page, but now those tables have lost their auto-update functionality, and this leads to getting order errors, based on "invalid payment method". I know I need to move some action, method or anything as well to the cart page, I just cant figure out what I need to move there.
Code I have moved from checkout to cart for displaying shipping methods
<?php if ( WC()->cart->needs_shipping() && WC()->cart->show_shipping() ) : ?>
<?php do_action( 'woocommerce_review_order_before_shipping' ); ?>
<?php wc_cart_totals_shipping_html(); ?>
<?php do_action( 'woocommerce_review_order_after_shipping' ); ?>
<?php endif; ?>
functions.php action change
remove_action('woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20);
add_action('order-checkout-methods', 'woocommerce_checkout_payment', 20);
Code I have moved from checkout to cart for displaying payment methods
<?php do_action('order-checkout-methods'); ?>
Problem is not in the custom function. The auto-update worked well with this, but only when it was in checkout
Any idea, what i need to add to the cart to make it work?
I see you have moved the display of shipping methods and payment methods from the checkout page to the cart page. but the issue is the auto-update functionality has been lost. that's why you need restore these.
To restore the auto-update functionality, you will need to move the relevant actions, methods, or code that handle the update of these fields to the cart page.

How to debug cart content in WooCommerce?

Found the following code in another thread:
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = wc_get_product( $values['data']->get_id());
echo "<b>".$_product->get_title().'</b> <br> Quantity: '.$values['quantity'].'<br>';
$price = get_post_meta($values['product_id'] , '_price', true);
echo " Price: ".$price."<br>";
}
?>
I'm a rookie when it comes to adding code.
Simple question: Where do I add this php code?
I have a test site, and would like to see the cart data (in order to try add some other customized code).
Will it simply print onto the page? Will it replacing the standard cart view? Can I view both?
Optimally it would open a new small browser window or gadget to show the "raw" data.
To add custom code to your site you first need to create a child theme. Then, you will need to insert the custom code inside the functions.php file of your active (child) theme.
If it's a staging/debug site you can use the woocommerce_before_cart hook to print the contents of the variables. Another check that you could add is to check if the current user is an administrator, so as not to see the data to other users of the site.
So it will be something like this:
add_action( 'woocommerce_before_cart', 'wc_cart_debug' );
function wc_cart_debug( $cart ) {
if ( ! current_user_can('administrator') ) {
return;
}
// Your code here.
}
RELATED ANSWERS
How to debug in WooCommerce 3
PHP and WordPress: Debugging
How to debug php code while developing wordpress plugins?
Debugging in PHP and Wordpress
Which is the best way to debug PHP in WordPress?

How to get my PHP Code Snippets work on my Wordpress website?

I am adding Prev and Next buttons to my Single Product Page on my Wordpress website with Woocommerce. I see that my code is working when I add it in my function.php but when I am trying to add it via PHP Code Snippet on my page it doesn't work. Can someone please help me with this?
p.s. I'm using PHP Code Snippets by XYZ PHP Code so I could put these buttons where I want, because otherwise if I use it in function.php it puts them only at the top of the page.
<?php
add_action( 'woocommerce_before_single_product', 'guzzz_prev_next_product' );
function guzzz_prev_next_product(){
echo '<div class="prev_next_buttons">';
// 'product_cat' will make sure to return next/prev from current category
next_post_link('<p style="float:right;">%link</p>', 'NEXT →', TRUE, ' ', 'product_cat');
previous_post_link('<p style="float:left;">%link</p>', '← PREVIOUS', TRUE,' ', 'product_cat');
echo '</div>';
}
I used the hook 'woocommerce_product_thumbnails' and kept the code in function.php. So my links are now where I want them to be, beneath the product image.

product description is not getting display after feature image - woocommerce shop

I am trying to make product short description below product feature image in the woocommerce shop page. I used the woocommerce shop hooks for the same. But the problem is, product description is getting displayed on top of the featured image. please see the image attached below.
Code Snipped in functions.php
function woocommerce_after_shop_loop_item_title_short_description() {
global $product;
if ( ! $product->get_short_description() ) return;
?>
<div itemprop="description">
<?php echo apply_filters( 'woocommerce_short_description', $product->get_short_description() ) ?>
</div>
<?php
}
add_action('woocommerce_after_shop_loop_item_title', 'woocommerce_after_shop_loop_item_title_short_description', 5);
I searched and tried different solutions and tried different woocommerce shop hooks mentioned here: (woocommerce_shop_loop_item_title etc) https://www.tychesoftwares.com/woocommerce-shop-page-hooks-visual-guide-with-code-snippets/ but hard luck.
Also can somebody tell me how to set fixed featured image size in shop page using woocommerce hooks, so that it won't get distorted (In below image size is not same) Any help would be really appreciated.
Based on your screenshot and the fact that this also happens with the default Storefront theme I would guess you customized they way your product thumbnail is being called. Moving it from the woocommerce_before_shop_loop_item_title hook to the woocommerce_after_shop_loop_item_title hook.
In that case changing the priority would help. Please try the following:
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_after_shop_loop_item_title_short_description', 20 );
function woocommerce_after_shop_loop_item_title_short_description() {
global $product;
if ( $short_description = $product->get_short_description() ) {
printf( '<div itemprop="description">%s</div>', $short_description );
}
}

Customizing add to cart message in Woocommerce 3

I added a link to the cart page inside the message that pops up when a product is successfully added to the cart in Woocommerce. This however does nothing when it's clicked. The inspector does show the correct working link.
// Add cart page URL
global $woocommerce;
$cart_url = $woocommerce->cart->get_cart_url();
$returnArray['messages']['success'] = sprintf(__('Products successfully added to cart! View cart','woocommerce'), $cart_url);
I think I should note that this part of the code is used twice in the page:
global $woocommerce;
$cart_url = $woocommerce->cart->get_cart_url();
I understand that using the same variable twice could cause issues but in this case they should do the same thing anyway but could this be the cause?
EDIT: changed the method for getting the cart page URL
$returnArray['messages']['success'] = sprintf( __('Products successfully added to cart! View cart','sf-european-fasteners'), wc_get_cart_url() );

Categories