Changing Prices – Nearly There!
-
I have got the price changing nearly working thanks to everyones help – there are still two issues though.
What I need to do is:
Have £20 added to the TOTAL cost of an item if the ‘New Logo Upload’ field is ‘yes’ – but is only needs to be added once, so if the product is £10, then one would be £30, but 5 would be £70 (£10 x 5 + £20)
At the moment, the code changes the cost of the product – but then the cart multiplies it again by the quantity so the price for 5 comes to £350 (ie: £70 * 5)
What I need to do is add the additional fee to the product total – not the product price.
The 2nd issue is – how do I get this to look at ALL the products in the cart, not just the one specified in the code?
The code I have is:
function calculate_cart_total( $cart_object ) { /* additional price that has to be added */ $additionalPrice = 20; $product_id = 245; 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 */ foreach ( $all_fields as $fields ) { /* Iterate through all the fields */ foreach ( $fields as $field ) { /* Check for your intended custom fields */ if( $field["name"] == "new_logo_upload" ) { /* Check for the value ( or it could be any condition logic ) */ if( $value['wccpf_new_logo_upload'] == "yes" ) { //change the price $quantity = floatval( $value['quantity'] ); $orgPrice = floatval( $value['data']->price ); $value['data']->price = ( ( $orgPrice * $quantity ) + $additionalPrice ); } } } } } } add_action( 'woocommerce_before_calculate_totals', 'calculate_cart_total', 1 );
Many thanks!
- The topic ‘Changing Prices – Nearly There!’ is closed to new replies.