Created
August 30, 2025 13:17
-
-
Save mehrshaddarzi/88fc584f72b6c03d32eb38006476bbeb to your computer and use it in GitHub Desktop.
Auto Select First Attribute WooCommerce Product
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
| <?php | |
| add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'set_default_available_option_for_all_attributes', 20 ); | |
| function set_default_available_option_for_all_attributes( $args ) { | |
| // این بخش فقط در بارگذاری اولیه صفحه (مانند صفحه محصول) اجرا میشود | |
| if ( ! wp_doing_ajax() ) { | |
| if ( ! empty( $args['selected'] ) ) { | |
| return $args; | |
| } | |
| global $product; | |
| if ( ! $product || ! $product->is_type('variable') ) { | |
| return $args; | |
| } | |
| $variations = $product->get_available_variations(); | |
| $current_attribute_name = 'attribute_' . $args['attribute']; | |
| foreach ( $args['options'] as $option_slug ) { | |
| if ( empty( $option_slug ) ) continue; | |
| foreach ( $variations as $variation ) { | |
| if ( $variation['is_in_stock'] && isset( $variation['attributes'][$current_attribute_name] ) && $variation['attributes'][$current_attribute_name] === $option_slug ) { | |
| $args['selected'] = $option_slug; | |
| return $args; | |
| } | |
| } | |
| } | |
| } | |
| return $args; | |
| } | |
| // for signle Attribute | |
| add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'set_default_available_option_for_all_attributes', 20 ); | |
| function set_default_available_option_for_all_attributes( $args ) { | |
| // اگر از قبل یک مقدار پیشفرض (مثلاً توسط مدیر سایت) تنظیم شده بود، آن را تغییر نده | |
| if ( ! empty( $args['selected'] ) ) { | |
| return $args; | |
| } | |
| // محصول متغیر را دریافت میکنیم | |
| global $product; | |
| if ( ! $product || ! $product->is_type('variable') ) { | |
| return $args; | |
| } | |
| // لیست تمام متغیرهای محصول که فعال و موجود هستند را میگیریم | |
| $variations = $product->get_available_variations(); | |
| // نام ویژگی فعلی را به صورت داینامیک دریافت میکنیم (مثلاً: 'attribute_pa_size' یا 'attribute_pa_color') | |
| $current_attribute_name = 'attribute_' . $args['attribute']; | |
| // در میان گزینههای این ویژگی (مثلاً: L, XL یا قرمز، آبی) حلقه میزنیم | |
| foreach ( $args['options'] as $option_slug ) { | |
| if ( empty( $option_slug ) ) { | |
| continue; | |
| } | |
| // حالا در میان متغیرهای محصول میگردیم تا متغیری را پیدا کنیم که با این گزینه مطابقت دارد | |
| foreach ( $variations as $variation ) { | |
| // آیا متغیر در انبار موجود است؟ | |
| if ( $variation['is_in_stock'] ) { | |
| // آیا ویژگی این متغیر با گزینه فعلی ما یکی است؟ | |
| if ( isset( $variation['attributes'][$current_attribute_name] ) && $variation['attributes'][$current_attribute_name] === $option_slug ) { | |
| // اولین گزینهای که پیدا شد را به عنوان "selected" تنظیم کن | |
| $args['selected'] = $option_slug; | |
| // فیلتر را با مقدار جدید برگردان و از حلقهها خارج شو | |
| return $args; | |
| } | |
| } | |
| } | |
| } | |
| return $args; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment