【发布时间】:2017-05-10 09:18:28
【问题描述】:
在社区的帮助下,我成功地创建、保存和打印标签及其值到单个产品页面。
我还可以使用 Polylang 将输入值翻译成不同的语言,但是翻译自定义标签(条件和品牌)非常困难。
有人可以帮我解决这些问题吗?
我尝试使用 Polylang、Saywhat...没有成功!
代码如下:
// Enabling and Displaying Fields in backend
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input( array( // Text Field type
'id' => '_conditions',
'label' => __( 'Conditions', 'woocommerce' ),
'placeholder' => 'i.e: brand-new; refurbished; defected...',
'desc_tip' => 'true',
'description' => __( 'Enter the conditions of the products here.', 'woocommerce' )
) );
woocommerce_wp_text_input( array( // Text Field type
'id' => '_brands',
'label' => __( 'Brands', 'woocommerce' ),
'placeholder' => 'i.e: Lacoste; Hugo Boss...etc',
'desc_tip' => 'true',
'description' => __( 'Enter names of the Brands of the products if any.', 'woocommerce' )
));
echo '</div>'; // Closing </div> tag HERE
}
// Save Fields values to database when submitted (Backend)
add_action( 'woocommerce_process_product_meta', 'woo_save_custom_general_fields' );
function woo_save_custom_general_fields( $post_id ){
// Saving "Conditions" field key/value
$conditions_field = $_POST['_conditions'];
if( !empty( $conditions_field ) )
update_post_meta( $post_id, '_conditions', esc_attr( $conditions_field ) );
// Saving "Brands" field key/value
$brands_field = $_POST['_brands'];
if( !empty( $brands_field ) )
update_post_meta( $post_id, '_brands', esc_attr( $brands_field ) );
}
add_action('woocommerce_single_product_summary', 'woo_display_custom_general_fields_values', 20);
function woo_display_custom_general_fields_values() {
global $product;
echo '<p class="custom-conditions">Conditions: ' . get_post_meta( $product->id, '_conditions', true ) . '</p>';
echo '<p class="custom-brands">Brands: ' . get_post_meta( $product->id, '_brands', true ) . '</p>';
}
这里是截图:
谢谢。
【问题讨论】:
-
我试过 loco 但没有帮助。老实说,我没有努力。我仍在寻找 Polylang 中的解决方案,因为我发现 Polylang 真的很棒!
-
Polylang 不会真正与 WooCommerce 完全兼容......你会看到,可能为时已晚,当你的项目大部分完成时(在这种情况下,删除 Polylang 并为另一个插件设置所有内容将是一个漫长而复杂的过程)...我根据经验告诉你,我在任何地方都使用 WPML,没有任何问题。所以现在考虑一下可能对你有好处。
-
感谢您的建议。是的,我改用 WPML,这个过程有点复杂,但它似乎比 Polylang 更稳定,更容易使用。可惜 Polylang!
标签: php wordpress woocommerce translation custom-fields