Last active
July 31, 2024 02:35
-
-
Save DeveloperWil/1d51ca8d37c972a5f341e6bcd63e4f87 to your computer and use it in GitHub Desktop.
WooCommerce: Disable out-of-stock product variations from showing in the drop-down front-end UI add "Sold Out" label
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Finds product varant option dropdown with " - sold out" and adds a "sold-out" class to the option. | |
| * You can add the following CSS to grey-out the sold out option | |
| * option.sold-out{ color:lightgray;} | |
| */ | |
| jQuery(window).load(function(){ | |
| jQuery('body.single-product .product.type-product table.variations td.value select option').each(function( index ){ | |
| var optionText = jQuery(this).text(); | |
| if( optionText.indexOf(" - sold out") >= 0 ){ | |
| jQuery(this).addClass( 'sold-out' ); | |
| } | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Disable out-of-stock variations | |
| * | |
| * Make sure you check "Manage Stock" on each variation. | |
| * Set the stock level to zero and in the front-end drop-down variations list | |
| * the variation will be greyed-out on the product drop-down | |
| * | |
| * @author Wil Brown zeropointdevelopment.com | |
| * @param $active | |
| * @param $variation | |
| * | |
| * @return boolean | |
| */ | |
| function zpd_variation_is_active( $active, $variation ) { | |
| if( ! $variation->is_in_stock() ) { | |
| return false; | |
| } | |
| return $active; | |
| } | |
| add_filter( 'woocommerce_variation_is_active', 'zpd_variation_is_active', 10, 2 ); | |
| /** | |
| * Adds " - sold out" to the drop-down list for out-of-stock variatons. | |
| * | |
| * Make sure you check "Manage Stock" on each variation. | |
| * Set the stock level to zero and in the front-end drop-down variations list | |
| * | |
| * @param $option | |
| * @param $_ | |
| * @param $attribute | |
| * @param $product | |
| * | |
| * @return mixed|string | |
| */ | |
| function zpd_add_sold_out_label_to_wc_product_dropdown( $option, $_, $attribute, $product ){ | |
| if( is_product() ){ | |
| $sold_out_text = ' - sold out'; | |
| $variations = $product->get_available_variations(); | |
| $attributes = $product->get_attributes(); | |
| $attribute_slug = zpd_wc_get_att_slug_by_title( $attribute, $attributes ); | |
| if( empty( $attribute_slug ) ) return $option; | |
| foreach ( $variations as $variation ) { | |
| if ( $variation['attributes']['attribute_' . $attribute_slug] === $option && $variation['is_in_stock'] === FALSE ) { | |
| $option .= $sold_out_text; | |
| } | |
| } | |
| } | |
| return $option; | |
| } | |
| add_filter( 'woocommerce_variation_option_name', 'zpd_add_sold_out_label_to_wc_product_dropdown', 1, 4 ); | |
| /** | |
| * Returns the slug of the WooCommerce attribute taxonomy | |
| * | |
| * @param $attribute_title | |
| * @param $attributes | |
| * | |
| * @return int|string | |
| */ | |
| function zpd_wc_get_att_slug_by_title( $attribute_title, $attributes ){ | |
| if ( empty( $attribute_title ) || empty( $attributes )) __return_empty_string(); | |
| $att_slug = ''; | |
| foreach( $attributes as $tax => $tax_obj ){ | |
| if( $tax_obj[ 'name'] === $attribute_title ){ | |
| $att_slug = $tax; | |
| } | |
| } | |
| return $att_slug; | |
| } |
Author
DeveloperWil
commented
Jul 31, 2024
via email
I've tested this on the current version of WordPress and WooCommerce and
the code still works.
Perhaps try to disable some plugins or code snippets to see of you have a
conflict.
Wil.
…On Tue, Jul 23, 2024 at 8:34 PM Mohsen Amra ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Hi
I have tried this method but it does not working,
Is it possible to uptade it?
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/DeveloperWil/1d51ca8d37c972a5f341e6bcd63e4f87#gistcomment-5130336>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABZONG5RXX7VDBDIR6RRVN3ZNYWS3BFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTANZRGUYTONBTU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
--
Wil Brown
*WordPress Consultant, Developer & Educator at **Zero Point Development
<https://zeropointdevelopment.com/>*
*WordPress Community Deputy & WordCamp Mentor*
*m. 0423 526 829 <+61423526829> **w. zeropointdevelopment.com
<https://zeropointdevelopment.com?utm_source=wil-zpd-email-signature>
t. @WilBrown_AU <https://twitter.com/WilBrown_AU> LIn. LinkedIn
<https://www.linkedin.com/in/developerwil/>*
*Get my FREE business courses <https://community.wilbrown.com/>. 👩🎓*
*Join me at WordPress Sydney
<https://rebrand.ly/66982?utm_source=wil-zpd-email-signature> and Elementor
Sydney <https://events.elementor.com/elementor-sydney/>*
<https://about.me/wil_brown?promo=email_sig&utm_source=product&utm_medium=email_sig&utm_campaign=edit_panel&utm_content=thumb>
Wil Brown
about.me/wil_brown
<https://about.me/wil_brown?promo=email_sig&utm_source=product&utm_medium=email_sig&utm_campaign=edit_panel&utm_content=thumb>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment