/** * On the checkout page create a hidden elements holding current customer meta values * * This let's the wp-e-commerce javascript process any dependency rules even if the store has configured * the checkout forms so that some fields are hidden. The most important of these fields are the * country, region and state fields. But it's just as easy to include all of them and not worry about * what various parts of WPeC, themes or plugs may be doing. * * @since 3.8.14 * * @access private */ function _wpsc_customer_meta_into_checkout_page() { $checkout_metas = _wpsc_get_checkout_meta(); foreach ($checkout_metas as $key => $value) { ?> <input class="wpsc-meta-value" type="hidden" value="<?php echo esc_attr($value); ?> " data-wpsc-meta-key="<?php echo esc_attr($key); ?> " /> <?php } }
/** * Update customer information using information supplied by shopper on WPeC pages * * @since 3.8.14 * * @global $_REQUEST['meta_data'] array of key value pairs that the user has changed, key is meta item name, value is new value * * @return JSON encoded response array with results * * $RESPONSE['request'] : array containing the original AJAX $_REQUEST that was sent to * the server, use to match up asynchronous AJAX transactions, or * to see original rquiest paramters * * $RESPONSE['customer_meta'] : array of key value pairs containing updated meta values. The * specific value changed is not included. If there isn't any updated * customer meta, other than the original meta changed, this array element * may not be present, or may be present and empty * * $response['checkout_info'] : array of updated checkout information, array key is the HTML element ID * where the information is presented on the checkout form. If there isn't * any updated checkout information this array element may not be present, * or may be present and empty * * */ function wpsc_customer_updated_data_ajax() { $success = true; // we will echo back the request in the (likely async) response so that the client knows // which transaction the response matches $response = array('request' => $_REQUEST); // update can be a single key/value pair or an array of key value pairs if (!empty($_REQUEST['meta_data'])) { $customer_meta = isset($_REQUEST['meta_data']) ? $_REQUEST['meta_data'] : array(); } elseif (!empty($_REQUEST['meta_key']) && isset($_REQUEST['meta_value'])) { $customer_meta = array($_REQUEST['meta_key'] => $_REQUEST['meta_value']); } else { _wpsc_doing_it_wrong(__FUNCTION__, __('missing meta key or meta array', 'wpsc'), '3.8.14'); $customer_meta = array(); } // We will want to know which interface elements have changed as a result of this meta update, // capture the current state of the elements $checkout_info_before_updates = _wpsc_get_checkout_info(); // We will want to know which, if any, checkout meta changes as a result of hooks and filters // that may fire as we update each meta item $all_checkout_meta_before_updates = _wpsc_get_checkout_meta(); if (!empty($customer_meta)) { foreach ($customer_meta as $meta_key => $meta_value) { // this will echo back any fields to the requester. It's a // means for the requester to maintain some state during // asynchronous requests if (!empty($meta_key)) { $updated = wpsc_update_customer_meta($meta_key, $meta_value); $success = $success & $updated; } } // loop through a second time so that all of the meta has been set, tht way if there are // dependencies in response calculation foreach ($customer_meta as $meta_key => $meta_value) { $response = apply_filters('wpsc_customer_meta_response_' . $meta_key, $response, $meta_key, $meta_value); } if ($success) { $response['type'] = 'success'; $response['error'] = ''; } else { $response['type'] = 'error'; $response['error'] = __('meta values may not have been updated', 'wpsc'); } } else { $response['type'] = 'error'; $response['error'] = __('invalid parameters, meta array or meta key value pair required', 'wpsc'); } // Let's see what the current state of the customer meta set is after we applied the requested updates $all_checkout_meta_after_updates = _wpsc_get_checkout_meta(); foreach ($all_checkout_meta_after_updates as $current_meta_key => $current_meta_value) { // if the meta key and value are the same as what was sent in the request we don't need to // send them back because the client already knows about this. // // But we have to check just in case a data rule or a plugin that used our hooks made some adjustments if (isset($all_checkout_meta_before_updates[$current_meta_key]) && $all_checkout_meta_before_updates[$current_meta_key] == $current_meta_value) { // new value s the same as the old value, why send it? unset($all_checkout_meta_after_updates[$current_meta_key]); unset($all_checkout_meta_before_updates[$current_meta_key]); continue; } // if the meta value we are considering sending back is one of the values the client gave, we don't send it // because the client already knows the meta value and it is probably already visible in the user interface if (isset($customer_meta[$current_meta_key]) && $customer_meta[$current_meta_key] == $current_meta_value) { // new value s the same as the old value, why send it? unset($all_checkout_meta_after_updates[$current_meta_key]); continue; } } // Any checkout meta that has changed as a result of the requeeted updates remains // in our array, add it to the response $response['customer_meta'] = $all_checkout_meta_after_updates; // Get the changed checkout information and if something has changed add it to the repsonse $new_checkout_info = _wpsc_remove_unchanged_checkout_info($checkout_info_before_updates, _wpsc_get_checkout_info()); if (!empty($new_checkout_info)) { $response['checkout_info'] = $new_checkout_info; } else { if (isset($response['checkout_info'])) { unset($response['checkout_info']); } } // do the shipping quotes need to be recalcualted? $response['needs_shipping_recalc'] = wpsc_cart_need_to_recompute_shipping_quotes(); wp_send_json_success($response); }