【问题标题】:Replace product "on backorder" to a custom field value in Woocommerce将产品“延期交货”替换为 Woocommerce 中的自定义字段值
【发布时间】:2018-05-10 07:04:32
【问题描述】:

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

我正在使用 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 代码(或者更确切地说,它是否真的可能)。

任何帮助都会很棒 - 即使它告诉我它无法完成!

【问题讨论】:

  • 您可以使用 ACF(高级自定义字段)插件在每个产品后端显示文本字段,并使用 get_field('name') 调用该字段;在前端。您还可以根据需要修改该字段。 wordpress.org/plugins/acf-for-woocommerce-product , businessbloomer.com/…
  • 感谢您的评论,我现在正在安装 ACF。你知道修改后的 php 会从上面的那个中得到什么吗? (本人缺乏php知识,学习缓慢)
  • 我尝试了您提供的两个 php 代码,但都产生了致命错误。我会更多地研究它,看看我是否可以在网上找到任何东西,因为我很想让它工作。感谢您花时间研究它并提供帮助!

标签: php wordpress woocommerce product custom-fields


【解决方案1】:

这可以使用以下代码来完成,它也将处理产品和产品变化:

// 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 文件中。经过测试并且可以工作。

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

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

【讨论】:

  • 您好,尽管您已经回答了我的问题,我非常感激。我想问一下是否有可能使上述代码对“购物车/结帐页面”也有效。
  • 我错过了这条评论,抱歉。如果您仍然需要它,请重新询问我会给您一些答案链接,这将允许它......如果您愿意,您也可以提出一个新问题。当您获得这种能力时,如果您喜欢/想要,请投票赞成这个答案。谢谢。
【解决方案2】:

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

【讨论】:

  • 所以您的自定义文本不显示,但默认文本仍然显示?
  • 很抱歉通信延迟。因此,如果我在简单产品上设置一条消息,说明“可延期交货”。周转1周。如果用户/客户选择了该简单产品,则会出现该消息。但是,如果您在可变产品上设置相同的按摩并且客户从下拉菜单中选择它。该消息不出现。希望这是有道理的
  • 是的,但是当他们选择可变产品时,您会得到默认文本吗?
  • 如果他们选择变量产品,他们不会得到默认文本
  • OK - 然后删除所有自定义代码,看看他们在选择变量产品时是否获得默认文本。如果没有,那是你的问题。这将是 Wordpress 中的一些设置
【解决方案3】:

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

过滤器

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

【讨论】:

    猜你喜欢
    • 2020-11-19
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 2019-05-09
    相关资源
    最近更新 更多