post_meta get_the_ID在wordpress中不起作用

问题描述 投票:0回答:2

命令get_post_meta(get_the_ID(),'add_price',true);在我的wordpress网站的此功能上不起作用:

function misha_recalculate_price( $cart_object ) {
    var_dump(get_post_type($post_ID));

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
            foreach ( $cart_object->get_cart() as $hash => $value ) {
                 $nowprice = $value['data']->get_price(); 
                 $addprice =  get_post_meta(get_the_ID(), 'add_price', true);
                 $newprice = $nowprice+$addprice ;
                 $value['data']->set_price( $newprice );         
            }           
}
add_action( 'woocommerce_before_calculate_totals', 'misha_recalculate_price' );
wordpress woocommerce
2个回答
1
投票

您可以这样尝试吗?

function misha_recalculate_price( $cart_object ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;

        foreach ( $cart_object->get_cart() as $hash => $value ) {
            $nowprice = $value['data']->get_price(); 
            $addprice =  get_post_meta($value['data']->get_id(), 'add_price', true);
            $newprice = $nowprice + $addprice;
            $value['data']->set_price( $newprice );         
        }
    }       
}
add_action( 'woocommerce_before_calculate_totals', 'misha_recalculate_price', 10, 1 );

0
投票
function misha_recalculate_price( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
            foreach ( $cart_object->get_cart() as $hash => $value ) {
                 $nowprice = $value['data']->get_price(); 
                $addprice = get_post_meta($value['data']->get_id(), 'cost_price', true);
                 $newprice = $nowprice+$addprice ;
                 $value['data']->set_price( $newprice );         
            }           
}
add_action( 'woocommerce_before_calculate_totals', 'misha_recalculate_price', 10, 1 );
© www.soinside.com 2019 - 2024. All rights reserved.