将产品“延期交货”替换为 Woocommerce 中的自定义字段值

问题描述 投票:0回答:4

首先感谢您查看这个问题。 我搜索并解决了许多类似的问题,但我还没有找到完美的解决方案。

我正在使用 wordpress/woocommerce 设置一个网站,但是我们的大多数产品都有固定的交货时间,因此一切都处于“延期交货 - 允许”状态。我不想在每个产品页面上显示“缺货”,而是想看看是否可以在每个产品中创建自定义字段并替换“缺货”文本以显示该自定义字段。

目前,我一直在使用以下代码,该代码仅更改每个产品的文本,但是,并非所有产品都在特定的交货时间内。

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);
}

我很感激我需要在每个产品中设置一个自定义字段并设置时间,但我不完全确定如何将每个产品的特定自定义字段链接到该 php 代码(或者更确切地说,是否实际上可能) .

任何帮助都会很棒 - 即使它告诉我这是不可能的!

php wordpress woocommerce product custom-fields
4个回答
4
投票

这可以通过以下代码来完成,该代码也可以处理产品和产品变体:

// 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 文件中。已测试并有效。

对于所有产品(可变产品除外,见下文)您将获得:

对于产品变化 (可变产品)


1
投票

该代码非常适合简单的产品。但对于变体,延期交货文本可用,但当客户做出选择时,它不会显示在产品页面上


0
投票

我稍微修改了 @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

过滤器

这适用于我的产品,但没有在简单产品上进行测试


0
投票

该代码非常适合简单的产品。但对于变化 延期交货文本可用,但不显示在 客户选择时的产品页面

我尝试了这个代码,它似乎适用于有变化的产品。

function custom_backorder_message( $message, $product ) {
    if ( $product->is_on_backorder() ) {
        $message = "My text...";
    }
    return $message;
}

add_filter( 'woocommerce_get_availability_text', 'custom_backorder_message', 10, 2 );
© www.soinside.com 2019 - 2024. All rights reserved.