-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc-button-variants-optons.php
180 lines (148 loc) · 6.13 KB
/
wc-button-variants-optons.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
<?php
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'rudr_button_variations', 20, 2 );
function rudr_button_variations( $html, $args ) {
// Extract necessary variables from $args
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute'];
$name = $args['name'] ?: 'attribute_' . sanitize_title( $attribute );
// Return original HTML if there are no options or no product
if ( empty( $options ) || ! $product ) {
return $html;
}
// Prepare an array to hold options along with their prices
$options_with_prices = [];
/**
* @var WC_Product_Variable $product
*/
// Get available variations for the product
$variations = $product->get_available_variations();
// Get the count of variation attributes
$countAttributes = count($product->get_variation_attributes());
if ($countAttributes > 1) {
return $html;
}
// Loop through the variations to fetch prices and attribute values
foreach ( $variations as $variation ) {
$variation_id = $variation['variation_id'];
$variation_obj = wc_get_product( $variation_id );
// Ensure the product is a valid variation
if ( $variation_obj && $variation_obj->is_type( 'variation' ) ) {
$attributes = $variation_obj->get_attributes();
$attribute_value = $attributes[strtolower($attribute)] ?? ''; // Get the specific attribute value
$price = $variation_obj->get_price();
// Check if the current attribute value is in the options list
if ( in_array( $attribute_value, $options, true ) ) {
$key = sanitize_title($attribute_value);
$options_with_prices[$key] = [
'value' => $attribute_value,
'name' => $attribute_value,
'price' => $price,
];
}
}
}
// Sort options by price if there's only one attribute
usort( $options_with_prices, function ( $a, $b ) {
return $a['price'] - $b['price'];
});
// Build the HTML for the variation buttons
$buttons = '<div class="variation-buttons">';
$buttons .= '<div class="title-variant" style="width: 100%; padding-left: 5px">Pilih ' . esc_html( $attribute ) . '</div>';
foreach ( $options_with_prices as $item ) {
$value = sanitize_text_field( $item['value'] );
$label = esc_html( $item['name'] );
$label .= ' - ' . wc_price( $item['price'] );
// Create button HTML
$buttons .= '<button type="button" class="variation-button" data-value="' . esc_attr( $value ) . '" data-name="' . esc_attr( $name ) . '">
<span>' . $label . '</span>
</button>';
}
$buttons .= '</div>';
// Return original select HTML plus the custom buttons
return $html . $buttons;
}
add_action('wp_head', function () {
?>
<style>
.variation-buttons {
display: flex;
flex-wrap: wrap;
}
.variation-button {
background-color: #f1f1f1;
border: 1px solid #ddd;
border-radius: 4px;
padding: 10px;
margin: 5px;
cursor: pointer;
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
color: #0b0b0b;
}
.variation-button .variation-price {
font-size: 0.9em;
color: #555;
}
.variation-button.selected {
background-color: #007cba;
color: #fff;
}
table.variations .label,
table.variations .value select {
display: none !important;
}
</style>
<?php
});
add_action('wp_footer', function () {
?>
<script>
jQuery(document).ready(function($) {
const selection = $('select[data-attribute_name]');
// When a variation button is clicked
$(document).on('click', '.variation-button', function() {
let $this = $(this);
let value = $this.data('value');
let name = $this.data('name');
let parent = $this.closest('.variation-buttons');
$('select[name="' + name + '"]').val(value).trigger('change');
parent.find('.variation-button').removeClass('selected');
$this.addClass('selected');
});
// Function to sync the buttons with the current dropdown selection
function syncButtonWithSelection() {
selection.each(function() {
let $select = $(this);
let name = $select.data('attribute_name');
let value = $select.val();
// Find the button that corresponds to the select's value and mark it as selected
let $button = $('.variation-button[data-value="' + value + '"][data-name="' + name + '"]');
// Remove the selected class from all buttons within this group
$button.closest('.variation-buttons').find('.variation-button').removeClass('selected');
// Add the selected class to the matched button
if ($button.length) {
$button.addClass('selected');
}
});
}
// Trigger sync initially on page load
syncButtonWithSelection();
// Update buttons when the user changes the select manually
selection.on('change', function() {
syncButtonWithSelection();
});
// Handle the reset button to clear selections
$('.reset_variations').on('click', function(e) {
e.preventDefault();
$('select[data-attribute_name]').each(function() {
$(this).val('').trigger('change'); // Clear select dropdown
});
$('.variation-button').removeClass('selected'); // Clear button selection
});
});
</script>
<?php
}, PHP_INT_MAX);