Sell in only some cities woocommerce - wordpress

In my account page,
I want to use a similar code as below to sell in a specific "cities" or "postcodes", the below code is used to sell in a specific "states":
/**
* Sell only in Alger & Zeralda
*/
function wc_sell_only_states( $states ) {
$states['DZ'] = array(
'ALG' => __( 'Alger', 'woocommerce' ),
'ZLD' => __( 'Zeralda', 'woocommerce' ),
);
return $states;
}
add_filter( 'woocommerce_states', 'wc_sell_only_states' );
What shall I use or modify?
I tried this, but it displays "Error code 500":
// define the woocommerce_countries_base_city callback
function filter_woocommerce_countries_base_city( $var ) {
// make filter magic happen here...
$var['DZ'] = array(
'ALG' => __( 'Alger', 'woocommerce' ),
'ZLD' => __( 'Zeralda', 'woocommerce' ),
);
return $var;
};
// add the filter
add_filter( 'woocommerce_countries_base_city', 'filter_woocommerce_countries_base_city', 10, 1 );
Thank you in advanced!

Try This it worked for me!!
add_filter( 'woocommerce_form_field_args', 'custom_form_field_args', 10, 3 );
function custom_form_field_args( $args, $key, $value ) {
if ( $args['id'] == 'billing_city' ) {
$args = array(
'label' => __( 'Town / City', 'woocommerce' ),
'required' => TRUE,
'clear' => TRUE,
'type' => 'select',
'options' => array(
'' => ('Select City' ),
'Bangalore' => ('Bangalore' ),
'Mysore' => ('Mysore' )
),
'class' => array( 'update_totals_on_change' )
);
} // elseif … and go on
return $args;
};

There is a simpler solution to this. The approach also depends on the some assumption which are listed below:
Assumption : If there is only one City which you want the City field to be set to then you can use jQuery. Using jQuery you will be setting the value of the field and make the fields disabled to avoid change in City field value.
A rough example will be :
jQuery('#billing_city').val('USA').attr('disabled','disabled');
You just need to add this line such that it is execute on Checkout page.

Related

Can't add a section to a Woocommerce Custom Setting Tab [duplicate]

I'm trying to add a custom settings tab to the WooCommerce settings screen. Basically I want to achieve a similar thing to the Products settings tab, with the subsections/subtabs:
I haven't been able to find any decent documentation on how to do this but I've been able to add a custom tab using this snippet:
class WC_Settings_Tab_Demo {
public static function init() {
add_filter( 'woocommerce_settings_tabs_array', __CLASS__ . '::add_settings_tab', 50 );
}
public static function add_settings_tab( $settings_tabs ) {
$settings_tabs['test'] = __( 'Settings Demo Tab', 'woocommerce-settings-tab-demo' );
return $settings_tabs;
}
}
WC_Settings_Tab_Demo::init();
Based on what I've dug up from various threads/tutorials, I've been trying to add the sections/subtabs to the new settings tab something like this:
// creating a new sub tab in API settings
add_filter( 'woocommerce_get_sections_test','add_subtab' );
function add_subtab( $sections ) {
$sections['custom_settings'] = __( 'Custom Settings', 'woocommerce-custom-settings-tab' );
$sections['more_settings'] = __( 'More Settings', 'woocommerce-custom-settings-tab' );
return $sections;
}
// adding settings (HTML Form)
add_filter( 'woocommerce_get_settings_test', 'add_subtab_settings', 10, 2 );
function add_subtab_settings( $settings, $current_section ) {
// $current_section = (isset($_GET['section']) && !empty($_GET['section']))? $_GET['section']:'';
if ( $current_section == 'custom_settings' ) {
$custom_settings = array();
$custom_settings[] = array( 'name' => __( 'Custom Settings', 'text-domain' ),
'type' => 'title',
'desc' => __( 'The following options are used to ...', 'text-domain' ),
'id' => 'custom_settings'
);
$custom_settings[] = array(
'name' => __( 'Field 1', 'text-domain' ),
'id' => 'field_one',
'type' => 'text',
'default' => get_option('field_one'),
);
$custom_settings[] = array( 'type' => 'sectionend', 'id' => 'test-options' );
return $custom_settings;
} else {
// If not, return the standard settings
return $settings;
}
}
I've been able to add new subsections to the Products tab using similar code to the above, but it isn't working for my new custom tab. Where am I going wrong here?
1) To add a setting tab with sections, you can firstly use the woocommerce_settings_tabs_array filter hook:
// Add the tab to the tabs array
function filter_woocommerce_settings_tabs_array( $settings_tabs ) {
$settings_tabs['my-custom-tab'] = __( 'My custom tab', 'woocommerce' );
return $settings_tabs;
}
add_filter( 'woocommerce_settings_tabs_array', 'filter_woocommerce_settings_tabs_array', 99 );
2) To add new sections to the page, you can use the woocommerce_sections_{$current_tab} composite hook where {$current_tab} need to be replaced by the key slug that is set in the first function:
// Add new sections to the page
function action_woocommerce_sections_my_custom_tab() {
global $current_section;
$tab_id = 'my-custom-tab';
// Must contain more than one section to display the links
// Make first element's key empty ('')
$sections = array(
'' => __( 'Overview', 'woocommerce' ),
'my-section-1' => __( 'My section 1', 'woocommerce' ),
'my-section-2' => __( 'My section 2', 'woocommerce' )
);
echo '<ul class="subsubsub">';
$array_keys = array_keys( $sections );
foreach ( $sections as $id => $label ) {
echo '<li>' . $label . ' ' . ( end( $array_keys ) == $id ? '' : '|' ) . ' </li>';
}
echo '</ul><br class="clear" />';
}
add_action( 'woocommerce_sections_my-custom-tab', 'action_woocommerce_sections_my_custom_tab', 10 );
3) For adding the settings, as well as for processing/saving, we will use a custom function, which we will then call:
// Settings function
function get_custom_settings() {
global $current_section;
$settings = array();
if ( $current_section == 'my-section-1' ) {
// My section 1
$settings = array(
// Title
array(
'title' => __( 'Your title 1', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_1'
),
// Text
array(
'title' => __( 'Your title 1.1', 'text-domain' ),
'type' => 'text',
'desc' => __( 'Your description 1.1', 'woocommerce' ),
'desc_tip' => true,
'id' => 'custom_settings_1_text',
'css' => 'min-width:300px;'
),
// Select
array(
'title' => __( 'Your title 1.2', 'woocommerce' ),
'desc' => __( 'Your description 1.2', 'woocommerce' ),
'id' => 'custom_settings_1_select',
'class' => 'wc-enhanced-select',
'css' => 'min-width:300px;',
'default' => 'aa',
'type' => 'select',
'options' => array(
'aa' => __( 'aa', 'woocommerce' ),
'bb' => __( 'bb', 'woocommerce' ),
'cc' => __( 'cc', 'woocommerce' ),
'dd' => __( 'dd', 'woocommerce' ),
),
'desc_tip' => true,
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_1'
),
);
} elseif ( $current_section == 'my-section-2' ) {
// My section 2
$settings = array(
// Title
array(
'title' => __( 'Your title 2', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_2'
),
// Text
array(
'title' => __( 'Your title 2.2', 'text-domain' ),
'type' => 'text',
'desc' => __( 'Your description 2.1', 'woocommerce' ),
'desc_tip' => true,
'id' => 'custom_settings_2_text',
'css' => 'min-width:300px;'
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_2'
),
);
} else {
// Overview
$settings = array(
// Title
array(
'title' => __( 'Overview', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_overview'
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_overview'
),
);
}
return $settings;
}
3.1) Add settings, via the woocommerce_settings_{$current_tab} composite hook:
// Add settings
function action_woocommerce_settings_my_custom_tab() {
// Call settings function
$settings = get_custom_settings();
WC_Admin_Settings::output_fields( $settings );
}
add_action( 'woocommerce_settings_my-custom-tab', 'action_woocommerce_settings_my_custom_tab', 10 );
3.2) Process/save the settings, via the woocommerce_settings_save_{$current_tab} composite hook:
// Process/save the settings
function action_woocommerce_settings_save_my_custom_tab() {
global $current_section;
$tab_id = 'my-custom-tab';
// Call settings function
$settings = get_custom_settings();
WC_Admin_Settings::save_fields( $settings );
if ( $current_section ) {
do_action( 'woocommerce_update_options_' . $tab_id . '_' . $current_section );
}
}
add_action( 'woocommerce_settings_save_my-custom-tab', 'action_woocommerce_settings_save_my_custom_tab', 10 );
Result:
Based on:
Implement a custom WooCommerce settings page, including page sections
woocommerce/includes/admin/settings/

Add a custom dropdown field on My account > edit account in WooCommerce

I am using the following code to display an additional input field on the edit account page of WooCommerce.
/**
* Step 1. Add your field - Age Range
*/
add_action( 'woocommerce_edit_account_form', 'misha_add_age_range_field_account_form' );
function misha_add_age_range_field_account_form() {
echo "<h4> Please fill in the following details to complete your profile for review </h4>";
woocommerce_form_field(
'certified_age_range',
array(
'type' => 'text',
'required' => true, // remember, this doesn't make the field required, just adds an "*"
'label' => 'Your Age',
'description' => '',
),
get_user_meta( get_current_user_id(), 'certified_age_range', true ) // get the data
);
}
/**
* Step 2. Save field value
*/
add_action( 'woocommerce_save_account_details', 'misha_save_age_range_account_details' );
function misha_save_age_range_account_details( $user_id ) {
update_user_meta( $user_id, 'certified_age_range', sanitize_text_field( $_POST['certified_age_range'] ) );
}
/**
* Step 3. Make it required
*/
add_filter('woocommerce_save_account_details_required_fields', 'misha_make_field_required');
function misha_make_age_range_field_required( $required_fields ){
$required_fields['certified_age_range'] = 'Age';
return $required_fields;
}
add_filter( 'woocommerce_customer_meta_fields', 'misha_admin_age_range_field' );
function misha_admin_age_range_field( $admin_fields ) {
$admin_fields['billing']['fields']['certified_age_range'] = array(
'label' => 'Age',
'description' => 'Get Certified Form Field',
);
return $admin_fields;
}
The code above works perfectly, and this is how the 'Age' field appears on the page:
But now I need to make this field into a dropdown one instead of a simple text field.
This said, I tried doing the above customisation with the following code but without the desired result. Any advice?
add_filter( 'woocommerce_save_account_details_required_fields' , 'custom_override_age_field' );
function custom_override_age_field( $account_fields ) {
$option_age = array(
'' => __( 'Select your Age Range' ),
'18-24' => '18-24',
'25-34' => '25-34',
'35-44' => '35-44',
'45-54' => '45-54',
'55-64' => '55-64',
'65+' => '65+',
);
$account_fields['account_first_name']['type'] = 'select';
$account_fields['account_first_name']['options'] = $option_age;
return $account_fields;
}
Your code says: Step 1. Add your field - in your attempt you are using the hook from step 3.. while step 3 indicates Make it required so that's your first mistake.
The bottom line is that in the first step you have to edit the woocommerce_form_field settings. In your code the type is 'text' and you have to change this to 'select'.
So you get:
/**
* Step 1. Add your field - Age Range
*/
function action_woocommerce_edit_account_form() {
echo "<h4> Please fill in the following details to complete your profile for review </h4>";
// Select field
woocommerce_form_field( 'certified_age_range', array(
'type' => 'select',
'class' => array( 'form-row-wide' ),
'label' => __( 'Your age', 'woocommerce' ),
'required' => true, // remember, this doesn't make the field required, just adds an "*"
'options' => array(
'' => __( 'Select your age range', 'woocommerce' ),
'18-24' => '18-24',
'25-34' => '25-34',
'35-44' => '35-44',
'45-54' => '45-54',
'55-64' => '55-64',
'65+' => '65+',
)
), get_user_meta( get_current_user_id(), 'certified_age_range', true ) );
}
add_action( 'woocommerce_edit_account_form', 'action_woocommerce_edit_account_form', 10, 0 );
/**
* Step 2. Make it required
*/
function filter_woocommerce_save_account_details_required_fields( $required_fields ) {
$required_fields['certified_age_range'] = __( 'Age', 'woocommerce' );
return $required_fields;
}
add_filter( 'woocommerce_save_account_details_required_fields', 'filter_woocommerce_save_account_details_required_fields', 10, 1 );
/**
* Step 3. Save field value
*/
function action_woocommerce_save_account_details( $user_id ) {
if ( isset( $_POST['certified_age_range'] ) ) {
// Update field
update_user_meta( $user_id, 'certified_age_range', sanitize_text_field( $_POST['certified_age_range'] ) );
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
/**
* Step 4. Get address fields for the edit user pages.
*/
function filter_woocommerce_customer_meta_fields( $args ) {
$args['billing']['fields']['certified_age_range'] = array(
'label' => __( 'Age', 'woocommerce' ),
'description' => __( 'Get Certified Form Field', 'woocommerce' ),
);
return $args;
}
add_filter( 'woocommerce_customer_meta_fields', 'filter_woocommerce_customer_meta_fields', 10, 1 );

how to disable display custom field in page template?

I created my custom fields through code and then I registered it with this code:
/**
* Content blocks for all pages
*/
$location_page = new ACF_Group_Location;
$location_page->add_rule( 'post_type', 'page');
$location_slots = new ACF_Group_Location;
$location_slots->add_rule( 'post_type', 'slots' );
$location_card = new ACF_Group_Location;
$location_card->add_rule( 'post_type', 'card' );
ACF_Group::create( 'content-blocks', __( 'Content blocks', 'dw' ) )
->add_location( $location_page )
->add_location( $location_slots )
->add_location( $location_card )
->set_attr( 'label_placement', 'top' )
->set_attr( 'hide_on_screen', array( 'the_content', 'comments' ) )
->add_fields( array(
array(
'label' => __( 'Content blocks', 'dw' ),
'name' => 'cb',
'type' => 'flexible_content',
'button_label' => __( 'Add content block', 'dw' ),
'layouts' => $cb
),
)
)->register();
add_rule() includes:
public function add_rule( $param, $value, $operator = '==' ) {
$this->rules[] = compact( 'param', 'value', 'operator' );
return $this;
}
and my question is what code do I need to add if I wanna disable them on my page template?
So you're using ACF programatically using this
instead of adding your custom fields to all pages and then try to disable it for a particular template just add your custom field group to the template you need.
<?php
add_action( 'register_acf_groups', 'my_theme_register_acf_groups' );
function mytheme_register_acf_groups() {
ACF_Group::create( 'my_group', 'My Group' )
->add_location_rule( 'post_type', 'page' )
->add_location_rule( 'page_template', 'template-contact.php' ) // <- see here
->set_attr( 'label_placement', 'left' )
->add_fields(array(
array(
'label' => 'Test Field',
'name' => 'test_field',
'type' => 'text'
),
))
->register();
}

Woocommerce postcode field placeholder

Adding placeholder to Postcode billing field in Woocommerce.
In core class-wc-countries.php
'postcode' => array(
'label' => __( 'Postcode / ZIP', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-last', 'address-field' ),
'clear' => true,
'validate' => array( 'postcode' ),
'autocomplete' => 'postal-code',
),
So there is no way to override it woocommerce_billing_fields filter. Why some fields have placeholder key but other not? Some fields have only labels, some only placeholders and some have both. I don't get logic behind this.
So my question is: how to add placeholder text to Postcode field. I can do it with Javascript, but that seams not natural. Also I can modify core, which is even worse then Javascript solution. What else can I do?
As suggested by #Ash Patel we can do it like this:
add_filter( 'woocommerce_checkout_fields', function($fields){
$fields['billing']['billing_postcode']['placeholder'] = __('My Post Code', 'woocommerce');
return $fields;
} );
And for shipping
add_filter( 'woocommerce_shipping_fields', function($fields){
$fields['shipping_postcode']['placeholder'] = __('My Post Code', 'woocommerce');
return $fields;
} );
add below code into your function.php for adding/updating postal code for checkout and edit screen.
//on edit address screen
function filter_woocommerce_billing_fields( $wooccm_billing_fields, $int ) {
$wooccm_billing_fields['billing_postcode']['placeholder'] = __('My Post Code', 'woocommerce');;
return $wooccm_billing_fields;
};
add_filter( 'woocommerce_billing_fields', 'filter_woocommerce_billing_fields', 10, 2 );
add_action('woocommerce_checkout_fields', 'update_placeholder_checkout_form_billing');
function update_placeholder_checkout_form_billing($wcCheckout_fields) {
$wcCheckout_fields['billing']['billing_postcode'] = array(
'label' => 'Postcode / ZIP',
'placeholder' => _x('My Post Code', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('checkout-billing-postal-code')
);
return $wcCheckout_fields;
}

How to get the values of Wordpress customize checkboxes

I can't figure out how to get the value - whether they are checked or not - from checkboxes in the WP customize manager.
This is the code in functions.php:
$wp_customize->add_setting('social_facebook', array(
'type' => 'option',
));
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'social_facebook',
array(
'label' => __( 'Facebook', 'theme_name' ),
'section' => 'social-icons',
'settings' => 'social_facebook',
'type' => 'checkbox',
)
)
);
And this is how I try to get the value:
<?php
$facebook = get_theme_mod('social_facebook');
if ($facebook != ''){?>
<style>
.facebook {display:inline!important;}
</style>
<?php }
?>
They values of the checkboxes are either "" (empty) or "1", so the system registers the checking of them. However, I don't know how to get the value through the get_theme_mod approach. Also, they don't have any name values, so I can't get the value through the usual way either.
$sticky_mod = get_theme_mod( 'wca_header_section_sticky' ) == '1' ? 'sticky' : '';
This is an example in my case - if that option is checked, it will echo a "sticky" class in my template.
if the setting 'my_theme_settings[social_facebook]' checkbox is unchecked:
<?php if( get_theme_mod( 'my_theme_settings[social_facebook]' ) == '') { ?>
//if setting is unchecked content here will be shown
<?php } // end if ?>
For full article see: http://themefoundation.com/wordpress-theme-customizer
Try use and customize this (tested, in functions.php:
function mytheme_customize_register( $wp_customize ){
$wp_customize->add_section(
// ID
'layout_section',
// Arguments array
array(
'title' => __( 'Layout', 'my_theme' ),
'capability' => 'edit_theme_options',
'description' => __( 'social needs ;)', 'my_theme' )
)
);
$wp_customize->add_setting(
// ID
'my_theme_settings[social_facebook]',
// Arguments array
array('type' => 'option')
);
$wp_customize->add_control(
// ID
'layout_control',
array(
'type' => 'checkbox',
'label' => __( 'Facebook', 'my_theme' ),
'section' => 'layout_section',
// This last one must match setting ID from above
'settings' => 'my_theme_settings[social_facebook]'
)
);
}
add_action( 'customize_register', 'mytheme_customize_register' );
to read in template
$my_theme_settings = get_option( 'my_theme_settings' );
echo $my_theme_settings['social_facebook'];
the problem is in the WP_Customize_Setting::value() it expect to return false
to uncheck the checkbox (or leave the checkbox unchecked) while some program will return '0' or ''.
In my case I have to extend the WP_Customize_Setting and override the value() method to force returning boolean.
<?php
class ForceBooleanSettings
extends WP_Customize_Setting {
public function value() {
return (bool) parent::value();
}
}
// Example on using this extend class
$customizer->add_setting(new ForceBooleanSettings(
$customizer,
'myuniquekey',
array(
'default' => false,
'transport' => 'refresh',
)));
?>
Here is my working solution:
function theme_name_custom_settings( $wp_customize ) {
$wp_customize->add_setting( 'social_facebook', array(
'default' => 1, // Set default value
'sanitize_callback' => 'esc_attr', // Sanitize input
)
);
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'social_facebook', // Setting ID
array(
'label' => __( 'Facebook', 'theme_name' ),
'section' => 'social_icons', // No hyphen
'settings' => 'social_facebook', // Setting ID
'type' => 'checkbox',
)
)
);
}
add_action( 'customize_register', 'theme_name_custom_settings' );
Then check it's value (0 or 1) like so:
if ( !get_theme_mod( 'social_facebook' ) ) { // false
return;
}
// or
if ( get_theme_mod( 'social_facebook' ) == 1 ) { // true
return;
}
<div class="facebook" <?php echo ( get_theme_mod( 'social_facebook' ) ) ? "style='display:none;'" : "" ?>>

Resources