• 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

    https://www.ads-software.com/plugins/wc-fields-factory/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Saravana Kumar K

    (@mycholan)

    The code you are using is outdated, here you can find the updated one.

    Thread Starter carver1g

    (@carver1g)

    Implemented the updated code and still overriding price wasn’t working. I did some investigating with Firefox’s Developer Tools and found the following:

    <table class=”wccpf_fields_table ” cellspacing=”0″>
    <tbody>
    <tr>
    <td class=”wccpf_label”><label for=”add_logo_1″>Add Logo? <span>*</span></label></td>
    <td class=”wccpf_value”>
    <ul class=”wccpf-field-layout-horizontal”>

    • <label><input class=”wccpf-field” name=”add_logo_1″ value=”yes” wccpf-type=”radio” wccpf-pattern=”mandatory” wccpf-mandatory=”yes” type=”radio”> Yes</label>
    • <label><input class=”wccpf-field” name=”add_logo_1″ value=”no” wccpf-type=”radio” wccpf-pattern=”mandatory” wccpf-mandatory=”yes” type=”radio”> No</label>
    • <span class=”wccpf-validation-message wccpf-is-valid-1″>Please select ‘yes’ or ‘no'</span>
      </td>
      </tr>
      </tbody>
      </table>

      The field name I created is ‘add_logo’ but as you can see the html shows the field name as ‘add_logo_1. I went back and updated the overriding code to read ‘wccpf_add_logo_1’ and everything worked fine (cart item price relfected the add logo price).

      Only problem now is the ‘add_logo’ field is required as a yes or no answer but client side validation showed no message for the field being empty. I disabled client side validation and purposely left the ‘add_logo’ field empty and click the add to cart button. I did receive an error message using the standard validation but also received the following notice;

      Notice: Undefined index: add_logo_1 in C:\server\site\wordpress\wp-content\plugins\wc-fields-factory\classes\wcff-product-form.php on line 313.

      Don’t quite understand if I create a field name as ‘add_logo’ the html shows the field name as ‘add_logo_1.

      Any help will be greatly appreciated.
      Thanks again

    Thread Starter carver1g

    (@carver1g)

    OK, I figured out why the html shows the field name as ‘add_log_1’ and not the actual field I created (add_logo) is due to the fact I have ‘fields cloning’ enabled. But am still getting the ‘Undefined index’ notice upon validation which doesn’t allow me to use ‘client side validation’ with the ‘add_logo_1’ field.

    Appreciate the help

    Plugin Author Saravana Kumar K

    (@mycholan)

    Hi carver1g, thanks for posting this, that client side validation issue for Radio & Check field, actually that was an open bug.

    Anyway I got the issues, will release the fix for this as well as for some other issues that raised by you earlier ASAP.

    Regards
    Sark

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