duanbaque4230 2017-05-17 10:53
浏览 47

仅在条件下运行函数

i want to run this function only if the current logged in user is the post author

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');

this is what i've tried but it isn't working, and it ran anyway

what i'm trying to do is that when users post to wordpress using woocommerce from frontend, the posted post is added to wordpress in draft state, and admins just publish/approve the posts

what's happening right now is that in anyway if the admin clicked on publish the function runs and updates the post metas, i don't want to update the post metas unless if the admin who publish the post is the current owner/author of the post, and if the admin who approve/publish the post is not the post author, the post just get published without any metas updates

  • 写回答

1条回答 默认 最新

  • douwan7382 2017-05-17 12:18
    关注

    ok the solution was in the sequence of calling the action, and the save_post runs on updating the post anyway without applying any checks

    so this can be a solution

    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');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大