• After updating to woocommrce 3.0 the overriding price function no longer works. This what I have in my functions.php file:

    function calculate_cart_total( $cart_object ) {
    /* additional price that has to be added */
    $additionalPrice = 3;
    foreach ( $cart_object->cart_contents as $key => $value ) {
    /* Check for the value ( or it could be any condition logic ) */
    /* Each custom field’s key would be prefixed with wccpf_ */
    if( isset( $value[ “wccpf_logo_1” ] ) && ( $value[ “wccpf_logo_1” ] == “rose”) ) {
    // change the price
    $orgPrice = floatval( $value[‘data’]->price );
    $value[‘data’]->price = ( $orgPrice + $additionalPrice );
    }
    }
    }
    add_action( ‘woocommerce_before_calculate_totals’, ‘calculate_cart_total’, 99 );

    Any help will be greatly appreciated.
    thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Try replacing

    $value[‘data’]->price = ( $orgPrice + $additionalPrice );

    with

    $value[‘data’]->set_price($orgPrice+$additionalPrice);

    Worked for me!

    Thread Starter carver1g

    (@carver1g)

    @seank123

    Tried your suggestion but all I get is a white screen.

    I do appreciate your reply.

    This is the full code that I’m using:

    
    function calculate_cart_total_name( $cart_object ) {
         
        /* additional price that has to be added */
        $additionalPrice = 5;
    
        
        foreach ( $cart_object->cart_contents as $key => $value ) {
            /* This will bring all the custom field objects that belongs to this product */
            $all_fields = apply_filters( 'wccpf/load/all_fields', $product_id );
            /* Iterate through all the field groups */
                  
                        /* Check for the value ( or it could be any condition logic ) */
    
                        if( $value['wccpf_player_name'] != "" ) {
                            //change the price
                            $quantity = floatval( $value['quantity'] );
                            $orgPrice = floatval( $value['data']->price );
    
                     $value['data']->set_price($orgPrice+$additionalPrice);
    
                        
                        }
                    }
    }
    
    add_action( 'woocommerce_before_calculate_totals', 'calculate_cart_total_name', 1 );
    

    Hopefully that might be of help to you!

    Thread Starter carver1g

    (@carver1g)

    I’ll give it a try and see what happens. Will let you know.
    Thanks

    Thread Starter carver1g

    (@carver1g)

    seank123,

    It’s possible I got a white screen at first trying your suggestion because I was missing “$all_fields = apply_filters( ‘wccpf/load/all_fields’, $product_id );” in the original code. I added the “$all_fields…” and your original suggestion and now the code works!

    I found these notices in the debug.log:

    PHP Notice: Undefined variable: product_id
    PHP Notice: price was called incorrectly

    Had to add a couple of things to this code to avoid notices in the debug log. Here is my modified code along with my annotations:

    unction calculate_cart_total( $cart_object ) {
     
        $additionalPrice = 3;
    
        $product_id = get_the_id();  //this corrected the undefined variable notice
         
        foreach ($cart_object->cart_contents as $key => $value) { 
    
         $all_fields = apply_filters( 'wccpf/load/all_fields', $product_id ); //missing in my original code
                      
            if( isset( $value["wccpf_logo_1"] ) && ( $value["wccpf_logo_1"] == "rose"||$value["wccpf_logo_1"] == "football"||$value["wccpf_logo_1"] == "baseball"||$value["wccpf_logo_1"] == "basketball")){
    
             $orgPrice = floatval($value['data']->get_price()); //this corrected the price called incorrectly notice
    
             $value['data']->set_price($orgPrice+$additionalPrice); //thanks to you
          }               
       }
    }
    add_action( 'woocommerce_before_calculate_totals', 'calculate_cart_total', 99 );

    I hand carve logos on cedar boxes I sell, that’s why the “wccpf_logo_1”.

    Been pulling what little hair I have left out for the past month trying to get this code to work again. Don’t know how you figured that one out but thanks to you it works. Well done!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Overriding price’ is closed to new replies.