0
我正在使用WooCommerce小推車小部件,並且我想限制顯示在小推車中的產品的數量。如果用戶將10種產品添加到購物車中,則迷你購物車小部件會顯示全部10.限制在小推車小部件中顯示的產品的數量
我想限制數量可以這麼說5個產品。
有沒有辦法做到這一點?
謝謝。
我正在使用WooCommerce小推車小部件,並且我想限制顯示在小推車中的產品的數量。如果用戶將10種產品添加到購物車中,則迷你購物車小部件會顯示全部10.限制在小推車小部件中顯示的產品的數量
我想限制數量可以這麼說5個產品。
有沒有辦法做到這一點?
謝謝。
你可以通過你的活動主題,實現這一首要cart/mini-cart.php
WooCommerce模板(見本相關的文檔:Template Structure + Overriding Templates via a Theme)。
簡單(如果尚未完成),您將需要從woocommerce
插件文件夾複製,一個名爲templates
到您的活動子主題(或主題)子文件夾,將其重命名woocommerce
..
之後,你會發現新的woocommerce
文件夾內的cart
子文件夾命名模板mini-cart.php
。
打開/編輯mini-cart.php
模板,並通過這種替換代碼,限制車5個項目:
<?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/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.5.0
*/
if (! defined('ABSPATH')) {
exit; // Exit if accessed directly
}
?>
<?php do_action('woocommerce_before_mini_cart'); ?>
<ul class="cart_list product_list_widget <?php echo $args['list_class']; ?>">
<?php if (! WC()->cart->is_empty()) : ?>
<?php
// Define HERE the number of items
$number_of_items = 5;
$loop_count = 0;
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
// Limiting number of items displayed in mini cart
if($loop_count < $number_of_items) {
// The counter
$loop_count++;
$_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_title(), $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);
?>
<li class="<?php echo esc_attr(apply_filters('woocommerce_mini_cart_item_class', 'mini_cart_item', $cart_item, $cart_item_key)); ?>">
<?php
echo apply_filters('woocommerce_cart_item_remove_link', sprintf(
'<a href="%s" class="remove" title="%s" data-product_id="%s" data-product_sku="%s">×</a>',
esc_url(WC()->cart->get_remove_url($cart_item_key)),
__('Remove this item', 'woocommerce'),
esc_attr($product_id),
esc_attr($_product->get_sku())
), $cart_item_key);
?>
<?php if (! $_product->is_visible()) : ?>
<?php echo str_replace(array('http:', 'https:'), '', $thumbnail) . $product_name . ' '; ?>
<?php else : ?>
<a href="<?php echo esc_url($product_permalink); ?>">
<?php echo str_replace(array('http:', 'https:'), '', $thumbnail) . $product_name . ' '; ?>
</a>
<?php endif; ?>
<?php echo WC()->cart->get_item_data($cart_item); ?>
<?php echo apply_filters('woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf('%s × %s', $cart_item['quantity'], $product_price) . '</span>', $cart_item, $cart_item_key); ?>
</li>
<?php
}
}
}
?>
<?php else : ?>
<li class="empty"><?php _e('No products in the cart.', 'woocommerce'); ?></li>
<?php endif; ?>
</ul><!-- end product list -->
<?php if (! WC()->cart->is_empty()) : ?>
<p class="total"><strong><?php _e('Subtotal', 'woocommerce'); ?>:</strong> <?php echo WC()->cart->get_cart_subtotal(); ?></p>
<?php do_action('woocommerce_widget_shopping_cart_before_buttons'); ?>
<p class="buttons">
<a href="<?php echo esc_url(wc_get_cart_url()); ?>" class="button wc-forward"><?php _e('View Cart', 'woocommerce'); ?></a>
<a href="<?php echo esc_url(wc_get_checkout_url()); ?>" class="button checkout wc-forward"><?php _e('Checkout', 'woocommerce'); ?></a>
</p>
<?php endif; ?>
<?php do_action('woocommerce_after_mini_cart'); ?>
在這裏,我們計算在foreach循環中的項目數,我們將其限制到指定號碼在變量$number_of_items
。
非常好。感謝它就像一個魅力。 – Ayanize