skip to Main Content

The commands get_post_meta(get_the_ID(), ‘add_price’, true); are not working at this function of my wordpress site:

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

2

Answers


  1. Chosen as BEST ANSWER
    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 );
    

  2. can you give it a try this way?

    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 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search