Woocommerce: set a custom location (geolocation) for the whole site - woocommerce

In Woocommerce: I found this code to set a custom country selected in the Country field of the Checkout Billing form, which works fine:
/**
* Change default_checkout_billing_country
*/
add_filter( 'default_checkout_billing_country', 'change_default_checkout_billing_country' );
function change_default_checkout_billing_country() {
return 'XX'; // country code
}
But this only affect the form. I need to set the geolocation for the whole site. I need this because I need the prices to be shown with/without taxes depending on the country.
I know that this is managed for woocommerce geolocation. But I need this for developer purposes.
Important: I need the filter to be fired on the 'init' and not in the checkout page.
Update: I tried with this, but not working:
/**
* Change default_checkout_country
*/
add_filter( 'default_checkout_country', 'change_default_checkout_country' );
function change_default_checkout_country() {
return 'XX'; // country code
}

Related

Exclude Backorder Products from WooCommerce Composite Options

I'm using WooCommerce Composite Products to create customer configurable packages from existing items in our Woo shop. These packages need to consist of In Stock products only, ignoring anything that is set to backorder.
For "Composite Options" I am using product categories (we have too many products to add individually), however doing it this way adds all products that are currently on backorder and set to "Allow, but notify customer".
Ideally I need a solution that allows me to use product categories which ignores any products that are set to backorder.
I tried to edit an existing snippet which would create a filter for backorder products but was not able to make any difference to the front end visible products, I'm sure more needs to be done to make it work. This is the original snippet I edited:
add_filter( 'woocommerce_composite_component_options_query_args_current', 'sw_cp_exclude_out_of_stock_options' );
function sw_cp_exclude_out_of_stock_options( $args ) {
$args[ 'exclude_out_of_stock' ] = true;
return $args;
}
I'm pretty sure the following is the basis of the code needed to figure out how to exclude back order products from component options (taken from Composite Products Plugin Editor):
woocommerce composite products > includes > data > class-wc-product-composite-data-store-cpt.php Line 1040 + 1041:
`// See 'WC_CP_Component::exclude_out_of_stock_options'.
'exclude_out_of_stock' => false`
woocommerce composite products > includes > class-wc-cp-component.php Line 785 - 789:
` /**
* Controls whether out of stock component options should be hidden.
*
* #since 8.0.3
*
* #return boolean
*/
public function exclude_out_of_stock_options() {
return apply_filters( 'woocommerce_component_options_exclude_out_of_stock', 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ), $this );
}`
Can anyone help me with the automated solution to this problem?

wordpress shipping counrty to be set automatically based on maxmind geolocation

Im trying to set the shipping country automatically based on customer location.. any clues?
add_filter( 'default_checkout_country', 'change_default_checkout_country' );
function change_default_checkout_country() {
return "AE";
}

How do I add product attributes to the product page in WooCommerce?

I'm using WooCommerce and Elementor Pro. Is there any way to add specific product attributes (e.g. gross and net weight) below the Add to cart button?
It seems like an obvious thing but I haven't found options or snippets for it.
First you add attributes to product. Then you use this snippet in functions.php:
// maybe remove tab with attributes
add_filter( 'woocommerce_product_tabs', 'remove_info', 100, 1 );
function remove_info( $tabs ) {
unset($tabs['additional_information']);
return $tabs;
}
// show attributes
add_action( 'woocommerce_single_product_summary', 'summary_attributes', 35 );
function summary_attributes() {
global $product;
if ( $product->has_attributes() ) {
wc_display_product_attributes( $product );
}
}
Setting Up WooCommerce Attributes
go to Products > Attributes
Add the attribute name
Slug will automatically be created, and we can leave the rest of these options untouched.
Click “Add attribute” button and your attribute will be added.
Configure The Attributes
After creating attribute, now we can add different variation on your product. Click on the Configure Terms option.
Enter the variation
Add some short description
Click on the add new tab
To understand it in a better way you can follow this tutorial

How can I add a default billing title to WooCommerce?

I'm currently looking for a hook, to add a default billing title to the billing_title select to the WooCommerce checkout and change-address setting at the MyAccount page.
It should be something like Please select salutation and should be a required field. This field comes from the WooCommerce Germanized plugin and is a required field in Germany. Because of this there is no documentation about this (sadly).
This is my solution:
/**
* Add billing title default selection value
*/
add_filter( 'woocommerce_gzd_title_options', 'add_new_billing_title', 10, 1 );
function add_new_billing_title( $titles ) {
array_unshift( $titles, 'Bitte auswählen' );
return $titles;
}
I'm using array_unshift to add the default Please select value in German as first element in the select.

WooCommerce Url rewrites

I've got the same problem described in this post:
[WordPress URL rewrite for WooCommerce attributes, except that I need to filter by attribute not only inside a category.
Unfortunately, I cannot post a comment before reaching a higher reputation, so I'm creating a new question.
I defined a manufacturer attribute and if I want to browse all products from a certain manufacturer, I can use a url like www.example.com/shop/?filter_manufacturer=230, where 230 is the attribute ID.
I tried adding a endpoint, like suggested in the post linked above above, but I cannot get the rewrites working; for example, if I try to open www.example.com/shop/manufacturer/manufacturer_name I get a 404 error.
It's not clear to me if I should change anything in the permalink settings in Wordpress and, if yes, how.
I've always flushed the rewrite rules after every edit, BTW.
The missing link between your question regarding WooCommerce attributes and the linked answer is that product attributes are merely taxonomies with a 'pa_' appended to their name.
In your case the taxonomy is called "pa_manufacturer". WooCommerce sets these up by default to have no query var attached.
So in lieu of filtering query_vars we are going to target when WooCommerce registers that particular taxonomy. I've also modified to remove anonymous functions.
In my example I am using "color", so adjust to "manufacturer". I was able to then go to a URL of http://example.com/shop/color/black and see all the black products. Note that this doesn't get you a term archive where /shop/color will list all the colors. That is a different question and a lot more work.
I didn't test the activation part, so if you get 404s after activating you can just delete the whole activation function and simply go to Settings>Permalinks and save the permalinks again.
/**
* Plugin Name: Add an WooCommerce attribute endpoint to the URLs
* Plugin URI: http://stackoverflow.com/q/28460538/383847
* Credit to: http://stackoverflow.com/a/24331768/1287812
*/
function so_28460538_add_rewrite_endpoint(){
add_rewrite_endpoint( 'color', EP_ALL );
}
add_action( 'init', 'so_28460538_add_rewrite_endpoint' );
function so_28460538_attribute_args( $args ){
$args['query_var'] = 'color';
return $args;
}
add_filter( 'woocommerce_taxonomy_args_pa_color', 'so_28460538_attribute_args' );
/**
* Refresh permalinks on plugin activation
* Source: http://wordpress.stackexchange.com/a/108517/12615
*/
function WCM_Setup_Demo_on_activation(){
if ( ! current_user_can( 'activate_plugins' ) )
return;
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "activate-plugin_{$plugin}" );
add_rewrite_endpoint( 'color', EP_ALL ); #source: http://wordpress.stackexchange.com/a/118694/12615
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'WCM_Setup_Demo_on_activation' );
EDIT
Adding some screenshots of my settings in case it will help determine why you are getting 404s:
Here are my permalinks settings:
and here is the WooCommerce setting for determining the product archive page:
And finally, here is the result of visting:
http://local.wordpress.dev/shop/color/black/
Where shop is the pretty permalink for the product archive page set above. All items have a 'black' color attribute.
I had a similar problem and was able to solve it like this:
function add_model_taxonomy_args($args) {
$args['query_var'] = 'filter_model';
return $args;
}
add_filter('woocommerce_taxonomy_args_pa_model', 'add_model_taxonomy_args' );
function custom_rewrite_rules() {
add_rewrite_tag('%filter_model%', '([a-zA-Z0-9-]+)');
add_rewrite_rule('^c/phone-cases/(.+?)/?$', 'index.php?product_cat=phone-cases&filter_model=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_rules', 10, 2);
Sample Url: c/phone-cases/iphone-8-plus/
Resulting Rewrite: index.php?product_cat=phone-cases&filter_model=iphone-8-plus

Resources