Skip to content

Commit 321c792

Browse files
author
Antti Kuosmanen
committed
Support simple products as well as variations
1 parent beb1ff8 commit 321c792

File tree

1 file changed

+55
-10
lines changed

1 file changed

+55
-10
lines changed

woocommerce-custom-availability.php

+55-10
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,16 @@ private function __construct() {
3838
// load textdomain for translations
3939
add_action( 'plugins_loaded', array( $this, 'load_our_textdomain' ) );
4040

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+
4147
// add variation custom fields
4248
add_action( 'woocommerce_product_after_variable_attributes', array( $this, 'custom_availability_variation_field' ), 10, 3 );
4349

44-
// save handler for the fields
50+
// save handler for the variation fields
4551
add_action( 'woocommerce_save_product_variation', array( $this, 'save_custom_availability_variation_field' ), 10, 2 );
4652

4753
// use custom availability in the frontend
@@ -55,6 +61,45 @@ function load_our_textdomain() {
5561
load_plugin_textdomain( 'woocommerce-custom-availability', false, dirname( plugin_basename(__FILE__) ) . '/lang/' );
5662
}
5763

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+
58103
/**
59104
* Show the custom availability edit field for variations
60105
*
@@ -66,7 +111,7 @@ public function custom_availability_variation_field( $loop, $variation_data, $va
66111
woocommerce_wp_text_input(
67112
array(
68113
'id' => '_custom_availability[' . $variation->ID . ']',
69-
'label' => __( 'Custom Availability:', 'woocommerce-custom-availability' ),
114+
'label' => __( 'Custom Availability', 'woocommerce-custom-availability' ) . ':',
70115
'placeholder' => '',
71116
'desc_tip' => 'true',
72117
'description' => __( 'Override the default availability text.', 'woocommerce-custom-availability' ),
@@ -76,12 +121,12 @@ public function custom_availability_variation_field( $loop, $variation_data, $va
76121
}
77122

78123
/**
79-
* Handle the custom availability edit field save
124+
* Handle the custom availability edit field save for variable products
80125
*
81126
* @param int $post_id
82127
*/
83128
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 ];
85130
if( ! empty( $custom_availability ) ) {
86131
update_post_meta( $post_id, '_custom_availability', esc_attr( $custom_availability ) );
87132
}
@@ -94,12 +139,12 @@ public function save_custom_availability_variation_field( $post_id ) {
94139
* @param WC_Product $product
95140
*/
96141
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 );
103148
}
104149
return $availability;
105150
}

0 commit comments

Comments
 (0)