• Resolved seank123

    (@seank123)


    Just noticed the following function that I use to add a fee to the product price when a field is used has stopped working – I think after WooCommerce updated to v3.

    
    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
                            $orgPrice = floatval( $value['data']->price );
                            $value['data']->price = (  $orgPrice + $additionalPrice );
                        }
                    }
    }
    
    add_action( 'woocommerce_before_calculate_totals', 'calculate_cart_total_name', 1 );
    
    

    It has worked perfectly up until now but no longer adds to the product price.

    However, if I change:

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

    to

    $cart_object->cart_contents_total += $additionalPrice;

    then the additional £5 is added to the cart total – so it looks like something is wrong with the $value[‘data’]->price = ( $orgPrice + $additionalPrice ); line – but I can’t see why?

    Thank you

Viewing 1 replies (of 1 total)
  • Thread Starter seank123

    (@seank123)

    Fixed it!

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

    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 );
Viewing 1 replies (of 1 total)
  • The topic ‘Change product price in cart stopped working’ is closed to new replies.