在这个线程的帮助下,我有一个功能可以显示和更新 WooCommerce 中使用 AJAX 的购物车的金额和总价。我没有想到的问题是我在网站上使用 PolyLang,因此希望在用户更改页面上的语言时翻译“产品”和“产品”的关键字。
我尝试pll_current_language()
在 AJAX 调用之前在条件中使用钩子functions.php
,然后定义变量$item
并$items
根据当前语言是什么,并将这些关键字插入 AJAX 返回的字符串中。我在模板文件中使用了类似的条件,所以它在那里工作。
但是,此功能无法按预期工作。这就是我的设置方式:
函数.php
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
// Cart Translations
$lang = pll_current_language();
$item;
$items;
if ($lang == 'sv'){
$item = "produkt";
$items = "produkter";
} else if ($lang == 'en') {
$item = "product";
$items = "products";
}
?>
<a class="cart-customlocation wpmenucart" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf ( _n( '%d ' . $item, '%d ' . $items, WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-customlocation'] = ob_get_clean();
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment', 30, 1 );
我遇到的问题是pll_current_language()
,即使我访问英文页面,钩子总是返回“sv”。所以看起来我不能在functions.php中使用那个钩子,或者我错过了什么?您对如何完成这项工作有任何建议吗?