Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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 避免丢失Woocommerce中管理产品自定义字段的值_Php_Wordpress_Woocommerce_Admin_Custom Fields - Fatal编程技术网

Php 避免丢失Woocommerce中管理产品自定义字段的值

Php 避免丢失Woocommerce中管理产品自定义字段的值,php,wordpress,woocommerce,admin,custom-fields,Php,Wordpress,Woocommerce,Admin,Custom Fields,在Woocommerce中,我在“常规设置”选项卡下的“管理产品”页面中添加了一些自定义字段,它可以正常工作。不过,我添加了一些第三方插件,每次处理订单时都会删除这些字段中保存的所有内容 “常规”选项卡上的默认字段以及其他第三方的字段不会被删除。我想知道是否有比我现在这样做更好的方法来创建字段和存储数据 这是我的密码: add_action( 'woocommerce_product_options_pricing', 'mpq_add_location_textbox_to_products'

在Woocommerce中,我在“常规设置”选项卡下的“管理产品”页面中添加了一些自定义字段,它可以正常工作。不过,我添加了一些第三方插件,每次处理订单时都会删除这些字段中保存的所有内容

“常规”选项卡上的默认字段以及其他第三方的字段不会被删除。我想知道是否有比我现在这样做更好的方法来创建字段和存储数据

这是我的密码:

add_action( 'woocommerce_product_options_pricing', 'mpq_add_location_textbox_to_products' );        

function mpq_add_location_textbox_to_products() {           
    if ( (has_term( 'workshops', 'product_cat' ) )){     
        woocommerce_wp_textarea_input( array( 
            'id' => 'mpq_location', 
            'class' => '', 
            'label' => 'Location:',
            'placeholder' => 'Enter Location'
            ) 
        );      
    }
}

add_action( 'save_post', 'mpq_save_location_textbox_to_post_meta' );

function mpq_save_location_textbox_to_post_meta( $product_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    if ( isset( $_POST['mpq_location'] ) ) {
            update_post_meta( $product_id, 'mpq_location', $_POST['mpq_location'] );
    } else delete_post_meta( $product_id, 'mpq_location' );
}

有一些错误和遗漏的东西…尝试以下方法可以解决您的问题:

add_action( 'woocommerce_product_options_pricing', 'add_location_product_custom_textaread_field' );
function add_location_product_custom_textaread_field() {
    global $post;

    $term = 'workshops'; // Product category term slug

    if ( has_term( $term, 'product_cat', $post->ID ) ){
        woocommerce_wp_textarea_input( array(
            'id' => '_mpq_location',
            'class' => '',
            'label' => 'Location:',
            'placeholder' => 'Enter Location'
        ) );

        echo '<input type="hidden" name="_mpq_location_nonce" value="' . wp_create_nonce() . '">';
    }
}

add_action( 'save_post_product', 'save_location_product_custom_textaread_field', 20, 1 );
function save_location_product_custom_textaread_field( $post_id ) {
    if ( ! isset( $_POST[ '_mpq_location_nonce' ] ) ) {
        return $post_id;
    }
    if ( ! wp_verify_nonce( $_POST[ '_mpq_location_nonce' ] ) ) {
        return $post_id;
    }
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }
    if ( ! current_user_can( 'edit_product', $post_id ) ) {
        return $post_id;
    }
    if ( isset( $_POST['_mpq_location'] ) ) {
        update_post_meta( $post_id, '_mpq_location', sanitize_textarea_field($_POST['_mpq_location']) );
    }
}
add_操作('woocommerce_product_options_pricing'、'add_location_product_custom_textaread_field');
函数添加\位置\产品\自定义\文本区域\字段(){
全球$员额;
$term='workshop';//产品类别术语slug
如果(有术语($term,$product\U cat',$post->ID)){
woocommerce\u wp\u文本区域\u输入(数组(
'id'=>'\u mpq\u位置',
“类”=>“”,
“标签”=>“位置:”,
'占位符'=>'输入位置'
) );
回声';
}
}
添加操作('save_post_product'、'save_location_product_custom_textaread_field',20,1);
函数保存\位置\产品\自定义\文本区域\字段($post\ id){
如果(!isset($\u POST['\u mpq\u location\u nonce'])){
返回$post_id;
}
如果(!wp\u verify\u nonce($\u POST['\u mpq\u location\u nonce'])){
返回$post_id;
}
if(已定义('DOING_AUTOSAVE')&&DOING_AUTOSAVE){
返回$post_id;
}
如果(!当前用户可以('编辑产品',$post\U id)){
返回$post_id;
}
如果(isset($_POST[''mpq_location'])){
更新发布元($发布id,'''.'mpq'.'位置',清理文本区域'.$字段($'.'发布[''mpq'.'位置]);
}
}
代码进入活动子主题(或活动主题)的function.php文件。测试和工作

我已将meat键更改为
\u mpq\u位置
,并删除了
else delete\u post\u meta()