将产品 "on backorder" 替换为 Woocommerce 中的自定义字段值
Replace product "on backorder" to a custom field value in Woocommerce
首先,感谢您查看此问题。
我已经搜索并解决了许多类似的问题,但是我没有设法找到完美的解决方案。
我正在使用 wordpress/woocommerce 建立网站,但是我们的大多数产品都有固定的交货时间,因此一切都处于 "back order - allow" 状态。我不想在每个产品页面上显示 "on backorder",而是想看看是否可以在每个产品中创建自定义字段并替换 "on backorder" 文本以显示该自定义字段。
目前,我一直在使用以下代码来更改每个产品的文本,但是,并非所有产品都在特定的交货时间。
add_filter( 'woocommerce_get_availability', 'backorder_text', 10, 2);
function backorder_text($available) {
return str_replace('Available on backorder', 'Approx lead time: 2-4 working weeks', $available);
}
我很感激我需要在设定的时间在每个产品中设置一个自定义字段,但我不完全确定如何 link 每个产品的特定自定义字段 php 代码(或者更确切地说,它是否真的可能)。
任何帮助都会很棒 - 即使它告诉我它无法完成!
这可以通过以下代码完成,该代码也将处理产品和产品变体:
// Add a custom field in admin product edit pages - inventory tab
add_action( 'woocommerce_product_options_stock_fields', 'add_product_options_stock_custom_field', 20 );
function add_product_options_stock_custom_field() {
global $product_object, $post;
woocommerce_wp_text_input( array(
'id' => '_backorder_text',
'type' => 'text',
'label' => __( 'Backorders text', 'woocommerce' ),
'description' => __( 'Backorders text. Add a custom backorders text to be displayed when products are on backorders.', 'woocommerce' ),
'desc_tip' => true,
) );
// jQuery: HIDE the fied if backorders are not enabled
?>
<script type="text/javascript">
jQuery( function($){
var a = 'select#_backorders',
b = 'p._backorder_text_field';
if( $(a).val() === 'no' )
$(b).hide();
$(a).on('change blur', function(){
if( $(a).val() === 'no' )
$(b).hide();
else
$(b).show();
});
});
</script>
<?php
}
// Save the custom field value from admin product edit pages - inventory tab
add_action( 'woocommerce_process_product_meta', 'save_product_options_stock_custom_field', 20, 1 );
function save_product_options_stock_custom_field( $product_id ) {
if ( isset( $_POST['_backorder_text'] ) )
update_post_meta( $product_id, '_backorder_text', sanitize_text_field( $_POST['_backorder_text'] ) );
}
// Variations: Add a custom field in admin variation options inventory
add_action( 'woocommerce_variation_options_inventory', 'add_variation_settings_fields', 20, 3 );
function add_variation_settings_fields( $loop, $variation_data, $variation_post ) {
woocommerce_wp_text_input( array(
'id' => '_backorder_text'.$loop,
'name' => '_backorder_text['.$loop.']',
'value' => get_post_meta( $variation_post->ID, '_backorder_text', true ),
'type' => 'text',
'label' => __( 'Backorders text', 'woocommerce' ),
'description' => __( 'Backorders text. Add a custom backorders text to be displayed when products are on backorders.', 'woocommerce' ),
'desc_tip' => true,
'wrapper_class' => 'form-row form-row-first',
) );
}
// Variations: Save a custom field value from admin variation options inventory
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
function save_variation_settings_fields( $variation_id, $i ) {
if( isset( $_POST['_backorder_text'][$i] ) )
update_post_meta( $variation_id, '_backorder_text', sanitize_text_field( $_POST['_backorder_text'][$i] ) );
}
add_filter( 'woocommerce_get_availability', 'custom_on_backorder_text', 10, 2 );
function custom_on_backorder_text( $availability, $product ) {
$backorder_text = get_post_meta( $product->get_id(), '_backorder_text', true );
if( $availability['class'] === 'available-on-backorder' && ! empty( $backorder_text ) )
$availability['availability'] = $backorder_text;
return $availability;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
对于所有产品(可变产品除外,见下文)您将获得:
对于产品变体 (可变产品的):
该代码非常适合简单的产品。但是对于变体,延期交货文本可用,但当客户做出选择时它不会显示在产品页面上
我稍微修改了@LoicTheAztec 代码以使用它在前端显示延期交货文本。
add_filter( 'woocommerce_get_availability_text', 'customize_availability_text', 10, 2);
function customize_availability_text( $availability, $product ) {
$backorder_text = get_post_meta( $product->get_id(), '_backorder_text', true );
if (! empty( $backorder_text )){$availability = str_replace('Available on backorder', $backorder_text , $availability);}
return $availability;
}
请注意,我使用的是
woocommerce_get_availability_text
过滤器
而不是
woocommerce_get_availability
过滤器
这适用于我的变体产品,未在简单产品上测试
首先,感谢您查看此问题。 我已经搜索并解决了许多类似的问题,但是我没有设法找到完美的解决方案。
我正在使用 wordpress/woocommerce 建立网站,但是我们的大多数产品都有固定的交货时间,因此一切都处于 "back order - allow" 状态。我不想在每个产品页面上显示 "on backorder",而是想看看是否可以在每个产品中创建自定义字段并替换 "on backorder" 文本以显示该自定义字段。
目前,我一直在使用以下代码来更改每个产品的文本,但是,并非所有产品都在特定的交货时间。
add_filter( 'woocommerce_get_availability', 'backorder_text', 10, 2);
function backorder_text($available) {
return str_replace('Available on backorder', 'Approx lead time: 2-4 working weeks', $available);
}
我很感激我需要在设定的时间在每个产品中设置一个自定义字段,但我不完全确定如何 link 每个产品的特定自定义字段 php 代码(或者更确切地说,它是否真的可能)。
任何帮助都会很棒 - 即使它告诉我它无法完成!
这可以通过以下代码完成,该代码也将处理产品和产品变体:
// Add a custom field in admin product edit pages - inventory tab
add_action( 'woocommerce_product_options_stock_fields', 'add_product_options_stock_custom_field', 20 );
function add_product_options_stock_custom_field() {
global $product_object, $post;
woocommerce_wp_text_input( array(
'id' => '_backorder_text',
'type' => 'text',
'label' => __( 'Backorders text', 'woocommerce' ),
'description' => __( 'Backorders text. Add a custom backorders text to be displayed when products are on backorders.', 'woocommerce' ),
'desc_tip' => true,
) );
// jQuery: HIDE the fied if backorders are not enabled
?>
<script type="text/javascript">
jQuery( function($){
var a = 'select#_backorders',
b = 'p._backorder_text_field';
if( $(a).val() === 'no' )
$(b).hide();
$(a).on('change blur', function(){
if( $(a).val() === 'no' )
$(b).hide();
else
$(b).show();
});
});
</script>
<?php
}
// Save the custom field value from admin product edit pages - inventory tab
add_action( 'woocommerce_process_product_meta', 'save_product_options_stock_custom_field', 20, 1 );
function save_product_options_stock_custom_field( $product_id ) {
if ( isset( $_POST['_backorder_text'] ) )
update_post_meta( $product_id, '_backorder_text', sanitize_text_field( $_POST['_backorder_text'] ) );
}
// Variations: Add a custom field in admin variation options inventory
add_action( 'woocommerce_variation_options_inventory', 'add_variation_settings_fields', 20, 3 );
function add_variation_settings_fields( $loop, $variation_data, $variation_post ) {
woocommerce_wp_text_input( array(
'id' => '_backorder_text'.$loop,
'name' => '_backorder_text['.$loop.']',
'value' => get_post_meta( $variation_post->ID, '_backorder_text', true ),
'type' => 'text',
'label' => __( 'Backorders text', 'woocommerce' ),
'description' => __( 'Backorders text. Add a custom backorders text to be displayed when products are on backorders.', 'woocommerce' ),
'desc_tip' => true,
'wrapper_class' => 'form-row form-row-first',
) );
}
// Variations: Save a custom field value from admin variation options inventory
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
function save_variation_settings_fields( $variation_id, $i ) {
if( isset( $_POST['_backorder_text'][$i] ) )
update_post_meta( $variation_id, '_backorder_text', sanitize_text_field( $_POST['_backorder_text'][$i] ) );
}
add_filter( 'woocommerce_get_availability', 'custom_on_backorder_text', 10, 2 );
function custom_on_backorder_text( $availability, $product ) {
$backorder_text = get_post_meta( $product->get_id(), '_backorder_text', true );
if( $availability['class'] === 'available-on-backorder' && ! empty( $backorder_text ) )
$availability['availability'] = $backorder_text;
return $availability;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
对于所有产品(可变产品除外,见下文)您将获得:
对于产品变体 (可变产品的):
该代码非常适合简单的产品。但是对于变体,延期交货文本可用,但当客户做出选择时它不会显示在产品页面上
我稍微修改了@LoicTheAztec 代码以使用它在前端显示延期交货文本。
add_filter( 'woocommerce_get_availability_text', 'customize_availability_text', 10, 2);
function customize_availability_text( $availability, $product ) {
$backorder_text = get_post_meta( $product->get_id(), '_backorder_text', true );
if (! empty( $backorder_text )){$availability = str_replace('Available on backorder', $backorder_text , $availability);}
return $availability;
}
请注意,我使用的是
woocommerce_get_availability_text
过滤器 而不是
woocommerce_get_availability
过滤器
这适用于我的变体产品,未在简单产品上测试