Woocommerce Hide Variations which are not possible - wordpress

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 );

Related

Display a custom field (field type: number) as currency with commas

I have a custom field for capturing and displaying the price of items that I am using in several different places on my site (not a shopping site, prices are just displayed as simple text, the field is setup as a number field type though).
Wherever I could create a variable and echo it in a loop or directly I managed to use money_format (although I know it is already deprecated but still works in my site).
$price = get_post_meta($post, 'price_attachment_page', true);
$pricedollar = money_format("%(#10n",$price);
and then I just call the variable $pricedollar
That is working fine when I have a chance to build the code directly. My problem comes when the same field is called in different places where I cannot do much about it, I use the same field in 2 different types of lightboxes (prettyphoto and glightbox)and within justified image gallery. All displaying in caption or description fields as simple text and this happens in literally thousands of places in my site (a very large catalog site).
I have in the mean time a simple CSS where needed:
:before {
content: "$";
}
to prefix the dollar sign, but that does not help with the commas or the period and the last 2 decimal places, The price looks very awkward.
I have tried all I found out there but nothing works.
This one looked promising and it is even on the documentation of ACF, I have it running on my functions.php but it does nothing.
add_filter( 'acf/format_value/name=price_attachment_page', 'format_number_as_currency', 10, 3 );
function format_number_as_currency( $value, $post_id, $field ){
if($value > 0) :
$value = '$' . number_format(($value), 0, '.', ',');
endif;
return $value;
}
I am sure if I alter glightbox, prettyphoto and JIG I can probably intercept where they process that specific field and add some code but I was wondering if there is a way to output that field with the currency format sitewide.
I have read at like at least 30 places some ideas to handle this and they all mention either echo or jquery ideas, I am not sure how I can make that work in the 3 places where I need that information displayed (lighboxes and responsive gallery).

Woocommerce Manually Increase $post_id to skip a series of order numbers

I am having an issue since reverting my site to a history state in which the $order_id was also reverted; however my 3rd party stock management has used a bunch of order Id's, about 100 out that will create a problem when I get to them and orders are re-made using old ID's that are already in the system.
I need to increase the count of the $order_id as to avoid this; seems easy enough but I have found NO support relating to this aside from digging around in the database and that seems unessisairy for something this simple.
I can manipulate the order number using the commonly found "add prefix or suffix to order number" but that affects all orders, past and future. So if I add a 0 to the end; all the old orders in the system suddenly also have a 0 added and it breaks the links to the external api managing the orders.
I need a way to just increase the $order_id by 100 ONLY on newly generated orders. I guess I could go post 100 articles or something tedious like that but I feel there should be a rather simple way to just increase the count on the site. Skip a few numbers then good to go y'know?
here is the code I used that changes all the orders; it is not helpful really but I mentioned it so here it is:
add_filter( 'woocommerce_order_number', 'test_woocommerce_order_number', 1, 2 );
function test_woocommerce_order_number( $oldnumber, $order ) {
$prefix = "AU-";
$suffix = 0;
return $prefix . $order->id . $suffix;
}
This will change my order number from 10338 to: AU-103380 BUT it changes all orders in the system. I simply want to increase the counter so that new orders begin at 10438.
In the end I simply made a hidden post and saved it 100 times. If you run into this issue and you need to skip 1,000 or 10,000+ values of the $order->id then my solution will not work for you. This worked for me; however "workaround-y" it may be.

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.)

Hide variations that don't match

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 );

Making the +/- Plus and Minus quantity button go up in increments of 12 in WooCommerce

I have been trying to solve what I thought might be an easy fix but I can't seem to find any information anywhere.
My problem is this:
I am setting up a wholesale store using WooCommerce with the min/max quantity and wholesale store plugins. I want to show a simple product with its single unit price (no problem) but as all products in the store are wholesale they can only be sold in (multiples) cartons of 6 or 12.
The min/max plugin allows me to set a minimum order quantity (say 12 items) but when I click on the -/+ Minus and Plus quantity selector to add another carton (another 12 items) it only adds one number (single item) at a time... E.g. 13, 14, 15, 16 and so on.
So my question is... "Is it possible to modify the 'quantity.php' file so the order quantities only go up in increments of 12?" (E.g. 12, 24, 36, 48, etc.)
I know I could simply set up and show the single carton cost as a simple product or do variables but my client wants to show a per unit price.
Thanks in advance for any feedback you may have.
There's no need to change core, nor even to use WooCommerce's template override. As Rashid points out, there is a filter in place for modifying this value. We just need to use it. Put this in a site-specific plugin.
add_filter( 'woocommerce_quantity_input_step', 'kia_quantity_input_step', 10, 2 );
function kia_quantity_input_step( $step, $product ){
return 6; // the will be the new step value
}
I'm sure you've moved on by now, but for posterity's sake this plug in might do what you are looking for:
http://www.woothemes.com/products/minmax-quantities/
I wanted similar functionality for my cart section which I achieved like this.
In the woocommerce template file forlders navigate to cart->cart.php file then search for something like this
apply_filters( 'woocommerce_quantity_input_step', '1', $_product );
Then change '1' to '12'. I believe it should be similar if you want to change on single product page.
This is quite hard to do without any hackery. If you don't mind changing a core file (which you will have to do again each time you update WooCommerce), you can do the following:
open wp-content/plugins/woocommerce/assets/js/frontend/woocommerce.js
go to line 34 "$qty.val(currentVal + 1);" and change '1' into '12'.
do the same for the minus function on line 52
note that this affects ALL products!

Resources