I’m using PopupMaker plugin for wordpress to create the popup up. This works by using a click trigger on a css selector of your choosing.
With this line of code I can output the variations for the particular product when I’m on its single product page.
add_action( 'woocommerce_before_add_to_cart_quantity', 'dump_attributes' );
function dump_attributes() {
global $product;
var_dump($product->get_attribute('size'));
}
I need to output a click-able link only when the customer selects a certain variation from the drop down.
The problem I have is generating that link only when a user selects the variation ‘Drum’.
2
Answers
Figured out one way to solve it using jQuery and getting the ID of the selection drop down (there were two different ID's named after the attribute slug
#size
&#pa_size
).I was able to give this a little more thought. First, we need to give the variation data that Woo sends via JSON a little extra value. I’m checking for a “black” color attribute:
if( 'black' === strtolower($variation->get_attribute('color')) ){
But you will want to change to suit your size attribute.
Next, I define the link as
$kia_data['custom_link'] = sprintf( '<a href="google.com">%s</a>', __( 'Your Link', 'your-textdomain' ) );
This is just a link to google and so you’ll need to change that.
Next, we add an empty
<div>
placeholder. And finally we load some scripts on variable products and listen for thefound_variation
event. When we get it, we get thevariation
from the JSON data and we can check for thecustom_link
we defined earlier and dump it into the empty holder<div>
.That’s probably clear as mud, but here’s the full code snippet:
Here is the result: