【问题标题】:Saving custom field data from Admin WooCommerce product data metabox从管理员 WooCommerce 产品数据元框保存自定义字段数据
【发布时间】:2018-02-07 18:40:29
【问题描述】:

在 Woocommerce 中,我使用一些自定义字段作为产品规格,并将规格保存在 post_meta 中。

我正在尝试创建一个 if 循环来写下 post_meta 另一个产品规范。

我现在使用的代码是:

add_action( 'woocommerce_product_options_general_product_data', 'BTW_field' );
function BTW_field() {
    woocommerce_wp_radio( 
        array( 
            'id'          => '_BTW', 
            'default'     => '21% BTW', 
            'required' => true,
            'options' => array(
                'Prijs is incl. 21% BTW'   => '21% BTW',
                'Margeproduct'   => 'Marge Product',
            )
        )
    );
}

add_action( 'woocommerce_process_product_meta', 'BTW_save' );
function BTW_save( $post_id ){  
    $BTW = $_POST['_BTW'];
    if( !empty( $BTW ) )
        update_post_meta( $post_id, '_BTW', esc_attr( $BTW ) );
}

现在我尝试重写BTW_save 函数,以便保存另一个post_meta

function BTW_save( $post_id ){  
$BTW = $_POST['_BTW'];
    if( !empty( $BTW ) ){
        update_post_meta( $post_id, '_BTW', esc_attr( $BTW ) );
    }
    if ($BTW == "Margeproduct (vrijgesteld van BTW)"){
        $BTW2 = "Margeproduct*"
    } else {
        $BTW2 = "21%"
    }
    update_post_meta( $post_id, '_BTW_NAME', esc_attr( $BTW2 ) );
}

我不知道如何检查$BTW 是否等于post_meta _BTW 以及如何重写它,所以$BTW2 也会在帖子元中保存为_BTW_NAME

【问题讨论】:

    标签: php wordpress woocommerce custom-fields product


    【解决方案1】:

    更新:当您设置 2 个不同的值时,最好改用选择字段。

    我还对您的代码进行了一些更改,涉及正确的变量命名和字段键命名(您应该能够轻松地重命名它们,记住建议使用小写和下划线)。

    代码如下:

    add_action( 'woocommerce_product_options_general_product_data', 'add_btw_field' );
    function add_btw_field() {
        global $post;
    
        // Get the selected value
        $value = get_post_meta( $post->ID, '_btw', true );
        if( empty( $value ) ) $value = 'btw'; // Default value
    
        woocommerce_wp_select( array(
            'id'       => 'btw_select',
            'label'    => __( 'BTW-prijsopties', 'woocommerce' ),
            'options'  => array(
                'btw'  => __( '21% BTW', 'woocommerce' ),
                'marge'   => __( 'Marge Product', 'woocommerce' ),
            ),
            'value'   => $value, // Displaying the selected value
        ) );
    }
    
    add_action( 'woocommerce_process_product_meta', 'save_btw_field' );
    function save_btw_field( $post_id ){
    
        if( empty( $_POST['btw_select'] ) ) return; // exit (in case of)
    
        update_post_meta( $post_id, '_btw', esc_attr( $_POST['btw_select'] ) );
    
        if ( $_POST['btw_select'] == 'btw' )
            $label = __( 'BTW 21%', 'woocommerce' );
        else
            $label = __( 'Margeproduct (vrijgesteld van BTW)', 'woocommerce' );
    
        update_post_meta( $post_id, '_btw_label', esc_attr( $label ) );
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    经过测试并且有效。你会得到类似的东西:

    默认情况下在创建或更新产品时,两个自定义字段都将保存为“btw”选项作为产品元数据...

    您将能够使用get_post_meta() 获得两个产品post_meta 自定义字段值:

    // Set HERE the product ID (or get it dynamically)
    $product_id = 37;
    $btw = get_post_meta( $product_id, '_btw', true ); // Values can be 'yes' or 'no'
    $btw_label = get_post_meta( $product_id, '_btw_label', true );
    
    // Output (testing):
    echo $btw_label;
    

    【讨论】:

      猜你喜欢
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 2017-09-27
      • 2022-01-19
      相关资源
      最近更新 更多