php - PHP嵌套函数逻辑错误-用WPML翻译functions.php
问题描述
我正在使用我正在使用的自定义代码翻译结帐页面上显示的一段文本。如何在 PHP 中正确使用嵌套函数?
我将 echo 更改为 WPML 可识别的功能,但在前端无济于事。
add_action( 'woocommerce_review_order_before_submit', 'bbloomer_checkout_add_on', 9999 );
function bbloomer_checkout_add_on() {
$product_ids = array( 14877, 14879, 15493 );
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( in_array( $product_in_cart, $product_ids ) ) {
$in_cart = true;
break;
}
}
if ( ! $in_cart ) {
echo '<h4><b>● Would you like to add 10/20/30 small sample vials?</b></h4>';
function change_sm_location_search_title( $original_value ) {
return '<h4><b>' . __('● Would you like to add 10/20/30 small sample vials?','text-domain') . '</b></h4>';
}
add_filter( 'sm-location-search-title', 'change_sm_location_search_title' );
echo '<p><a class="button" style="width: 140px" href="?add-to-cart=1183"> €1.2 (10) </a><a class="button" style="width: 140px" href="?add-to-cart=9945"> €2.1 (20)</a><a class="button" style="width: 140px" href="?add-to-cart=9948"> €3 (30)</a></p>';
}
}
回声静止显示在前端,但新的文本域功能仅显示在后端。
解决方案
过滤器用于替换值。您应该将过滤器函数声明放在主函数之外,并使用 apply_filters 调用来使用过滤器。
您也可以改用动作挂钩。我建议阅读使用钩子和过滤器:https ://docs.presscustomizr.com/article/26-wordpress-actions-filters-and-hooks-a-guide-for-non-developers
这个答案是为了更好地理解过滤器是如何工作的:https ://wordpress.stackexchange.com/questions/97356/trouble-understanding-apply-filters
这应该有效(未经测试)。
add_action( 'woocommerce_review_order_before_submit', 'bbloomer_checkout_add_on', 9999 );
function bbloomer_checkout_add_on() {
$product_ids = array( 14877, 14879, 15493 );
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( in_array( $product_in_cart, $product_ids ) ) {
$in_cart = true;
break;
}
}
if ( ! $in_cart ) {
echo apply_filters('sm-location-search-title', 'Would you like to add 10/20/30 small sample vials?');
echo '<p><a class="button" style="width: 140px" href="?add-to-cart=1183"> €1.2 (10) </a><a class="button" style="width: 140px" href="?add-to-cart=9945"> €2.1 (20)</a><a class="button" style="width: 140px" href="?add-to-cart=9948"> €3 (30)</a></p>';
}
}
function change_sm_location_search_title( $original_value ) {
return '<h4><b>' . __($original_value,'text-domain') . '</b></h4>';
}
add_filter( 'sm-location-search-title', 'change_sm_location_search_title');
推荐阅读
- javascript - 节点 js,process.env 不读取环境变量
- flex-lexer - JFlex Block Comment 规则说明
- c++ - 在 4 字节值上使用运算符“*”,然后将结果转换为 8 字节值
- android - Unity:垂直组布局滚动不适用于 Android
- c - 了解输出背后的原因
- r - 处理比可用内存更大的 quanteda 对象
- php - 如何添加外部 PHP 文件以在 HTML 登录页面中传播
- string - 找到我们可以交换的子序列数,以便给定的字符串保持良好状态
- r - 使用 psych::ICC 时的错误消息:参数长度为零
- python - Conway 在 Python 中的生命游戏在应该是死区的地方生成随机的长线