/** * Update a customer meta. * * Implement your own system by hooking into 'wpsc_update_customer_meta'. * * @access public * @since 3.8.9 * @param string $key Meta key * @param mixed $value Meta value * @param string|int $id Customer ID. Optional. Defaults to current customer. * @return boolean|WP_Error True if successful, false if not successful, WP_Error * if there are any errors. */ function wpsc_update_customer_meta($key, $value, $id = false) { if (!$id) { $id = wpsc_get_current_customer_id(); } $result = apply_filters('wpsc_update_customer_meta', null, $key, $value, $id); if ($result) { return $result; } return wpsc_update_visitor_meta($id, $key, $value); }
/** * make sure the shipping country meta is stored as individual values not as an array * * @access private * @since 3.8.14 */ function _wpsc_fix_shipping_country_meta() { global $wpdb; $sql = 'SELECT * from ' . $wpdb->wpsc_visitormeta . ' WHERE meta_key = "shippingcountry"'; $metas = $wpdb->get_results($sql, OBJECT); foreach ($metas as $meta) { $meta_value = maybe_unserialize($meta->meta_value); if (is_array($meta_value)) { wpsc_update_visitor_meta($meta->wpsc_visitor_id, 'shippingregion', $meta_value[1]); wpsc_update_visitor_meta($meta->wpsc_visitor_id, 'shippingcountry', $meta_value[0]); } } }
/** * Update the customer mata values that are passed to the application from the checkout form POST * * With the submit checkout we should get an array of all the checkout values. These values should already * be stored as customer meta, bet there are cases where the submit processing may arrive before or in parallel * with the request to update meta. There is also value in cehcking to be sure the meta stored is what is coming * with the POST as it preserves non-js compatibility and being able to use the submit action as an API * * @since 3.8.14.1 * * @access private * * @param array $checkout_post_data * * @return none */ function _wpsc_checkout_customer_meta_update($checkout_post_data) { global $wpdb; if (empty($checkout_post_data) || !is_array($checkout_post_data)) { return; } $id = wpsc_get_current_customer_id(); $form_sql = 'SELECT * FROM `' . WPSC_TABLE_CHECKOUT_FORMS . '` WHERE `active` = "1" ORDER BY `checkout_set`, `checkout_order`;'; $form_data = $wpdb->get_results($form_sql, ARRAY_A); foreach ($form_data as $index => $form_field) { if (isset($checkout_post_data[$form_field['id']])) { $meta_key = $form_field['unique_name']; $meta_value = $checkout_post_data[$form_field['id']]; switch ($form_field['type']) { case 'delivery_country': if (is_array($meta_value)) { if (isset($meta_value[0])) { wpsc_update_visitor_meta($id, 'shippingcountry', $meta_value[0]); } if (isset($meta_value[1])) { wpsc_update_visitor_meta($id, 'shippingregion', $meta_value[1]); } } else { // array had only country, update the country wpsc_update_visitor_meta($id, 'shippingcountry', $meta_value); } break; case 'country': if (is_array($meta_value) && count($meta_value) == 2) { wpsc_update_visitor_meta($id, 'billingcountry', $meta_value[0]); wpsc_update_visitor_meta($id, 'billingregion', $meta_value[1]); } else { if (is_array($meta_value)) { $meta_value = $meta_value[0]; } wpsc_update_visitor_meta($id, 'billingcountry', $meta_value); } break; default: wpsc_update_visitor_meta($id, $meta_key, $meta_value); break; } } } }
/** * Update any values dependant on billing region * * @since 3.8.14 * * @access private * @param mixed $meta_value Optional. Metadata value. * @param string $meta_key Metadata name. * @param int $visitor_id visitor ID * @return none */ function _wpsc_updated_visitor_meta_billingregion($meta_value, $meta_key, $visitor_id) { if (!empty($meta_value)) { $billingstate = wpsc_get_state_by_id($meta_value, 'name'); } else { $billingstate = ''; } wpsc_update_visitor_meta($visitor_id, 'billingstate', $billingstate); }
/** * when visitor meta is updated we need to check if the shipping same as billing * option is selected. If so we need to update the corresponding meta value. * * @since 3.8.14 * @access private * @param $meta_value varies value being stored * @param $meta_key string name of the attribute being stored * @param $visitor_id int id of the visitor to which the attribute applies * @return n/a */ function _wpsc_vistor_shipping_same_as_billing_meta_update($meta_value, $meta_key, $visitor_id) { // remove the action so we don't cause an infinite loop remove_action('wpsc_updated_visitor_meta', '_wpsc_vistor_shipping_same_as_billing_meta_update', _WPSC_USER_META_HOOK_PRIORITY); // if the shipping same as billing option is being checked then copy meta from billing to shipping if ($meta_key == 'shippingSameBilling') { $meta_value = _wpsc_make_value_into_bool($meta_value); if ($meta_value) { $checkout_names = wpsc_checkout_unique_names(); foreach ($checkout_names as $meta_key) { $meta_key_starts_with_billing = strpos($meta_key, 'billing', 0) === 0; if ($meta_key_starts_with_billing) { $other_meta_key_name = 'shipping' . substr($meta_key, strlen('billing')); if (in_array($other_meta_key_name, $checkout_names)) { $billing_meta_value = wpsc_get_customer_meta($meta_key); wpsc_update_visitor_meta($visitor_id, $other_meta_key_name, $billing_meta_value); } } } } } else { $shipping_same_as_billing = wpsc_get_visitor_meta($visitor_id, 'shippingSameBilling', true); if ($shipping_same_as_billing) { $meta_key_starts_with_billing = strpos($meta_key, 'billing', 0) === 0; $meta_key_starts_with_shipping = strpos($meta_key, 'shipping', 0) === 0; if ($meta_key_starts_with_billing) { $checkout_names = wpsc_checkout_unique_names(); $other_meta_key_name = 'shipping' . substr($meta_key, strlen('billing')); if (in_array($other_meta_key_name, $checkout_names)) { wpsc_update_visitor_meta($visitor_id, $other_meta_key_name, $meta_value); } } elseif ($meta_key_starts_with_shipping) { $checkout_names = wpsc_checkout_unique_names(); $other_meta_key_name = 'billing' . substr($meta_key, strlen('shipping')); if (in_array($other_meta_key_name, $checkout_names)) { wpsc_update_visitor_meta($visitor_id, $other_meta_key_name, $meta_value); } } } } // restore the action we removed at the start add_action('wpsc_updated_visitor_meta', '_wpsc_vistor_shipping_same_as_billing_meta_update', _WPSC_USER_META_HOOK_PRIORITY, 3); }
function _wpsc_visitor_location_is_changing($meta_value, $meta_key, $visitor_id) { $location_change_updated = false; $what_about_the_visitor_location_changed = wpsc_get_visitor_meta($visitor_id, 'location_attributes_changed', true); if (!$what_about_the_visitor_location_changed) { $what_about_the_visitor_location_changed = array(); } if (!array_key_exists($meta_key, $what_about_the_visitor_location_changed)) { $what_about_the_visitor_location_changed[$meta_key] = $meta_value; wpsc_update_visitor_meta($visitor_id, 'location_attributes_changed', $what_about_the_visitor_location_changed); $location_change_updated = true; } return $location_change_updated; }
/** * Get a deprecated customer meta value that mirrors what was once "checkout_details". * * @since 3.8.14 * @param string|int $id Customer ID. Optional. Defaults to current customer * @return array checkout details array */ function _wpsc_update_deprecated_visitor_meta_checkout_details($meta_data_in_old_format, $key = 'checkout_details', $id = null) { global $wpdb; if (!$id) { $id = wpsc_get_current_customer_id(); } $form_sql = 'SELECT * FROM `' . WPSC_TABLE_CHECKOUT_FORMS . '` WHERE `active` = "1" ORDER BY `checkout_set`, `checkout_order`;'; $form_data = $wpdb->get_results($form_sql, ARRAY_A); foreach ($form_data as $index => $form_field) { if (isset($meta_data_in_old_format[$form_field['id']])) { $meta_key = $form_field['unique_name']; $meta_value = $meta_data_in_old_format[$form_field['id']]; switch ($form_field['type']) { case 'delivery_country': if (is_array($meta_value) && count($meta_value) == 2) { wpsc_update_visitor_meta($id, 'shippingcountry', $meta_value[0]); wpsc_update_visitor_meta($id, 'shippingregion', $meta_value[1]); } else { if (is_array($meta_value)) { $meta_value = $meta_value[0]; } wpsc_update_visitor_meta($id, 'shippingcountry', $meta_value); wpsc_update_visitor_meta($id, 'shippingregion', ''); } break; case 'country': if (is_array($meta_value) && count($meta_value) == 2) { wpsc_update_visitor_meta($id, 'billingcountry', $meta_value[0]); wpsc_update_visitor_meta($id, 'billingregion', $meta_value[1]); } else { if (is_array($meta_value)) { $meta_value = $meta_value[0]; } wpsc_update_visitor_meta($id, 'billingcountry', $meta_value); wpsc_update_visitor_meta($id, 'billingregion', ''); } break; default: wpsc_update_visitor_meta($id, $meta_key, $meta_value); break; } } } $deprecated_meta_value = wpsc_get_visitor_meta($id, $key, true); if (!empty($deprecated_meta_value)) { wpsc_delete_visitor_meta($id, $key); } return $meta_data_in_old_format; }
/** * Get visitor attribute * * @param $attribute attribute name * @param $value attribute value * @return this * @since 3.8.14 */ function set($attribute, $value) { $property_name = '_' . $attribute; $this->{$property_name} = $value; if (in_array($attribute, self::$visitor_table_attribute_list)) { // test if change of the attribute is permitted if (self::$visitor_table_attribute_list($attribute)) { wpsc_update_visitor($this->_id, array($attribute => $value)); } } else { wpsc_update_visitor_meta($this->_id, $attribute, $value); return $this; } }