Overriding Price
-
For my cedar boxes I have 3 fields. A name field, an Add Logo field which is yes or no, and a select field where the customer can pick the logo to carve on the box. I am using the following code:
function calculate_cart_total( $cart_object ) {
/* additional price that has to be added */
$additionalPrice = 3;
$product_id = 1021;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 ((array) $all_fields as $fields ) {
/* Iterate through all the fields */
foreach ((array) $fields as $field ) {
/* Check for your intended custom fields */
if( $field[‘name’] == ‘add_logo’ ) {
/* Check for the value ( or it could be any condition logic ) */
$fvalue = WC()->session->get( $value[‘wccpf_unique_key’].$field[‘name’] );
if( $fvalue == ‘yes’ ) {
//change the price
$quantity = floatval( $value[‘quantity’] );
$orgPrice = floatval( $value[‘data’]->price );
$value[‘data’]->price = ( ( $orgPrice + $additionalPrice ) * $quantity );
}
}
}
}
}
}
add_action( ‘woocommerce_before_calculate_totals’, ‘calculate_cart_total’, 99 );I had to use (array) for ‘foreach’ due to the fact I was getting the following warning:
Warning: Invalid argument supplied for foreach() in C:\server\site\wordpress\wp-content\themes\storefront-child\functions.php on line 910
The code doesn’t work for it doesn’t add $3.00 for a logo to the price in the cart. I’ve checked other plugins to see if there is a conflict and can’t find any.
Any help will be greatly appreciated.
Thanks
- The topic ‘Overriding Price’ is closed to new replies.