我目前有一个带有WooCommerce订阅插件的WooCommerce网站。我试图在通过仪表板创建新订阅订单时添加post meta,但在订阅仍处于挂起状态时,很难保存meta。
仅当满足以下两个条件时,我才需要保存该值:
只有当帖子是通过WordPress仪表板创建的,而不是通过前端的购物车系统创建时,才应首先创建帖子只有当meta不存在时,才需要第二个meta,只有对于新的帖子,即使帖子仍然悬而未决,也需要这样做,这是我目前拥有的:
add_action( \'added_post_meta\', \'shop_sync_on_shop_subscription_save\', 10, 4 );
add_action( \'updated_post_meta\', \'shop_sync_on_shop_subscription_save\', 10, 4 );
function shop_sync_on_shop_subscription_save( $meta_id, $post_id, $meta_key, $meta_value ) {
if ( $meta_key == \'_edit_lock\' ) { // we\'ve been editing the post
if ( get_post_type( $post_id ) == \'shop_subscription\' ) { // we\'ve been editing a subscription
$remaining_issues = get_post_meta($post_id, \'_remaining_issues\');
if ($remaining_issues == "") { // we only want to update to the default 6 if this has not already been set
update_post_meta($post_id, \'_remaining_issues\', \'6\');
}
}
}
}