Answer the question
In order to leave comments, you need to log in
How to make ajax price update based on the number of items in the mini cart?
Hello!
There is a mini-basket, I want to update the price from the number of goods. How to do it?
Mini cart code:
<?php
/**
* Mini-cart
*
* Contains the markup for the mini-cart, used by the cart widget.
*
* This template can be overridden by copying it to yourtheme/woocommerce/cart/mini-cart.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 5.2.0
*/
defined('ABSPATH') || exit;
do_action('woocommerce_before_mini_cart'); ?>
<?php if (!WC()->cart->is_empty()) : ?>
<div class="minicart__order">
<?php
do_action('woocommerce_before_mini_cart_contents');
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);
if ($_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters('woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key)) {
$product_name = apply_filters('woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key);
$thumbnail = apply_filters('woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key);
$product_price = apply_filters('woocommerce_cart_item_price', WC()->cart->get_product_price($_product), $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);
?>
<div class="minicart__product" <?php echo esc_attr(apply_filters('woocommerce_mini_cart_item_class', 'mini_cart_item', $cart_item, $cart_item_key)); ?>>
<div class="minicart__product__title">
<div class="minicart__product__name">
<?php echo wp_kses_post($product_name); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</div>
<div class="minicart__product__desc">
Ø23см
</div>
</div>
<div class="minicart__product__counter">
<div class="minicart__product__counter__minus" data-id = "<?php echo wp_kses_post($product_id); ?>">-</div>
<div class="minicart__product__counter__amount" data-id = "<?php echo wp_kses_post($product_id); ?>" data-quantity = "<?php echo wp_kses_post($cart_item['quantity']); ?>"><?php
if ( $_product->is_sold_individually() ) {
$product_quantity = sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key );
} else {
$product_quantity = woocommerce_quantity_input(
array(
'input_name' => "cart[{$cart_item_key}][qty]",
'input_value' => $cart_item['quantity'],
'max_value' => $_product->get_max_purchase_quantity(),
'min_value' => '0',
'product_name' => $_product->get_name(),
),
$_product,
false
);
}
echo apply_filters( 'woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item ); // PHPCS: XSS ok.
?></div>
<div class="minicart__product__counter__plus" data-id = "<?php echo wp_kses_post($product_id); ?>">+</div>
</div>
<div class="minicart__product__price">
<div>
<span>
<?php echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); // PHPCS: XSS ok. ?>
</span>
</div>
</div>
<?php echo wc_get_formatted_cart_item_data($cart_item); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</div>
<?php
}
}
do_action('woocommerce_mini_cart_contents');
?>
</div>
<div class="minicart__total">
<?php
/**
* Hook: woocommerce_widget_shopping_cart_total.
*
* @hooked woocommerce_widget_shopping_cart_subtotal - 10
*/
do_action('woocommerce_widget_shopping_cart_total');
?>
</div>
<?php do_action('woocommerce_widget_shopping_cart_before_buttons'); ?>
<p class="woocommerce-mini-cart__buttons buttons"><?php do_action('woocommerce_widget_shopping_cart_buttons'); ?></p>
<?php do_action('woocommerce_widget_shopping_cart_after_buttons'); ?>
<?php else : ?>
<p class="woocommerce-mini-cart__empty-message"><?php esc_html_e('No products in the cart.', 'woocommerce'); ?></p>
<?php endif; ?>
<?php do_action('woocommerce_after_mini_cart'); ?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question