我试图在Woocommerce购物车小部件的底部或顶部显示购物车中物品的数量.似乎默认情况下,在侧栏中时,购物车小部件不会执行此操作.
我研究了SO,发现了以下answer,但添加单个小计的目标略有不同.我尝试通过传递WC()-> cart-> get_cart_contents_count()而不是WC()-> cart-> get_cart()来进行修改,以查看是否可以获取购物车商品计数,但是没有显示…任何建议?
我尝试做的例子:
<?PHP
/**
* Mini-cart
*
* Contains the markup for the mini-cart, used by the cart widget
*
* @author woothemes
* @package WooCommerce/Templates
* @version 2.1.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->get_cart_contents_count() ) > 0 ) : ?>
<?PHP
foreach ( WC()->cart->get_cart_contents_count() 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_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 );
echo "<p>".$product_price."</p>";
?>
<li>
<?PHP if ( ! $_product->is_visible() ) { ?>
<?PHP echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name; ?>
<?PHP } else { ?>
<a href="<?PHP echo get_permalink( $product_id ); ?>">
<?PHP echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name; ?>
</a>
<?PHP } ?>
<?PHP echo WC()->cart->get_item_data( $cart_item ); ?>
<?PHP $new_product_price_array = explode ( get_woocommerce_currency_symbol(), $product_price);
$new_product_price = number_format((float)$new_product_price_array[1] * $cart_item['quantity'], 2, '.', '');
?>
<?PHP echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s × %s=%s%s', $cart_item['quantity'], $product_price,get_woocommerce_currency_symbol(), $new_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->get_cart_contents_count() ) > 0 ) : ?>
<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 WC()->cart->get_cart_url(); ?>" class="button wc-forward"><?PHP _e( 'View Cart', 'woocommerce' ); ?></a>
<a href="<?PHP echo WC()->cart->get_checkout_url(); ?>" class="button checkout wc-forward"><?PHP _e( 'Checkout', 'woocommerce' ); ?></a>
</p>
<?PHP endif; ?>
<?PHP do_action( 'woocommerce_after_mini_cart' ); ?>
解决方法:
如果要在迷你购物车小部件中显示购物车项目计数,可以使用挂钩:
1)在微型车内容之前:
add_action( 'woocommerce_before_mini_cart', 'minicart_count_after_content' );
function minicart_count_after_content() {
$items_count = WC()->cart->get_cart_contents_count();
$text_label = _n( 'Item', 'Items', $items_count, 'woocommerce' );
?>
<p class="total item-count"><strong><?PHP echo $text_label; ?>:</strong> <?PHP echo $items_count; ?></p>
<?PHP
}
2)在按钮下方的底部:
add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'minicart_count_before_content' );
function minicart_count_before_content() {
$items_count = WC()->cart->get_cart_contents_count();
$text_label = _n( 'Item', 'Items', $items_count, 'woocommerce' );
?>
<p class="total item-count"><strong><?PHP echo $text_label; ?>:</strong> <?PHP echo $items_count; ?></p>
<?PHP
}
代码进入您的活动子主题(或活动主题)的function.PHP文件中.经过测试和工作.
WC() – > cart-> get_cart_contents_count();将为您提供总项目数,包括数量.
如果要获取购物车中的物品数,请改用:
$items_count = sizeof( WC()->cart->get_cart() );
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。