I am having post code based minimum order amount for orders for my Woocommerce shop.
Also I am having postcode based custom error messages to appear once the user type postcode like how much he needs more to checkout.
So preventing the user to type invalid postcodes(which means the postcodes that we do not deliver), i am displaying drop down to pick his valid postcode in Checkout page.
Based on this thread Change postcode shipping field to a dropdown in Woocommerce, i could turn the postcode field to dropdown with the options.
But i am getting the error that the "Postcode is not valid" by Woocommerce when i select any option from the drop down. Also my custom messages also not displaying.
Below is the code I m using from the above thread.
add_filter( 'woocommerce_default_address_fields' , 'customize_postcode_fields' );
function customize_postcode_fields( $adresses_fields ) {
$adresses_fields['postcode']['type'] = 'select';
$adresses_fields['postcode']['options'] = array(
'' => __('Select your postcode', 'woocommerce'),
'option_1' => '000001',
'option_2' => '000002',
'option_3' => '000003'
);
return $adresses_fields;
}
Try to use the following instead:
add_filter( 'woocommerce_default_address_fields' , 'customize_postcode_fields' );
function customize_postcode_fields( $adresses_fields ) {
$adresses_fields['postcode']['type'] = 'select';
$adresses_fields['postcode']['input_class'] = array('state_select');
$adresses_fields['postcode']['options'] = array(
'' => __('Select your postcode', 'woocommerce'),
'000001' => '000001',
'000002' => '000002',
'000003' => '000003'
);
return $adresses_fields;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works on last WooCommerce version 4.5.1 under Storefront theme.
Related
we use the plugin
Shop Exporter Deluxe to output all orders in a CSV, this works quite well so far.
Now we have switched everything to multistores and receive the orders from the child shops, which we then want to export. Until then everything works. Now we have the product meta "WOONET_PARENT_ORDER_ORIGIN_TEXT" from the multishops in the database.
We want to add this value to the CSV.
We looked at the snippets from the plugin author
https://visser.com.au/documentation/store-exporter-deluxe/code-snippets/
and tried the following
function custom_woo_ce_extend_order_fields( $fields ) {
$fields[] = array(
'name' => 'get_store',
'label' => 'Multistore Shop',
'hover' => 'Get the Multistore Shopname'
);
return $fields;
}
add_filter( 'woo_ce_order_fields', 'custom_woo_ce_extend_order_fields' );
function custom_woo_ce_extend_order( $order, $order_id ) {
$order_get = wc_get_order($order);
$order->get_store .= $order_get->get_meta('WOONET_PARENT_ORDER_ORIGIN_TEXT');
return $order;
}
add_filter( 'woo_ce_order', 'custom_woo_ce_extend_order', 10, 2 );
The select field works so far, so the first function works.
However, no new value is added to the CSV
I thought this would be easy but I'm not getting the results I want. Basically I have 2 countries in woocommerce CA and US. I'm trying to remove one conditionally, and I can do that with the following code below. However, when I go from 2 countries to 1, the dropdown menu still appears. An odd thing I'm noticing with the below code as well, is that if I go into my Woocommerce settings, then the country that is removed with this code is also removed from the "Sell to specific countries" options.... not sure what's going on. Thanks in advance.
add_filter( 'woocommerce_countries', 'custom_woocommerce_countries_limit');
function custom_woocommerce_countries_limit( $countries ) {
/*
will place a conditional here if x then remove country
*/
unset($countries['CA']);
$countries = array(
'US' => __( 'United States', 'woocommerce' )
);
return $countries;
}
EDIT: Using this hook may be close to the answer, but when I use this one, The states don't turn into a dropdown...?
add_filter( 'woocommerce_countries_allowed_countries', 'custom_woocommerce_countries_limit');
You can use woocommerce_countries_allowed_countries filter hook, but you should need some additional code to force the state fields to be a state dropdown (select field):
add_filter( 'woocommerce_countries_allowed_countries', 'filter_allowed_countries_conditionally');
function filter_allowed_countries_conditionally( $countries ) {
// will place a conditional here if x then remove country
if( true ) {
$countries = array( 'US' => __( 'United States', 'woocommerce' ) );
} else {
$countries = array( 'CA' => __( 'Canada', 'woocommerce' ) );
}
return $countries;
}
// Force billing state field type to be a dropdown
add_filter( 'woocommerce_billing_fields', 'filter_billing_state_fields', 100, 1 );
function filter_billing_state_fields( $fields ) {
$fields['billing_state']['type'] = 'state';
return $fields;
}
// Force shipping state field type to be a dropdown
add_filter( 'woocommerce_shipping_fields', 'filter_shipping_state_fields', 100, 1 );
function filter_shipping_state_fields( $fields ) {
$fields['shipping_state']['type'] = 'state';
return $fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
The country of Vietnam in WooCommerce does not have defined states, so I
added some states to my checkout page.
This is my code:
add_filter( 'woocommerce_states', 'vietnam_cities_woocommerce' );
function vietnam_cities_woocommerce( $states ) {
$states['VN'] = array(
'HCM' => __('Hồ Chí Minh', 'woocommerce') ,
'HANOI' => __('Hà Nội', 'woocommerce') ,
'HAIPHONG' => __('Hải Phòng', 'woocommerce') ,
);
return $states;
}
It do work as I would like, but it is an optional field for Vietnam.
How to make this state field as required for Vietnam?
Any help is appreciated.
The following function will make for Vietnam the state field as a required in woocommerce:
add_filter( 'woocommerce_get_country_locale', 'custom_country_locale', 10, 1 );
function custom_default_address_fields( $locale ) {
$locale['VN']['state']['required'] = true;
return $locale;
}
Code goes in functions.php file of your active child theme (active theme). Tested and works.
Explanations: Each country have specific "locale" fields settings in WooCommerce. We are adding/altering the default WooCommerce country locale settings that are defined on WC_Countries get_country_locale() method
I am working on a WordPress eCommerce website, with WooCommerce being the chosen Shopping Platform.
I have created a Custom Field, within the WooCommerce Product Dashboard by inserting the following code into the functions.php file:
function product_custom_fields_add(){
echo '<div class="product_custom_field">';
// Minimum Required Custom Letters
woocommerce_wp_text_input(
array(
'id' => '_minimum_engrave_text_option',
'name' => '_minimum_engrave_text_option',
'desc' => __('set custom minimum Lettering text field', 'woocommerce'),
'label' => __('Minimum Letters', 'woocommerce'),
'desc_tip' => 'true'
)
);
echo '</div>';
}
add_action('woocommerce_product_options_advanced', 'product_custom_fields_add');
In order to save the values, I have entered the following code into the functions.php file:
// Save Minimum Required Custom Letters
function woocommerce_product_custom_fields_save1($post_id){
if ( ! empty( $_POST['_minimum_engrave_text_option'] ) )
update_post_meta($post_id, '_minimum_engrave_text_option', esc_attr( $_POST['_minimum_engrave_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save1' );
The above code works when a value is entered into the Custom Field. My problem is that I am unable to successfully save a Blank Value. Whenever I save a Product, the Custom Field either auto populates the Field with '1' or saves a previously entered number.
I tried applying the answer, from a similar question, but could not quote get it to work.
Does anyone know where I am going wrong here?
Try to replace empty() by isset() as a condition in the if statement of your hooked function:
// Save Minimum Required Custom Letters
function woocommerce_product_custom_fields_save1($post_id){
if ( isset( $_POST['_minimum_engrave_text_option'] ) )
update_post_meta($post_id, '_minimum_engrave_text_option', esc_attr( $_POST['_minimum_engrave_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save1' );
Now this will works
Is there a way/plugin in Wordpress WooCommerce to restrict a certain product from being DOWNLOADED in a certain region. Remember this is not suppose to be site wide, but on a product to product basis.
What I imagine is that on checkout if a product has download restrictions enabled a function pulls the current user's location (country) and compares it against an array of permitted countries for that product. If there is a match the check in proceeds if not, it returns a message informing the user that the product they requested is not available for download in their country. THE QUESTION IS, DOES SUCH A PLUGIN, FEATURE, FUNCTION or SNIPPET exist, if so where?
UPDATE: Seeing that there was no answer I have gone ahead and started creating something on my own. I have no previous PHP experience so please help me make this code concise. You can try it. IS THIS CORRECT?
UPDATE (SOLUTION): Woocommerce now has a built in functionality that checks user location and stores it for the shop owner to use in custom functions, go wild with it :)
The following code goes into your theme's functions.php file. It will add a "Region Settings" panel to your product page's add/edit page under the "General Tab". It has two options, "restriction type:" which can be set to "Allow" or "Deny" and the "Regions: " option where you specify the countries that will be affected. If a product's region settings are not set, it will allow everyone to access it.
/**
* Mazwi WooCommerce Region Control BETA
* ------------------------------------
*
*
*
* Execute code if the user's country (set for each product) is allowed
*
* Author: Taf Makura
* Thanks to Remi Corson's Tutorial
*/
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
// Display Fields
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
?>
<?php
// Select
woocommerce_wp_select(
array(
'id' => '_restriction-type',
'label' => __( 'Restriction type', 'woocommerce' ),
'options' => array(
'allow' => __( 'Allow', 'woocommerce' ),
'deny' => __( 'Deny', 'woocommerce' ),
)
)
);
// Create Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_regions',
'label' => __( 'Regions', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Please enter two letter country codes. Each country code should be followed by a coma Example: ZW, AU, ZA, US ', 'woocommerce' )
)
);
echo '</div>';
}
function woo_add_custom_general_fields_save( $post_id ){
// Select
$woocommerce_select = $_POST['_restriction-type'];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, '_restriction-type', esc_attr( $woocommerce_select ) );
// Textarea
$woocommerce_textarea = $_POST['_regions'];
if( !empty( $woocommerce_textarea ) )
update_post_meta( $post_id, '_regions', esc_html( $woocommerce_textarea ) );
}
The following code goes into the template .php file where the conditional execution is suppose to happen. I can imagine if you place the add to cart loop (add to cart button) here it will allow you to control which products can be bought in certain countries. On a product by product basis.
<?php global $woocommerce;
// Get restriction type (deny or allow) for current product
$restriction_type = get_post_meta( $post->ID, '_restriction-type', true );
// Get region(s) the above restriction type is applied to
$regions = get_post_meta( $post->ID, '_regions', true );
// Turns this string into an array.
$regions_array = (explode(',', str_replace('/\s+/','', $regions)));
// Get users current IP location from WooCommerce
$base_country = (..... YOU NEED TO GET THE USER LOCATION ISO COUNTRY CODE ......)
// If user's current IP location is either allowed, is not denied or is not set in the region settings = success
if( $restriction_type == 'allow' && in_array($base_country , $regions_array) || $restriction_type == 'deny' && !in_array($base_country , $regions_array) || $restriction_type == '' || $regions == '' ) {
if ($restriction_type == '' || $regions == '') {
// Code to execute on success if a product is not set (NOTE: It will not be restricted)
echo('This product\'s region control has not been set, you can set it in WP Admin');
}
// Code to execute on success if a products region settings are set to allow access
echo('YOU ARE IN');
} else {
// Code to execute when region is restricted
echo(' you are restricted,');
}
?>
I'm not sure if you saw/tried this, but according to http://docs.woothemes.com/document/configuring-woocommerce-settings/ you can do what you are asking for.
To configure your shop go to WooCommerce > Settings. Then browse through the content below to get more information on the WooCommerce Options.
Allowed Countries
Here you can select whether you want to sell/ship to too countries, or
a select few – useful if only trading within your own country for
instance. Customers outside your allowed countries will not be able to
checkout.
Specific Countries
Define the countries you’re willing to sell/ship to. You must set the
“Allowed Countries” option to "Specific Countries".