-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-wc-min-max-quantities-blocks.php
184 lines (172 loc) · 6.78 KB
/
class-wc-min-max-quantities-blocks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* WC_Min_Max_Quantities_Blocks class.
*/
class WC_Min_Max_Quantities_Blocks {
public function __construct() {
add_action( 'init', array( $this, 'register_custom_blocks' ) );
add_action( 'woocommerce_block_template_area_product-form_after_add_block_inventory', array( $this, 'add_blocks_to_product_editor' ) );
}
public function register_custom_blocks() {
if ( isset( $_GET['page'] ) && 'wc-admin' === $_GET['page'] ) {
register_block_type( WC_MMQ_ABSPATH . 'assets/dist/admin/product-editor/min-quantity/block.json' );
register_block_type( WC_MMQ_ABSPATH . 'assets/dist/admin/product-editor/max-quantity/block.json' );
register_block_type( WC_MMQ_ABSPATH . 'assets/dist/admin/product-editor/group-of-quantity/block.json' );
register_block_type( WC_MMQ_ABSPATH . 'assets/dist/admin/product-editor/variation-exclude/block.json' );
register_block_type( WC_MMQ_ABSPATH . 'assets/dist/admin/product-editor/variation-group-of-quantity/block.json' );
register_block_type( WC_MMQ_ABSPATH . 'assets/dist/admin/product-editor/variation-max-quantity/block.json' );
register_block_type( WC_MMQ_ABSPATH . 'assets/dist/admin/product-editor/variation-min-quantity/block.json' );
register_block_type( WC_MMQ_ABSPATH . 'assets/dist/admin/product-editor/variation-override/block.json' );
}
}
public function add_blocks_to_product_editor( $inventory_tab ) {
if ( ! method_exists( $inventory_tab, 'add_section' ) ) {
return;
}
if ( $inventory_tab->get_root_template()->get_id() === 'simple-product' ) {
$this->add_blocks_to_simple_product_template( $inventory_tab );
} elseif ( $inventory_tab->get_root_template()->get_id() === 'product-variation' ) {
$this->add_blocks_to_product_variation_template( $inventory_tab );
}
}
private function add_blocks_to_simple_product_template( $inventory_tab ) {
$section = $inventory_tab->add_section(
array(
'id' => 'wc_min_max_section',
'attributes' => array(
'title' => __( 'Quantity rules', 'woocommerce-min-max-quantities' ),
),
'hideConditions' => array(
array(
'expression' => 'editedProduct.sold_individually === true',
),
),
)
);
$section->add_block(
array(
'id' => 'wc_min_max_group_of_quantity',
'blockName' => 'woocommerce-min-max/group-of-quantity-field',
'attributes' => array(
'label' => __( 'Sell in groups of', 'woocommerce-min-max-quantities' ),
)
)
);
$section->add_block(
array(
'id' => 'wc_min_max_minimum_allowed_quantity',
'blockName' => 'woocommerce-min-max/min-quantity-field',
'attributes' => array(
'label' => __( 'Min. Quantity', 'woocommerce-min-max-quantities' ),
)
)
);
$section->add_block(
array(
'id' => 'wc_min_max_maximum_allowed_quantity',
'blockName' => 'woocommerce-min-max/max-quantity-field',
'attributes' => array(
'label' => __( 'Max. Quantity', 'woocommerce-min-max-quantities' ),
)
)
);
$section->add_block(
array(
'id' => 'wc_min_max_allow_combination',
'blockName' => 'woocommerce/product-checkbox-field',
'hideConditions' => array(
array(
'expression' => 'editedProduct.type !== "variable"',
),
),
'attributes' => array(
'label' => __( 'Combine variations', 'woocommerce-min-max-quantities' ),
'property' => 'meta_data.allow_combination',
'checkedValue' => 'yes',
'uncheckedValue' => 'no',
'tooltip' => __( 'Check to apply the settings above to the sum of quantities of variations. E.g., max quantity of 5 can be satisfied by adding 2 units of one variation and 3 units of another.', 'woocommerce-min-max-quantities' ),
)
)
);
$section->add_block(
array(
'id' => 'wc_min_max_cart_exclude_from_order',
'blockName' => 'woocommerce/product-checkbox-field',
'attributes' => array(
'label' => __( 'Exclude from order rules', 'woocommerce-min-max-quantities' ),
'property' => 'meta_data.minmax_cart_exclude',
'checkedValue' => 'yes',
'uncheckedValue' => 'no',
'tooltip' => sprintf( __( 'Check to exclude this product from the total order quantity and value calculations set up in %1$sorder settings.%2$s', 'woocommerce-min-max-quantities' ), '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=products' ) . '" target="_blank">', '</a>' ),
)
)
);
}
private function add_blocks_to_product_variation_template( $inventory_tab ) {
$section = $inventory_tab->add_section(
array(
'id' => 'wc_min_max_section',
'attributes' => array(
'title' => __( 'Quantity rules', 'woocommerce-min-max-quantities' ),
),
)
);
$section->add_block(
array(
'id' => 'wc_min_max_override_main_product',
'blockName' => 'woocommerce-min-max/variation-override-field',
'attributes' => array(
'label' => __( "Override the main product's settings", 'woocommerce-min-max-quantities' ),
)
)
);
$section->add_block(
array(
'id' => 'wc_min_max_group_of_quantity',
'blockName' => 'woocommerce-min-max/variation-group-of-quantity-field',
'attributes' => array(
'label' => __( 'Sell in groups of', 'woocommerce-min-max-quantities' ),
)
)
);
$section->add_block(
array(
'id' => 'wc_min_max_minimum_allowed_quantity',
'blockName' => 'woocommerce-min-max/variation-min-quantity-field',
'attributes' => array(
'label' => __( 'Min. Quantity', 'woocommerce-min-max-quantities' ),
'tooltip' => __(
'Enter a minimum quantity customers can buy in a single order. This is particularly useful for items sold in larger quantities, like multipacks.',
'woocommerce-min-max-quantities'
),
)
)
);
$section->add_block(
array(
'id' => 'wc_min_max_maximum_allowed_quantity',
'blockName' => 'woocommerce-min-max/variation-max-quantity-field',
'attributes' => array(
'label' => __( 'Max. Quantity', 'woocommerce-min-max-quantities' ),
'placeholder' => __('No limit', 'woocommerce-min-max-quantities'),
'tooltip' => __(
'Enter a maximum quantity customers can buy in a single order. This is particularly useful for items that have limited quantity, like art or handmade goods.',
'woocommerce-min-max-quantities'
),
)
)
);
$section->add_block(
array(
'id' => 'wc_min_max_cart_exclude_from_order',
'blockName' => 'woocommerce-min-max/variation-exclude-field',
'attributes' => array(
'label' => __( 'Exclude from order rules', 'woocommerce-min-max-quantities' ),
'tooltip' => sprintf( __( 'Check to exclude this product from the total order quantity and value calculations set up in %1$sorder settings.%2$s', 'woocommerce-min-max-quantities' ), '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=products' ) . '" target="_blank">', '</a>' ),
),
)
);
}
}
// Add blocks to new product editor.
new WC_Min_Max_Quantities_Blocks();