当 WooCommerce 购物车和结帐页面上的购物车中的特定产品时更改 "Total" 文本
Change "Total" text when specific products in cart on WooCommerce cart and checkout pages
如果购物车中有特定产品 ID,我希望更改 WooCommerce 购物车和结帐页面(见附图)上的文本“总计”。
我尝试在 javascript 中实现这一点,但它只适用于所有人:
<script type="text/javascript">
(function($) {
$(document).ready(function() {
$('#your_my_order_element_id').html('Your New string');
//$('.your_my_order_element_class').html('Your New string');
});
})(jQuery);
</script>
有人可以把我推向正确的方向吗?任何帮助将不胜感激!
要更改购物车和结帐页面上的总文本,您可以编辑模板文件。可以在 templates/cart/cart-totals.php
中找到
可以通过将此模板复制到 yourtheme/woocommerce/cart/cart-totals.php.
来覆盖该模板
所以替换(第 97 行 - 100 - @version 2.3.6)
<tr class="order-total">
<th><?php esc_html_e( 'Total', 'woocommerce' ); ?></th>
<td data-title="<?php esc_attr_e( 'Total', 'woocommerce' ); ?>"><?php wc_cart_totals_order_total_html(); ?></td>
</tr>
有
<tr class="order-total">
<?php
// The targeted product ids, multiple product IDs can be entered, separated by a comma
$targeted_ids = array( 30, 815 );
// Flag, false by default
$flag = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$flag = true;
break;
}
}
// True
if ( $flag ) {
?>
<th><?php esc_html_e( 'Authorize', 'woocommerce' ); ?></th>
<td data-title="<?php esc_attr_e( 'Authorize', 'woocommerce' ); ?>"><?php wc_cart_totals_order_total_html(); ?></td>
<?php
} else {
?>
<th><?php esc_html_e( 'Total', 'woocommerce' ); ?></th>
<td data-title="<?php esc_attr_e( 'Total', 'woocommerce' ); ?>"><?php wc_cart_totals_order_total_html(); ?></td>
<?php
}
?>
</tr>
或者不覆盖模板文件,而是使用 gettext() 过滤器。
代码进入活动子主题(或活动主题)的 functions.php
文件
function filter_gettext( $translated, $original_text, $domain ) {
// Is admin
if ( is_admin() ) return $translated;
// No match
if ( $original_text != 'Total' ) return $translated;
// The targeted product ids, multiple product IDs can be entered, separated by a comma
$targeted_ids = array( 30, 815 );
// Flag, false by default
$flag = false;
// WC Cart
if ( WC()->cart ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$flag = true;
break;
}
}
}
// True
if ( $flag ) {
$translated = __( 'Authorize', 'woocommerce' );
}
return $translated;
}
add_filter( 'gettext', 'filter_gettext', 10, 3 );
如果购物车中有特定产品 ID,我希望更改 WooCommerce 购物车和结帐页面(见附图)上的文本“总计”。
我尝试在 javascript 中实现这一点,但它只适用于所有人:
<script type="text/javascript">
(function($) {
$(document).ready(function() {
$('#your_my_order_element_id').html('Your New string');
//$('.your_my_order_element_class').html('Your New string');
});
})(jQuery);
</script>
有人可以把我推向正确的方向吗?任何帮助将不胜感激!
要更改购物车和结帐页面上的总文本,您可以编辑模板文件。可以在 templates/cart/cart-totals.php
中找到可以通过将此模板复制到 yourtheme/woocommerce/cart/cart-totals.php.
所以替换(第 97 行 - 100 - @version 2.3.6)
<tr class="order-total">
<th><?php esc_html_e( 'Total', 'woocommerce' ); ?></th>
<td data-title="<?php esc_attr_e( 'Total', 'woocommerce' ); ?>"><?php wc_cart_totals_order_total_html(); ?></td>
</tr>
有
<tr class="order-total">
<?php
// The targeted product ids, multiple product IDs can be entered, separated by a comma
$targeted_ids = array( 30, 815 );
// Flag, false by default
$flag = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$flag = true;
break;
}
}
// True
if ( $flag ) {
?>
<th><?php esc_html_e( 'Authorize', 'woocommerce' ); ?></th>
<td data-title="<?php esc_attr_e( 'Authorize', 'woocommerce' ); ?>"><?php wc_cart_totals_order_total_html(); ?></td>
<?php
} else {
?>
<th><?php esc_html_e( 'Total', 'woocommerce' ); ?></th>
<td data-title="<?php esc_attr_e( 'Total', 'woocommerce' ); ?>"><?php wc_cart_totals_order_total_html(); ?></td>
<?php
}
?>
</tr>
或者不覆盖模板文件,而是使用 gettext() 过滤器。
代码进入活动子主题(或活动主题)的 functions.php
文件
function filter_gettext( $translated, $original_text, $domain ) {
// Is admin
if ( is_admin() ) return $translated;
// No match
if ( $original_text != 'Total' ) return $translated;
// The targeted product ids, multiple product IDs can be entered, separated by a comma
$targeted_ids = array( 30, 815 );
// Flag, false by default
$flag = false;
// WC Cart
if ( WC()->cart ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$flag = true;
break;
}
}
}
// True
if ( $flag ) {
$translated = __( 'Authorize', 'woocommerce' );
}
return $translated;
}
add_filter( 'gettext', 'filter_gettext', 10, 3 );