Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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 创建新产品时添加meta_密钥_Php_Sql_Wordpress_Post_Woocommerce - Fatal编程技术网

Php 创建新产品时添加meta_密钥

Php 创建新产品时添加meta_密钥,php,sql,wordpress,post,woocommerce,Php,Sql,Wordpress,Post,Woocommerce,我创建此函数是为了在数据库中使用metakey生成一个新位置:“_price”,因为当我从前端创建一个新产品时,它是空的。代码只在我更新帖子时有效,而不是在我第一次创建产品时有效。 你能帮助我吗?谢谢 function wpufe_update_post_price( $post_id ) { if ( 'product' != $_POST['post_type'] ) return; $regular_price = get_post_meta( $post_id, '_regular_p

我创建此函数是为了在数据库中使用metakey生成一个新位置:“_price”,因为当我从前端创建一个新产品时,它是空的。代码只在我更新帖子时有效,而不是在我第一次创建产品时有效。 你能帮助我吗?谢谢

function wpufe_update_post_price( $post_id ) {
if ( 'product' != $_POST['post_type'] )
return;

$regular_price = get_post_meta( $post_id, '_regular_price', true );
$sale_price = get_post_meta( $post_id, '_price', true );

update_post_meta( $post_id, '_price', $regular_price );

if ( !empty( $sale_price ) ) {

    update_post_meta( $post_id, '_price', $sale_price );

}

}
add_action('save_post_product', 'wpufe_update_post_price', 10, 1);
 add_action('wp_insert_post', 'wpufe_update_post_price', 10, 1);