Add Variation SKU
-
I am trying include the variation sku, but the following code only adds the parent sku.
/* Sku shortcode */ function cf7_add_sku(){ global $product; return $product->get_sku(); } add_shortcode(‘CF7_ADD_SKU’, ‘cf7_add_SKU’);Any idea how to fix that? Many thanks in advance!
-
Hello? No support anymore? 🙁
Hi newshop,
This is custom code you’re writing related to a different plugin, it’s not something that can be supported, as I have no idea what your $product is or anything about it – the crux of your question does not have to do with this plugin.
What you want to do is talk to the author of whatever theme/plugin is providing your $product variable and ask how to get the SKU you’re looking for out of it. Then write your shortcode using that value. The internals of your shortcode are irrelevant to this plugin, it’s just the return value that is relevant.
Bear in mind that this plugin only has to do with values that are determined when the page loads. If your variation SKU is something chosen by the user after page load, you’d need some javascript to change the value on the client side, and that is not something this plugin does.
Thanks for your answer.
The $product comes from woocommerce. The code I posted is a snippet that I found in this support forum for your plugin. I am just searching for a possibility to add the SKU of a variable product (woocommerce) to the contact 7 form. Do you think this is possible? The SKUs are saved in the database, so I think there must be a way to call them in the form, right?!I’m sure you can get that info. But as I’m not informed about the details of WooCommerce, that’s not something I can advise you on.
I’d suggest that you print out the entire $product object and see what it contains.
If it’s not there, you probably need to generate an entire new $product object based on the variation ID instead, and ask that for its sku.
Again, really way outside the scope of this plugin. The idea is you figure out how to get the data you need, then you pass it to the CF7 DTX. Hopefully WooCommerce provides a function to retrieve exactly what you need 🙂
Hmm ok..I dont know how to do that.
Thats why I searched for another solution and have a new goal: I would like to include the stock status of each product variation into the contact form. The following code creates an shortcode and this shortcode outputs the stock status for each variation:function availability_info() { global $product; if( $product->is_type( 'variable' ) ){ $variations = $product->get_available_variations(); if( ! empty( $variations ) ){ foreach ( $variations as $variation ){ $variation = $product->get_child( $variation['variation_id'] ); echo $variation->get_formatted_variation_attributes(); echo $variation->is_in_stock() ? __( 'In Stock', 'yith-woocommerce-wishlist' ) : __( 'Out of Stock', 'yith-woocommerce-wishlist' ); } } } elseif( $stock_status == 'out-of-stock' ) { $stock_status = "Out"; echo __( 'Out of Stock', 'yith-woocommerce-wishlist' ); } else { $stock_status = "In"; echo __( 'In Stock', 'yith-woocommerce-wishlist' ); } return $output; } add_shortcode( 'instock', 'availability_info' );So this works.
Now I added the shortcode as dynamic field into a contact form and then the contact form showed the right output, but the admin-email doesnt contain it.
I added [dynamictext stock “instock”] to the form and [stock] to the email.-
This reply was modified 9 years, 4 months ago by
newshop.
No idea how to include the shortcode’s output to the email? :/
Your shortcode is not coded correctly and you need to fix it. You are echoing your output, not returning it.
All shortcodes must return, not echo.
Your $output variable that you are returning isn’t even defined, so you’re returning null. You need to define it at the beginning of your function, append your strings to that variable, and then return it. Don’t echo anything.
Thank you, I got the code from the YITH team and thought its correct because it outputs what I want when I paste it somewhere in my site, only the contact form 7 email doesnt contain it. So I will ask the YITH team to correct it and then try it again.
Yup, I’m sure once they see that they just need to return instead of echo it should work fine
-
This reply was modified 9 years, 4 months ago by
The topic ‘Add Variation SKU’ is closed to new replies.