Woocommerce“延期交货”文本值取决于实际产品交货时间

我有很多缺货交货时间不同的产品。我根据产品类型创建了一个 SELECT 值,这显示了不同产品的不同交货时间。当我输入我拥有的所有产品的交货时间时,这很有效。直到我想要改变交货时间为止。我有不同的品牌,它们有不同的交货时间,而且它们往往会不时发生变化。如果发生这种情况,我不想手动更改所有产品。我想为不同类型的产品创建一个代码,我希望有可能不时轻松更改交货时间。

我为产品创建了“选择”选项,这样我就可以更改我想要的交货时间。一个品牌有 5 天,另一个 10 天等等。但不幸的是,当我在functions.php 中更新代码中的品牌提前期值时,产品在管理中的产品实际更新之前不会更新。这会导致我的代码无法正常工作,因为我仍然需要在交付周期发生变化时手动更新所有产品。

如果我在代码中有某种变量,我可以选择在所有产品交货时间自动更新之后更改变量,那就太好了。这是我目前拥有的代码 - 此代码非常有用,但仅在我不需要修改交货时间时才有效。

有人知道如何获得这种动态吗?我想不时更新品牌 x、y 和 z 的提前期,我需要产品自动更新提前期。谢谢!

 // 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;


// Select
woocommerce_wp_select( array( 
    'id'      => '_backorder_text','label'   => __( 'Backorders text','woocommerce' ),'description' => __( 'Backorders text. Add a custom backorders text to be displayed when products are on backorders.','options' => array(
        ''         => __( '','Shipping: 1 day'   => __( 'Brand x','Shipping: 3-5 days'   => __( 'Brand y','Shipping: 5-10 days' => __( 'Brand z','woocommerce' )
        )
    )
);

    // 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',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,true ),'type'          => 'text','label'         => __( 'Backorders text','description'   => __( 'Backorders text. Add a custom backorders text to be displayed when products are on backorders.','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,sanitize_text_field( $_POST['_backorder_text'][$i] ) );
}

add_filter( 'woocommerce_get_availability','custom_on_backorder_text',2 );
function custom_on_backorder_text( $availability,$product ) {
    $backorder_text = get_post_meta( $product->get_id(),true );

    if( $availability['class'] === 'available-on-backorder' && ! empty( $backorder_text ) )
        $availability['availability'] = $backorder_text;

    return $availability;
}

wb214427411 回答:Woocommerce“延期交货”文本值取决于实际产品交货时间

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2055.html

大家都在问