Hide variations that don't match - wordpress

So I'm currently working on a Woocommerce store that has lots of variations.
What I'm basically looking for is for the next variation to refresh based on what was chosen on the variation chosen before that.
For example, if you go on http://freelance.tstwebdesign.co.uk/platino/product/plain-platinum-court/,
I want to choose a selection from "Width" which should then refresh "Depth" showing only the depths available that match with that width.
How can this be done?

It's default function for not many variations. For many variations you can do it with function
// Woo Commerce – Make Variations only appear when available
function custom_wc_ajax_variation_threshold( $qty, $product ) {
return 500;
}
add_filter( ‘woocommerce_ajax_variation_threshold’, ‘custom_wc_ajax_variation_threshold’, 10, 2 );

Related

WooCommerce - Variant Product Price Filter not firing

Really struggling with this one and have a sneaky suspicion that the WooCommerce filter may have been removed. I have to tweak variation prices based on a condition that worked at about version 4.4 of Woo.
The code no longer runs so for now I am just trying to break it. Below is the code I am running.
function _custom_price_varaints( $price, $product ) {
die('123');
// -- My Logic here
}
add_filter('woocommerce_product_variation_get_regular_price', '_custom_price_varaints', 10, 2 );
add_filter('woocommerce_product_variation_get_price', '_custom_price_varaints', 10, 2 );
I expect on the product category front end template ( and shop top level ) that when it loads a variant product on the site, it will just break. This never happens. If I change the filter to woocommerce_product_get_price then it has the desired effect ( i.e. breaks on a non variant product ). It's like that filter is never being run and if I am not using the correct filter which one should I be using?

WooCommerce | Variable Product | Assign same image to Attribute X (out of Y) per value

In WooCommerce variable product, there are attributes and attribute values, which create variations.
E.g. :
Attribute: 1 (Size) | Values: XS/S/M/L/XL
Attribute: 2 (Style/Color) | Values: Red/Green/Blue/Black/...
Attribute: 3 (Fabric) | Cotton/Polyester/Some other stuff...
Let's assume you have 1 picture per style value.
Let's also assume you have 500 total variations (due to triple attributes, which stacks).
Is there a simple way to associate a single picture per "ATTRIB2 VALUE" so you don't have to manually edit every single picture from variations?
100 products with 500 single variations seems like a lot of monkey work, there has to be a better way.
Edit:
1 picture for ALL blue variations;
1 picture for all red variations...
etc...
 
Side-question: Is there a way to use the bulk "Set Regular Price" to apply to ALL variations instead of only the ones on active "page"?
The following works for me in my test installation:
/**
* Replace Woocommerce Product Variation Images
* based on variation slug
*/
add_filter( 'woocommerce_product_variation_get_image_id', 'so_filter_wooc_product_variation_images', 10, 2 ) ;
function so_filter_wooc_product_variation_images( $image_id, $data ) {
//the variation product slug includes "-black"...
if ( strpos( $data->slug, '-black') ) {
//replace with new image by id number
return $new_image_id ;
}
//else return the image_id according to default settings
return $image_id ;
}
Now, you don't have search the product slug as I have done here, necessarily: There are other variables that, depending in part on your installation design, may distinguish one set of variable products from another. By default, however, the product slug for a product variation will combine the attribute terms. So, if your product is "Test Product"; you have two sizes, "Big" and "Small," and two colors, "Black" and "White"; they're enabled to produce variations; and the variations are priced (which is required), you'll get product slugs in the format test-product-big-black and test-product-big-white, and so on, so this seems like a sound method for achieve approximately what you want, focused on the attribute.
It's not, however, a foolproof method: if the attribute name occurs in the item title, then there's some chance of occasional overlap - for, say, a product with the title "Test Black Product." How to solve this issue programmatically is, obviously, a more complicated question: The function I've provided here still illustrates the general concept.
As for $new_image_id_number, you could get that in various different ways, so I've left that open. For example, you could just find the attachment ID by hovering over the image you want to use in the Media Library. Alternatively, you could create a site option - the old-fashioned away or maybe with Advanced Custom Fields Pro - and extract the ID from an image upload.
Anyway, this working solution turns out to be fairly simple. The filter hook is based on the protected function in abstract-wc-data.php...
$value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );
...which is constructed from the "hook prefix," in this case woocommerce_product_variation_, and the property in question, in this case image_id.
(I was initially confused when, viewing the product variation class, I found a filter with a similar structure that essentially duplicates the functionality for certain cases that don't apply here.)

Woocommerce - Change tax class programmatically on Order and recalcluate

I´m working on a solution to change the tax class based on the country of the customer AND if he can provide a VAT-ID or not.
The store is EU based and providing services to B2B and B2C.
So for all EU based companies that can provide a VAT-ID, no taxes will be charged unless the origin country is the same.
For all customers who cannot provide a VAT-ID, our local taxes will be added.
And so on...
All solutions I have found are only interacting with the cart, but as I´m creating the order programmatically I need another solution.
Is there a way (a hook maybe) to change the tax_class while creating the order?
For testing I tried to add this method but that did not work. It fires the filter but does not change the tax class in the order.
function wc_change_tax_class( $tax_class, $product ) {
$tax_class = 'Zero rate';
return $tax_class;
}
add_filter( 'woocommerce_product_get_tax_class', 'wc_change_tax_class', 1, 2 );
add_filter( 'woocommerce_product_variation_get_tax_class', 'wc_change_tax_class', 1, 2 );
Also I tried this method in the function that creates the order but with the same result. Nothing happend.
$woocommerce->customer->set_is_vat_exempt( true );
Thanks for any help!
After hours of trying to find the reason, the solution was quite easy:
All solutions I found suggested to write 'Zero rate' or 'Zero Rate'. Depending on what the Name was given in the Woocommerce settings. But for me, it does only work if you write it this way 'zero-rate'. So change the table class name and remove any spaces in the woocommerce settings and of course in the variable.
$tax_class = 'zero-rate';
Hope that saves someone's time!

How to change price according to variation in Woocommerce cart

I have two variations in my product. In cart page I need to choose the variations and with respective to variations I need to display the price.
How this can be done? Any track is welcome.
Kindly help me because I have searched a lot a solution for this, and I didn't anything yet that is working as I expect.
I have tried the following code in cart.php to get the variations.
<?php if( ! empty( $_product ) && $_product->get_type( 'variation' ) ){
$variation_attributes = $_product->get_variation_attributes();
$first_attribute = array_slice($variation_attributes, 0,1);
$second_attribute = array_slice($variation_attributes, 1,2); } ?>
By $first_attribute and $second_attribute, I populate the variations in a select box.
I have two attributes and four variations. So according to the variation I need to display the Price. I am not using third party PlugIn.
I believe this is already a part of WooCommerce's set up - as long as you have the variation set up specifically for X-license and X-plan you should be able to set the price to that variation.
Variation Data
Each variation may be assigned.
General:
Regular Price (required) – Set the price for this variation.
Source and full documentation: https://docs.woocommerce.com/document/variable-product/

Woocommerce Hide Variations which are not possible

I have a lot of product variations … 50 at the moment. In the first Dropdown, you can either select Single or Double. In the second Dropdown, you can select the picture frame size.
And here is what I don't get … the size 23x23 and 50x50 is only available for Single Pictures. But when I select Double I am able to select 23x23 and 50x50 just to get the information that this variation is not possible.
I am looking for a solution that when I select Double I am not even able to select nor see 50x50.
I have found this post but the code is not working for me: Hide variations that don't match
It seems to be a common problem but there is not really a solution.
It is because Woocommerce limits the max variations to 30.
Here is the code with which I managed to solve it:
function custom_wc_ajax_variation_threshold( $qty, $product ) {
return 50; // Increase default 30 to 50
}
add_filter( 'woocommerce_ajax_variation_threshold', 'custom_wc_ajax_variation_threshold', 100, 2 );

Resources