@@ -38,10 +38,16 @@ private function __construct() {
38
38
// load textdomain for translations
39
39
add_action ( 'plugins_loaded ' , array ( $ this , 'load_our_textdomain ' ) );
40
40
41
+ // add single custom fields
42
+ add_action ( 'woocommerce_product_options_stock_fields ' , array ( $ this , 'custom_availability_field ' ) );
43
+
44
+ // save handler for custom fields
45
+ add_action ( 'save_post ' , array ( $ this , 'save_custom_availability_field ' ) );
46
+
41
47
// add variation custom fields
42
48
add_action ( 'woocommerce_product_after_variable_attributes ' , array ( $ this , 'custom_availability_variation_field ' ), 10 , 3 );
43
49
44
- // save handler for the fields
50
+ // save handler for the variation fields
45
51
add_action ( 'woocommerce_save_product_variation ' , array ( $ this , 'save_custom_availability_variation_field ' ), 10 , 2 );
46
52
47
53
// use custom availability in the frontend
@@ -55,6 +61,45 @@ function load_our_textdomain() {
55
61
load_plugin_textdomain ( 'woocommerce-custom-availability ' , false , dirname ( plugin_basename (__FILE__ ) ) . '/lang/ ' );
56
62
}
57
63
64
+ /**
65
+ * Show the custom availability edit field for simple products
66
+ */
67
+ public function custom_availability_field () {
68
+ global $ post ;
69
+
70
+ woocommerce_wp_text_input (
71
+ array (
72
+ 'id ' => '_custom_availability_simple ' ,
73
+ 'label ' => __ ( 'Custom Availability ' , 'woocommerce-custom-availability ' ),
74
+ 'placeholder ' => '' ,
75
+ 'desc_tip ' => 'true ' ,
76
+ 'description ' => __ ( 'Override the default availability text. ' , 'woocommerce-custom-availability ' ),
77
+ 'value ' => get_post_meta ( $ post ->ID , '_custom_availability ' , true )
78
+ )
79
+ );
80
+ }
81
+
82
+ /**
83
+ * Handle the custom availability edit field save for simple products
84
+ */
85
+ public function save_custom_availability_field () {
86
+ global $ post ;
87
+ if ( defined ( 'DOING_AUTOSAVE ' ) && DOING_AUTOSAVE ) {
88
+ return ;
89
+ }
90
+
91
+ if ( 'product ' != $ post ->post_type ) {
92
+ return ;
93
+ }
94
+
95
+ if ( ! isset ( $ _REQUEST ['_custom_availability_simple ' ] ) ) {
96
+ return ;
97
+ }
98
+
99
+ update_post_meta ( $ post ->ID , '_custom_availability ' , $ _REQUEST ['_custom_availability_simple ' ] );
100
+ }
101
+
102
+
58
103
/**
59
104
* Show the custom availability edit field for variations
60
105
*
@@ -66,7 +111,7 @@ public function custom_availability_variation_field( $loop, $variation_data, $va
66
111
woocommerce_wp_text_input (
67
112
array (
68
113
'id ' => '_custom_availability[ ' . $ variation ->ID . '] ' ,
69
- 'label ' => __ ( 'Custom Availability: ' , 'woocommerce-custom-availability ' ),
114
+ 'label ' => __ ( 'Custom Availability ' , 'woocommerce-custom-availability ' ) . ' : ' ,
70
115
'placeholder ' => '' ,
71
116
'desc_tip ' => 'true ' ,
72
117
'description ' => __ ( 'Override the default availability text. ' , 'woocommerce-custom-availability ' ),
@@ -76,12 +121,12 @@ public function custom_availability_variation_field( $loop, $variation_data, $va
76
121
}
77
122
78
123
/**
79
- * Handle the custom availability edit field save
124
+ * Handle the custom availability edit field save for variable products
80
125
*
81
126
* @param int $post_id
82
127
*/
83
128
public function save_custom_availability_variation_field ( $ post_id ) {
84
- $ custom_availability = $ _POST ['_custom_availability ' ][ $ post_id ];
129
+ $ custom_availability = $ _REQUEST ['_custom_availability ' ][ $ post_id ];
85
130
if ( ! empty ( $ custom_availability ) ) {
86
131
update_post_meta ( $ post_id , '_custom_availability ' , esc_attr ( $ custom_availability ) );
87
132
}
@@ -94,12 +139,12 @@ public function save_custom_availability_variation_field( $post_id ) {
94
139
* @param WC_Product $product
95
140
*/
96
141
public function custom_availability ( $ availability , $ product ) {
97
- if ( $ product ->variation_id ) {
98
- $ custom_availability = get_post_meta ( $ product -> variation_id , '_custom_availability ' , true );
99
- if ( ! empty ( $ custom_availability ) ) {
100
- $ availability [ ' class ' ] = ' custom-availability ' ;
101
- $ availability ['availability ' ] = esc_attr ( $ custom_availability ) ;
102
- }
142
+ $ product_id = isset ( $ product ->variation_id ) ? $ product -> variation_id : $ product -> id ;
143
+ $ custom_availability = get_post_meta ( $ product_id , '_custom_availability ' , true );
144
+
145
+ if ( ! empty ( $ custom_availability ) ) {
146
+ $ availability ['class ' ] = ' custom-availability ' ;
147
+ $ availability [ ' availability ' ] = esc_attr ( $ custom_availability );
103
148
}
104
149
return $ availability ;
105
150
}
0 commit comments