/** * @deprecated */ function woocommerce_array_overlay($a1, $a2) { return wc_array_overlay($a1, $a2); }
/** * Apply locale and get address fields. * @param mixed $country (default: '') * @param string $type (default: 'billing_') * @return array */ public function get_address_fields($country = '', $type = 'billing_') { if (!$country) { $country = $this->get_base_country(); } $fields = $this->get_default_address_fields(); $locale = $this->get_country_locale(); if (isset($locale[$country])) { $fields = wc_array_overlay($fields, $locale[$country]); } // Prepend field keys $address_fields = array(); foreach ($fields as $key => $value) { $keys = array_keys($fields); $address_fields[$type . $key] = $value; // Add email and phone after company or last if ($type == 'billing_' && ('company' === $key || !array_key_exists('company', $fields) && $key === end($keys))) { $address_fields['billing_email'] = array('label' => __('Email Address', 'woocommerce'), 'required' => true, 'type' => 'email', 'class' => array('form-row-first'), 'validate' => array('email')); $address_fields['billing_phone'] = array('label' => __('Phone', 'woocommerce'), 'required' => true, 'type' => 'tel', 'class' => array('form-row-last'), 'clear' => true, 'validate' => array('phone')); } } $address_fields = apply_filters('woocommerce_' . $type . 'fields', $address_fields, $country); return $address_fields; }
/** * Merge two arrays. * * @param array $a1 * @param array $a2 * @return array */ function wc_array_overlay($a1, $a2) { foreach ($a1 as $k => $v) { if (!array_key_exists($k, $a2)) { continue; } if (is_array($v) && is_array($a2[$k])) { $a1[$k] = wc_array_overlay($v, $a2[$k]); } else { $a1[$k] = $a2[$k]; } } return $a1; }
/** * Apply locale and get address fields * @param mixed $country * @param string $type (default: 'billing_') * @return array */ public function get_address_fields($country = '', $type = 'billing_') { if (!$country) { $country = $this->get_base_country(); } $fields = $this->get_default_address_fields(); $locale = $this->get_country_locale(); if (isset($locale[$country])) { $fields = wc_array_overlay($fields, $locale[$country]); } // Prepend field keys $address_fields = array(); foreach ($fields as $key => $value) { $address_fields[$type . $key] = $value; } // Billing/Shipping Specific if ($type == 'billing_') { $address_fields['billing_email'] = array('label' => __('Email Address', 'woocommerce'), 'required' => true, 'class' => array('form-row-first'), 'validate' => array('email')); $address_fields['billing_phone'] = array('label' => __('Phone (Use this format in order to get SMS notifications: 27XXXXXXXX)', 'woocommerce'), 'required' => true, 'class' => array('form-row-last'), 'clear' => true, 'validate' => array('phone')); } $address_fields = apply_filters('woocommerce_' . $type . 'fields', $address_fields, $country); return $address_fields; }
/** * Test wc_array_overlay(). * * @since 2.2 */ public function test_wc_array_overlay() { $a1 = array('apple' => 'banana', 'pear' => 'grape', 'vegetables' => array('cucumber' => 'asparagus')); $a2 = array('strawberry' => 'orange', 'apple' => 'kiwi', 'vegetables' => array('cucumber' => 'peas')); $overlayed = array('apple' => 'kiwi', 'pear' => 'grape', 'vegetables' => array('cucumber' => 'peas')); $this->assertEquals($overlayed, wc_array_overlay($a1, $a2)); }
/** * Apply locale and get address fields * * @access public * @param mixed $country * @param string $type (default: 'billing_') * @return array */ public function get_address_fields($country, $type = 'billing_') { if (!$country) { $country = $this->get_base_country(); } $fields = $this->get_default_address_fields(); $locale = $this->get_country_locale(); if (isset($locale[$country])) { $fields = wc_array_overlay($fields, $locale[$country]); // If default country has postcode_before_city switch the fields round. // This is only done at this point, not if country changes on checkout. if (isset($locale[$country]['postcode_before_city'])) { if (isset($fields['postcode'])) { $fields['postcode']['class'] = array('form-row-wide', 'address-field'); $switch_fields = array(); foreach ($fields as $key => $value) { if ($key == 'city') { // Place postcode before city $switch_fields['postcode'] = ''; } $switch_fields[$key] = $value; } $fields = $switch_fields; } } } // Prepend field keys $address_fields = array(); foreach ($fields as $key => $value) { $address_fields[$type . $key] = $value; } // Billing/Shipping Specific if ($type == 'billing_') { $address_fields['billing_email'] = array('label' => __('Email Address', 'woocommerce'), 'required' => true, 'class' => array('form-row-first'), 'validate' => array('email')); $address_fields['billing_phone'] = array('label' => __('Phone', 'woocommerce'), 'required' => true, 'class' => array('form-row-last'), 'clear' => true, 'validate' => array('phone')); } $address_fields = apply_filters('woocommerce_' . $type . 'fields', $address_fields, $country); // Return return $address_fields; }
function init_address_fields($b_country = '', $s_country = '', $show_country = true) { $this->billing_fields = apply_filters('woocommerce_admin_billing_fields', array('first_name' => array('label' => __('First Name', 'woocommerce'), 'show' => false), 'last_name' => array('label' => __('Last Name', 'woocommerce'), 'show' => false), 'company' => array('label' => __('Company', 'woocommerce'), 'show' => false), 'address_1' => array('label' => __('Address 1', 'woocommerce'), 'show' => false), 'address_2' => array('label' => __('Address 2', 'woocommerce'), 'show' => false), 'city' => array('label' => __('City', 'woocommerce'), 'show' => false), 'postcode' => array('label' => __('Postcode', 'woocommerce'), 'show' => false), 'country' => array('label' => __('Country', 'woocommerce'), 'show' => false, 'class' => 'js_field-country select short', 'type' => 'select', 'options' => array('' => __('Select a country…', 'woocommerce')) + WC()->countries->get_allowed_countries()), 'state' => array('label' => __('State/County', 'woocommerce'), 'class' => 'js_field-state select short', 'show' => false), 'email' => array('label' => __('Email', 'woocommerce')), 'phone' => array('label' => __('Phone', 'woocommerce')))); $this->shipping_fields = apply_filters('woocommerce_admin_shipping_fields', array('first_name' => array('label' => __('First Name', 'woocommerce'), 'show' => false), 'last_name' => array('label' => __('Last Name', 'woocommerce'), 'show' => false), 'company' => array('label' => __('Company', 'woocommerce'), 'show' => false), 'address_1' => array('label' => __('Address 1', 'woocommerce'), 'show' => false), 'address_2' => array('label' => __('Address 2', 'woocommerce'), 'show' => false), 'city' => array('label' => __('City', 'woocommerce'), 'show' => false), 'postcode' => array('label' => __('Postcode', 'woocommerce'), 'show' => false), 'country' => array('label' => __('Country', 'woocommerce'), 'show' => false, 'type' => 'select', 'class' => 'js_field-country select short', 'options' => array('' => __('Select a country…', 'woocommerce')) + WC()->countries->get_shipping_countries()), 'state' => array('label' => __('State/County', 'woocommerce'), 'class' => 'js_field-state select short', 'show' => false))); if (!empty($b_country) || !empty($s_country)) { $countries = new WC_Countries(); $locale = $countries->get_country_locale(); $state_arr = WC()->countries->get_allowed_country_states(); } if (!empty($b_country)) { if (isset($locale[$b_country])) { $this->billing_fields = wc_array_overlay($this->billing_fields, $locale[$b_country]); // If default country has postcode_before_city switch the fields round. // This is only done at this point, not if country changes on checkout. if (isset($locale[$b_country]['postcode_before_city'])) { if (isset($this->billing_fields['postcode'])) { $this->billing_fields['postcode']['class'] = array('form-row-wide', 'address-field'); $switch_fields = array(); foreach ($this->billing_fields as $key => $value) { if ($key == 'city') { // Place postcode before city $switch_fields['postcode'] = ''; } $switch_fields[$key] = $value; } $this->billing_fields = $switch_fields; } } if (isset($state_arr[$b_country]) && !empty($state_arr[$b_country])) { $this->billing_fields['state']['type'] = 'select'; $this->billing_fields['state']['class'] = array('form-row-left', 'address-field', 'chosen_select'); $this->billing_fields['state']['options'] = $state_arr[$b_country]; } } } if (!empty($s_country)) { if (isset($locale[$s_country])) { $this->shipping_fields = wc_array_overlay($this->shipping_fields, $locale[$s_country]); // If default country has postcode_before_city switch the fields round. // This is only done at this point, not if country changes on checkout. if (isset($locale[$s_country]['postcode_before_city'])) { if (isset($this->shipping_fields['postcode'])) { $this->shipping_fields['postcode']['class'] = array('form-row-wide', 'address-field'); $switch_fields = array(); foreach ($this->shipping_fields as $key => $value) { if ($key == 'city') { // Place postcode before city $switch_fields['postcode'] = ''; } $switch_fields[$key] = $value; } $this->shipping_fields = $switch_fields; } } if (isset($state_arr[$s_country]) && !empty($state_arr[$s_country])) { $this->shipping_fields['state']['type'] = 'select'; $this->shipping_fields['state']['class'] = array('form-row-left', 'address-field', 'chosen_select'); $this->shipping_fields['state']['options'] = $state_arr[$b_country]; } } } }
/** * Apply locale and get address fields. * @param mixed $country (default: '') * @param string $type (default: 'billing_') * @return array */ public function get_address_fields($country = '', $type = 'billing_') { if (!$country) { $country = $this->get_base_country(); } $fields = $this->get_default_address_fields(); $locale = $this->get_country_locale(); if (isset($locale[$country])) { $fields = wc_array_overlay($fields, $locale[$country]); } // Prepend field keys $address_fields = array(); foreach ($fields as $key => $value) { $keys = array_keys($fields); $address_fields[$type . $key] = $value; } // Add email and phone fields. if ('billing_' === $type) { $address_fields['billing_phone'] = array('label' => __('Phone', 'woocommerce'), 'required' => true, 'type' => 'tel', 'class' => array('form-row-first'), 'validate' => array('phone'), 'autocomplete' => 'tel'); $address_fields['billing_email'] = array('label' => __('Email address', 'woocommerce'), 'required' => true, 'clear' => true, 'type' => 'email', 'class' => array('form-row-last'), 'validate' => array('email'), 'autocomplete' => 'no' === get_option('woocommerce_registration_generate_username') ? 'email' : 'email username'); } /** * Important note on this filter: Changes to address fields can and will be overridden by * the woocommerce_default_address_fields. The locales/default locales apply on top based * on country selection. If you want to change things like the required status of an * address field, filter woocommerce_default_address_fields instead. */ $address_fields = apply_filters('woocommerce_' . $type . 'fields', $address_fields, $country); return $address_fields; }