簡體   English   中英

僅在有條件的情況下運行功能

[英]run function only if condition

我僅在當前登錄用戶為帖子作者時才要運行此功能

global  $user_ID;
$admin_user = get_user_by( 'id', $user_ID );
$selected   = empty( $post->ID ) ? $user_ID : $post->post_author;

function return_custom_price( $post ) {
global $post;
$new_total = get_post_meta( $post->ID, '_total_egp', true);
$price = get_post_meta($post->ID, '_regular_price', true);
//$sale_price = get_post_meta($post->ID, '_sale_price', true);
$now_price = get_post_meta($post->ID, '_price', true);
$woo_usd_rate = get_post_meta( $post->ID, '_usd_rate', true);
$woo_price_usd = get_post_meta( $post->ID, '_price_usd', true);
$cog = $woo_price_usd * $woo_usd_rate;

if($user_ID == $selected ){

update_post_meta($post->ID, '_regular_price', $new_total);
update_post_meta($post->ID, '_sale_price', '');
update_post_meta($post->ID, '_price', $new_total);
update_post_meta( $post->ID, '_wc_cog_cost', $cog );

}
}

add_action('save_post', 'return_custom_price');

這是我嘗試過的方法,但是沒有用,無論如何它都運行了

我想做的是,當用戶使用woocommerce從前端發布到wordpress時,發布的帖子以草稿狀態添加到wordpress中,而管理員只是發布/批准帖子

現在發生的事情是,無論如何,如果管理員單擊了“發布”功能,它將運行並更新帖子的元數據,除非發布帖子的管理員是帖子的當前所有者/作者,否則我不想更新帖子的元數據。 ,並且如果批准/發布該帖子的管理員不是該帖子的作者,則該帖子將在不進行任何元更新的情況下被發布

好了,解決方案是按照調用操作的順序進行,並且save_post會在不應用任何檢查的情況下更新帖子

所以這可以解決

function return_custom_price( $post ) {
global  $post;
$market = get_post_meta( $post->ID, 'market', true);
if($market == "" ){
    return;
}
$new_total = get_post_meta( $post->ID, '_total_egp', true);
$price = get_post_meta($post->ID, '_regular_price', true);
//$sale_price = get_post_meta($post->ID, '_sale_price', true);
$now_price = get_post_meta($post->ID, '_price', true);
$woo_usd_rate = get_post_meta( $post->ID, '_usd_rate', true);
$woo_price_usd = get_post_meta( $post->ID, '_price_usd', true);
$cog = $woo_price_usd * $woo_usd_rate;

update_post_meta($post->ID, '_regular_price', $new_total);
update_post_meta($post->ID, '_sale_price', '');
update_post_meta($post->ID, '_price', $new_total);
update_post_meta( $post->ID, '_wc_cog_cost', $cog );
}   
add_action('save_post', 'return_custom_price');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM