Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从管理产品数据元盒保存自定义字段数据_Php_Wordpress_Woocommerce_Custom Fields_Product - Fatal编程技术网

Php 从管理产品数据元盒保存自定义字段数据

Php 从管理产品数据元盒保存自定义字段数据,php,wordpress,woocommerce,custom-fields,product,Php,Wordpress,Woocommerce,Custom Fields,Product,在Woocommerce中,我为产品规格使用一些自定义字段,并将规格保存在post_meta中 我正在尝试做一个if循环,写在另一个产品规范的post\u meta中 我现在使用的代码是: add_action( 'woocommerce_product_options_general_product_data', 'BTW_field' ); function BTW_field() { woocommerce_wp_radio( array(

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

我正在尝试做一个if循环,写在另一个产品规范的
post\u 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\u save
函数,这样它将保存另一个
post\u 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\u meta
\u BTW
以及如何重写它,以便
$BTW2
也将在post meta中保存为
\u BTW\u NAME
更新:当您设置两个不同的值时,最好使用选择字段

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

代码如下:

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\u post\u meta()
获取两个产品
post\u meta
自定义字段值:


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

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

代码如下:

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\u post\u meta()
获取两个产品
post\u meta
自定义字段值: