'no'); }); add_filter('usedkit_save_settings', function ($out, $post) { return $out + array('specs' => !empty($post['specs']) ? 'yes' : 'no'); }, 10, 2); add_action('usedkit_settings_rows', function ($s) { ?> get_meta('_usedkit_specs') : array(); $current = $stored['template'] ?? get_user_meta(get_current_user_id(), 'usedkit_last_template', true); $values = $stored['values'] ?? array(); $templates = usedkit_spec_templates(); usedkit_panel_heading(__('Specs', 'usedkit')); $options = array('' => __('None', 'usedkit')); foreach ($templates as $k => $t) { $options[$k] = $t[0]; } woocommerce_wp_select(array('id' => '_usedkit_template', 'label' => __('Kind of item', 'usedkit'), 'options' => $options, 'value' => $current, 'custom_attributes' => array('onchange' => "document.querySelectorAll('.usedkit-spec').forEach(function(e){e.style.display=e.dataset.t===this.value?'':'none'},this)"))); foreach ($templates as $t => $template) { foreach ($template[1] as $k => $field) { $id = '_usedkit_spec_' . $t . '_' . $k; $value = $t === $current ? ($values[$k] ?? '') : ''; $label = $field[0] . ($field[1] === 'number' && $field[2] !== '' ? ' (' . $field[2] . ')' : ''); echo ''; } } }, 20); add_action('woocommerce_admin_process_product_object', function ($product) { if (!usedkit_specs_on() || !usedkit_product_form_ok($product) || !isset($_POST['_usedkit_template'])) { // phpcs:ignore WordPress.Security.NonceVerification -- verified in usedkit_product_form_ok() return; } $templates = usedkit_spec_templates(); $t = sanitize_key(wp_unslash($_POST['_usedkit_template'])); // phpcs:ignore $before = (array) $product->get_meta('_usedkit_specs'); $values = array(); $show = array(); if (isset($templates[$t])) { update_user_meta(get_current_user_id(), 'usedkit_last_template', $t); foreach ($templates[$t][1] as $k => $field) { $v = usedkit_posted('_usedkit_spec_' . $t . '_' . $k); if ($v !== null && $v !== '' && ($field[1] !== 'select' || in_array($v, $field[2], true))) { $values[$k] = $v; $show[$field[0]] = usedkit_spec_display($field, $v); } } } // The names this tab set last time: any of them not set now is cleared. $old_names = array(); foreach (($templates[$before['template'] ?? ''][1] ?? array()) as $k => $field) { if (isset($before['values'][$k])) { $old_names[] = mb_strtolower($field[0]); } } usedkit_apply_specs($product, $show, $old_names); if ($values || $t !== '') { $product->update_meta_data('_usedkit_specs', array('template' => $t, 'values' => $values)); } else { $product->delete_meta_data('_usedkit_specs'); } }); /** Set the product's attributes to `$show` [name => value], dropping `$drop` names. */ function usedkit_apply_specs($product, $show, $drop = array()) { $attrs = $product->get_attributes(); $lower = array_map('mb_strtolower', array_keys($show)); foreach ($attrs as $key => $attr) { $name = mb_strtolower($attr->get_name()); if (!$attr->is_taxonomy() && (in_array($name, $lower, true) || in_array($name, $drop, true))) { unset($attrs[$key]); } } $position = count($attrs); foreach ($show as $name => $value) { $a = new WC_Product_Attribute(); $a->set_name($name); $a->set_options(array($value)); $a->set_visible(true); $a->set_variation(false); $a->set_position($position++); $attrs[sanitize_title($name)] = $a; } $product->set_attributes($attrs); }