First, here's the website: https://aypac.de/woocommerce/shop/ (please don't mind the CSS fuck-ups or the absence of a redirection button to the cart/checkout).
So I've copied the apply coupon code from the cart page to my widget/shortcode which displays the cart as a sidebar, and it works but I'm having problems with unwanted redirections.
Apply coupon reloads the whole page while it would be nicer if it would only reload the side cart (or the whole widget area).
More problematic is that the remove coupon button sends me to the cart page instead of reloading the same page.
I think it's possible to do since it works with deleting items (it's somehow linked to the class used for the button). I tried tweaking stuff in the coupon code itself thanks to a filter, but it didn't do any good (I barely know any php tbh, I can basically read it and copy-paste stuff around, but not write it per say).
Here's the code for the sidecart (a shortcode in a widget area), the last ~20 lines are for the coupons:
<?php
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( is_admin() ) return false;
// Required to calculate deposit
WC()->cart->calculate_fees();
//$dec_p = !empty($locale['decimal_point']) ? $locale['decimal_point'] : ',';
// Calculate the total cart weight (incl. jars)
$prod_weight = WC()->cart->get_cart_contents_weight()/1000;
$jar_weight = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
//var_dump($cart_item);
//$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
if (!empty($cart_item['variation']) && !empty($cart_item['variation']['attribute_pa_volume'])) {
$v = strtolower($cart_item['variation']['attribute_pa_volume']);
if (in_array($v, jar_to_weight)) {
$jar_weight += jar_to_weight[$v];
}
}
}
$tot_weight = $jar_weight + $prod_weight;
$weightclass = ($tot_weight < 6) ? 'low_weight' : (($tot_weight < 12) ? 'med_weight' : 'hig_weight');
?>
<div class="LCKL_shop-sidecart">
<?php if( WC()->cart->is_empty() ) { ?>
<div class="LCKL_shop-empty">
<h3>
Your basket is empty
</h3>
</div>
<?php
} else {
do_action( 'LCKL_shop-before-items' );
?>
<ul class="LCKL_shop-items">
<?php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
$item_subtotal = $cart_item['data']->get_price() * $cart_item['quantity']; //WC()->cart->get_product_price( $_product )
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
}
// Get parent product name
$parent_product = wc_get_product($cart_item['product_id']);
$product_name = $parent_product->get_name();
$volume_html = $_product->get_attribute('pa_volume');
$volume_html = esc_html(apply_filters( 'woocommerce_variation_option_name', $volume_html));
$vol_wei = $volume_html;
if ($volume_html && $_product->get_weight() > 0) {
$vol_wei .= '/';
}
if ($_product->get_weight() > 0) {
$vol_wei .= wc_format_weight($_product->get_weight());
}
?>
<!--Start of item row-->
<li class="LCKL_shop-item">
<div class="LCKL_shop-title">
<span>
<?php
if ( ! $product_permalink ) {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $product_name, $cart_item, $cart_item_key ) . ' ' );
} else {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $product_name ), $cart_item, $cart_item_key ) );
}
do_action( 'woocommerce_after_cart_item_name', $cart_item, $cart_item_key ); ?>
</span>
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
'<a href="%s" class="remove_from_cart_button" aria-label="%s" data-product_id="%s" data-cart_item_key="%s" data-product_sku="%s"><i class="icon-bin"></i></a>',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $cart_item_key ),
esc_attr( $_product->get_sku() )
), $cart_item_key );
?>
</div>
<div>
<?= $vol_wei ?>
</div>
<div class="LCKL_shop-pricing">
<span>
<?= esc_html( $cart_item['quantity'] ); ?>
×
<?= apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); ?>
<!--+ <i class="icon-deposit1"></i>-->
</span>
<span class="LCKL_shop-price">
<?php echo apply_filters( 'woocommerce_cart_item_price', wc_price($item_subtotal) ). get_option( 'woocommerce_price_display_suffix' ); ?>
</span>
<!--
<div class="wc-block-components-quantity-selector">
<input class="wc-block-components-quantity-selector__input" type="number" step="1" min="1" max="99" aria-label="Quantity of Oat flakes in your cart." value="<?= esc_html( $cart_item['quantity'] ); ?>">
<button aria-label="Reduce quantity" class="wc-block-components-quantity-selector__button wc-block-components-quantity-selector__button--minus">-</button>
<button aria-label="Increase quantity" class="wc-block-components-quantity-selector__button wc-block-components-quantity-selector__button--plus">+</button>
</div> -->
</div>
</li>
<?php } ?>
<!--End of item row-->
</ul>
<div class="LCKL_shop-subtotal">
<table>
<tr>
<td class="LCKL_tooltip"><span>Cart weight:</span><span class="LCKL_tooltiptext">Includes the weight of the jars; make sure you can carry everything!</span></td>
<td class="<?= $weightclass ?>">
<?= number_format($tot_weight, 2) . ' kg' ?>
</td>
</tr>
<tr>
<td>Subtotal:</td>
<td><?php wc_cart_totals_subtotal_html(); ?></td>
</tr>
<?php foreach ( WC()->cart->get_fees() as $fee ) : ?>
<tr>
<td class="LCKL_tooltip"><span><?= esc_html( $fee->name ) ?></span>:
<span class="LCKL_tooltiptext">Amount that you will get back when you return all the jars and/or bags</span></td>
<td><?php wc_cart_totals_fee_html( $fee ); ?></td>
</tr>
<?php endforeach; ?>
<?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
<tr class="coupon-<?php echo esc_attr( sanitize_title( $code ) ); ?>">
<td><?php wc_cart_totals_coupon_label( $coupon ); ?></td>
<td><?php wc_cart_totals_coupon_html( $coupon ); ?></td>
</tr>
<?php endforeach; ?>
</table>
<form method="post">
<input type="text" name="coupon_code" class="input-text" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" id="coupon_code" value="" />
<button type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply', 'woocommerce' ); ?>"><?php esc_html_e( 'Apply', 'woocommerce' ); ?></button>
<div><?php _e( 'Total:', 'sidebar-cart' ); ?></div>
<div data-title="<?php esc_attr_e( 'Total', 'woocommerce' ); ?>"></div>
<div><?php wc_cart_totals_order_total_html();?></div>
</div>
<?php } ?>
</div>
I also have this filter to replace the text for the remove button in my functions.php:
function filter_woocommerce_cart_totals_coupon_html( $coupon_html, $coupon, $discount_amount_html ) {
// Change text
$coupon_html = str_replace( '[Remove]', '<i class="icon-bin"></i>', $coupon_html );
return $coupon_html;
}
add_filter( 'woocommerce_cart_totals_coupon_html', 'filter_woocommerce_cart_totals_coupon_html', 10, 3 );