Title: make wccpf_unique_key conditional
Last modified: August 31, 2016

---

# make wccpf_unique_key conditional

 *  Resolved [thomaswp](https://wordpress.org/support/users/thomaswp/)
 * (@thomaswp)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/make-wccpf_unique_key-conditional/)
 * Thnx for this plugin! I found a small bug, the function save_wccpf_data is always
   triggered, adding the unique key ($cart_item_data[‘wccpf_unique_key’] = $unique_cart_item_key;)
   should be skipped on products with no custom fields.
    Because now the key is 
   present on every product you add to cart, this breaks the update quantity if 
   same product is already in cart functionality.
 * this seems to do the trick, but not 100% sure:
 *     ```
       function save_wccpf_data( $cart_item_data, $product_id ) {
       		if( $product_id ) {
       			$val = "";
       			$wccpf_options = get_option( 'wccpf_options' );
       			$wccpf_options =  is_array( $wccpf_options ) ? $wccpf_options : array();
   
       			$fields_cloning = isset( $wccpf_options["fields_cloning"] ) ? $wccpf_options["fields_cloning"] : "no";
       			$all_fields = apply_filters( 'wcff/load/all_fields', $product_id, 'wccpf' );
   
       			if (count($all_fields ) > 0) {
       				$unique_cart_item_key = md5( microtime().rand() );
       				$cart_item_data['wccpf_unique_key'] = $unique_cart_item_key;
   
       				if( $fields_cloning == "no" ) {
       					foreach ( $all_fields as $fields ) {
       						foreach ( $fields as $field ) {
       							$is_multi_file = isset( $field["multi_file"] ) ? $field["multi_file"] : "no";
       							if( isset( $_REQUEST[ $field["name"] ] ) || isset( $_FILES[ $field["name"] ] ) ) {
       								if( $field["type"] != "checkbox" && $field["type"] != "file" ) {
       									$cart_item_data[ "wccpf_" . $field["name"] ] = $_REQUEST[ $field["name"] ];
       								} else if( $field["type"] == "checkbox" ) {
       									$cart_item_data[ "wccpf_" . $field["name"] ] = implode( ", ", $_REQUEST[ $field["name"] ] );
       								} else {
       									$res = array();
       									/* Handle the file upload */
       									if( $is_multi_file == "yes" ) {
       										$files = $_FILES[ $field["name"] ];
       										foreach ( $files['name'] as $key => $value ) {
       											if ( $files['name'][$key] ) {
       												$file = array(
       													'name'     => $files['name'][$key],
       													'type'     => $files['type'][$key],
       													'tmp_name' => $files['tmp_name'][$key],
       													'error'    => $files['error'][$key],
       													'size'     => $files['size'][$key]
       												);
       												$temp_res = apply_filters( 'wccpf/upload/type=file', $file );
       												if( isset( $temp_res['error'] ) ) {
       													$res = $temp_res;
       													break;
       												} else {
       													$res[] = $temp_res;
       												}
       											}
       										}
       									} else {
       										$res = apply_filters( 'wccpf/upload/type=file', $_FILES[ $field["name"] ] );
       									}
       									if( !isset( $res['error'] ) ) {
       										$cart_item_data[ "wccpf_" . $field["name"] ] = json_encode( $res );
       										do_action( 'wccpf/uploaded/file', $res );
       									} else {
       										wc_add_wp_error_notices( $field["message"], 'error' );
       									}
       								}
       							}
       						}
       					}
       				} else {
       					if( isset( $_REQUEST["quantity"] ) ) {
       						$pcount = intval( $_REQUEST["quantity"] );
       						foreach ( $all_fields as $fields ) {
       							foreach ( $fields as $field ) {
       								$is_multi_file = isset( $field["multi_file"] ) ? $field["multi_file"] : "no";
       								for( $i = 1; $i <= $pcount; $i++ ) {
       									if( isset( $_REQUEST[ $field["name"] . "_" . $i ] ) || isset( $_REQUEST[ $field["name"] . "_" . $i . "[]" ] ) || isset( $_FILES[ $field["name"] . "_" . $i ] ) ) {
       										if( $field["type"] != "checkbox" && $field["type"] != "file" ) {
       											$cart_item_data[ "wccpf_" . $field["name"] . "_" . $i ] = $_REQUEST[ $field["name"] . "_" . $i ];
       										} else if( $field["type"] == "checkbox" ) {
       											$cart_item_data[ "wccpf_" . $field["name"] . "_" . $i ] = implode( ", ", $_REQUEST[ $field["name"] . "_" . $i ] );
       										} else {
       											$res = array();
       											/* Handle the file upload */
       											if( $is_multi_file == "yes" ) {
       												$files = $_FILES[ $field["name"] . "_" . $i ];
       												foreach ( $files['name'] as $key => $value ) {
       													if ( $files['name'][$key] ) {
       														$file = array(
       																'name'     => $files['name'][$key],
       																'type'     => $files['type'][$key],
       																'tmp_name' => $files['tmp_name'][$key],
       																'error'    => $files['error'][$key],
       																'size'     => $files['size'][$key]
       														);
       														$temp_res = apply_filters( 'wccpf/upload/type=file', $file );
       														if( isset( $temp_res['error'] ) ) {
       															$res = $temp_res;
       															break;
       														} else {
       															$res[] = $temp_res;
       														}
       													}
       												}
       											} else {
       												$res = apply_filters( 'wccpf/upload/type=file', $_FILES[ $field["name"] . "_" . $i ] );
       											}
       											if( !isset( $res['error'] ) ) {
       												$cart_item_data[ "wccpf_" . $field["name"] . "_" . $i ] = json_encode( $res );
       												do_action( 'wccpf/uploaded/file', $res );
       											} else {
       												wc_add_wp_error_notices( $field["message"], 'error' );
       											}
       										}
       									}
       								}
       							}
       						}
       					}
       				}
       			}		
   
       		}
       		return $cart_item_data;
       	}
       ```
   
 * [https://wordpress.org/plugins/wc-fields-factory/](https://wordpress.org/plugins/wc-fields-factory/)

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Saravana Kumar K](https://wordpress.org/support/users/mycholan/)
 * (@mycholan)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/make-wccpf_unique_key-conditional/#post-7142783)
 * Hi, thanks for posting this, the behavior should be like you said, I update with
   my next release.
 *  Plugin Author [Saravana Kumar K](https://wordpress.org/support/users/mycholan/)
 * (@mycholan)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/make-wccpf_unique_key-conditional/#post-7142848)
 * Hi, the fix has been included with V1.2.9
 *  Thread Starter [thomaswp](https://wordpress.org/support/users/thomaswp/)
 * (@thomaswp)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/make-wccpf_unique_key-conditional/#post-7142894)
 * Thnx!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘make wccpf_unique_key conditional’ is closed to new replies.

 * ![](https://ps.w.org/wc-fields-factory/assets/icon-128x128.jpg?rev=2738843)
 * [WC Fields Factory](https://wordpress.org/plugins/wc-fields-factory/)
 * [Support Threads](https://wordpress.org/support/plugin/wc-fields-factory/)
 * [Active Topics](https://wordpress.org/support/plugin/wc-fields-factory/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wc-fields-factory/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wc-fields-factory/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [thomaswp](https://wordpress.org/support/users/thomaswp/)
 * Last activity: [10 years, 1 month ago](https://wordpress.org/support/topic/make-wccpf_unique_key-conditional/#post-7142894)
 * Status: resolved