-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-product-options-admin.php
145 lines (87 loc) · 4.89 KB
/
simple-product-options-admin.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
<?php
class wpec_simple_product_options_walker extends Walker {
var $tree_type = 'category';
// Not sure what this is for. It was in the WordPress Walker_Category_Checklist class
// with a note saying "TODO: decouple this", so I have left it in.
var $db_fields = array( 'parent' => 'parent', 'id' => 'term_id' );
// Don't need to output anything - if this was a nest list it would be a <ul>
// It's here purely to override the default output with nothing.
function start_lvl( &$output, $depth = 0, $args = array() ) {
}
// Same as above for the closing tag.
function end_lvl( &$output, $depth = 0, $args = array() ) {
}
// Start variation set or variation
function start_el( &$output, $category, $depth = 0, $args = array(), $current_object_id = 0 ) {
// Only show product option sets, and their immediate children:
if ( $depth > 1 )
return;
extract( $args );
if ( empty( $taxonomy ) )
$taxonomy = 'wpsc-variation';
if ( $depth == 0 ) {
// Start variation set
$output .= '<div class="product_option_set">';
$output .= '<label class="set_label">
<input class="wpec-spo-option-set-checkbox" type="checkbox"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) .'name="tax_input['.esc_attr($taxonomy).'][]" value="' . esc_attr($category->term_id) . '">' . esc_html( apply_filters( 'the_category', $category->name ) ) . '
</label>';
} else {
// Start variation
$output .= '<div class="product_option" style="margin-left: 1em;">
<label>
<input class="wpec-spo-option" type="checkbox"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . 'name="tax_input['.esc_attr($taxonomy).'][]" value="'.esc_attr($category->term_id).'">' . esc_html( apply_filters( 'the_category', $category->name ) ) . '
</label>';
}
}
// End variation set or variation
function end_el( &$output, $category, $depth = 0, $args = array() ) {
if ( $depth > 1 )
return;
$output .= '</div>';
}
}
class wpec_simple_product_options_admin {
function __construct() {
add_action ( 'admin_init', array ( &$this, 'admin_init' ),11 );
}
function admin_init() {
// Remove standard WP meta box for the taxonomy, and add our own
remove_meta_box ( 'wpec_product_optiondiv', 'wpsc-product', 'side' ) ;
if ( get_terms ( 'wpec_product_option', array ( 'fields' => 'count', 'hide_empty' => false ) ) ) {
add_meta_box ( 'wpec-spo-product-options', __('Product Options', 'wpec_spo'), array(&$this, 'meta_box'), 'wpsc-product', 'normal', 'default' ) ;
add_filter ( 'taxonomy_dropdown_args', array ( &$this, 'limit_taxonomy_parent_choices' ), 10, 2 );
add_action ( 'wpec_product_option_pre_add_form', array ( &$this, 'show_taxonomy_hierarchy_warning' ) );
wp_enqueue_script( 'simple-product-options', plugin_dir_url( __FILE__ ).'js/simple-product-options.js', array());
}
}
function show_taxonomy_hierarchy_warning () {
echo '<div class="error" class="updated">You should create "<strong>Parent</strong>" elements for each option type you want, and then "<strong>child</strong>" elements for each individual choice for that option. <strong>Do not</strong> create children of children as they won\'t show up</div>';
}
function limit_taxonomy_parent_choices ( $args, $taxonomy ) {
if ( $taxonomy != 'wpec_product_option' )
return $args;
$args['depth'] = 1 ;
return $args;
}
function meta_box() {
global $post;
// Get variation data from WP Terms
$product_term_data = wp_get_object_terms( $post->ID, 'wpec_product_option' );
if ( !empty( $product_term_data ) ) {
foreach ( $product_term_data as $product_term )
$product_terms[] = $product_term->term_id;
} else {
$product_terms = array();
}
?> <ul id="wpec_product_optionchecklist" class="list:wpec_product_option categorychecklist form-no-clear"><input type='hidden' name='tax_input[wpec_product_option][]' value='0' /><?php
wp_terms_checklist ( $post->ID, array (
'taxonomy' => 'wpec_product_option',
'selected_cats' => $product_terms,
'walker' => new wpec_simple_product_options_walker,
'checked_ontop' => false
)
);
?></ul> <?php
}
}
$wpec_simple_product_options_admin = new wpec_simple_product_options_admin();