从购物车中的 Woocommerce 计数跨度中删除单词 header
Remove word from Woocommerce count span in cart header
有人知道吗,我可以在 woocommerce 中的什么地方找到 .php
文件,我可以在其中从最后一个跨度中删除单词 "item" 或 "items"?
我已经用一些 jQuery 代码试过了,但只有当我完全加载页面时它才有效。当我单击添加到购物车或从购物车中删除项目时,购物车仅在没有我的 .js
文件的情况下重新加载 woocommerce 以删除这两个词。
有人可以帮助我吗?
谢谢
$('.count').html($('.count').html().replace(' items',''));
$('.count').html($('.count').html().replace(' item',''));
<a class="cart-contents" href="http://*****.de/warenkorb/" title="View your shopping cart">
<span class="amount">0,00 €</span>
<span class="count">0 items</span><!--Here I want to remove the Word items to show just the number-->
</a>
经过几天的思考,我找到了一个解决方案(我很高兴也很生气,因为当你知道答案时,解决方案就很容易了).
首先你必须找到文件 woocommerce/templates/cart/mini-cart.php 来覆盖我们的函数。
找到它后,您必须找到以下行:
<?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
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
ob_start();
?>
<a class="cart-contents" href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" title="<?php _e( 'View your shopping cart', 'storefront' ); ?>">
<span class="count"><?php echo sprintf (_n( '%d', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?></span>
</a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
?>
现在您必须保存文件并重新加载页面并将某些东西放入您的购物车(或删除)以更新您的购物车。知道应该做! :-)
如果您也想将价格添加到 header,您还必须在 <span class="count">
上方添加以下代码行:
<span class="amount"><?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?></span>
如果您有任何问题,可以随时给我留言……
为避免更新 Storefront 时这些调整被覆盖的风险,您还可以像这样重写 functions.php 文件中的函数:
if ( ! function_exists( 'storefront_cart_link' ) ) {
function storefront_cart_link() {
?>
<a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'storefront' ); ?>">
<?php /* translators: %d: number of items in cart */ ?>
<?php echo wp_kses_post( WC()->cart->get_cart_subtotal() ); ?> <span class="count"><?php echo WC()->cart->get_cart_contents_count(); ?></span>
</a>
<?php
}
}
有人知道吗,我可以在 woocommerce 中的什么地方找到 .php
文件,我可以在其中从最后一个跨度中删除单词 "item" 或 "items"?
我已经用一些 jQuery 代码试过了,但只有当我完全加载页面时它才有效。当我单击添加到购物车或从购物车中删除项目时,购物车仅在没有我的 .js
文件的情况下重新加载 woocommerce 以删除这两个词。
有人可以帮助我吗?
谢谢
$('.count').html($('.count').html().replace(' items',''));
$('.count').html($('.count').html().replace(' item',''));
<a class="cart-contents" href="http://*****.de/warenkorb/" title="View your shopping cart">
<span class="amount">0,00 €</span>
<span class="count">0 items</span><!--Here I want to remove the Word items to show just the number-->
</a>
经过几天的思考,我找到了一个解决方案(我很高兴也很生气,因为当你知道答案时,解决方案就很容易了).
首先你必须找到文件 woocommerce/templates/cart/mini-cart.php 来覆盖我们的函数。
找到它后,您必须找到以下行:
<?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
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
ob_start();
?>
<a class="cart-contents" href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" title="<?php _e( 'View your shopping cart', 'storefront' ); ?>">
<span class="count"><?php echo sprintf (_n( '%d', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?></span>
</a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
?>
现在您必须保存文件并重新加载页面并将某些东西放入您的购物车(或删除)以更新您的购物车。知道应该做! :-)
如果您也想将价格添加到 header,您还必须在 <span class="count">
上方添加以下代码行:
<span class="amount"><?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?></span>
如果您有任何问题,可以随时给我留言……
为避免更新 Storefront 时这些调整被覆盖的风险,您还可以像这样重写 functions.php 文件中的函数:
if ( ! function_exists( 'storefront_cart_link' ) ) {
function storefront_cart_link() {
?>
<a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'storefront' ); ?>">
<?php /* translators: %d: number of items in cart */ ?>
<?php echo wp_kses_post( WC()->cart->get_cart_subtotal() ); ?> <span class="count"><?php echo WC()->cart->get_cart_contents_count(); ?></span>
</a>
<?php
}
}