/** * Test wc_sanitize_taxonomy_name(). * * @since 2.2 */ public function test_wc_sanitize_taxonomy_name() { $this->assertEquals('name-with-spaces', wc_sanitize_taxonomy_name('Name With Spaces')); $this->assertEquals('namewithtabs', wc_sanitize_taxonomy_name('Name With Tabs')); $this->assertEquals('specialchars', wc_sanitize_taxonomy_name('special!@#$%^&*()chars')); $this->assertEquals('look-of-ಠ_ಠ', wc_sanitize_taxonomy_name('Look Of ಠ_ಠ')); }
/** * Layered Nav Init * * @package WooCommerce/Widgets * @access public * @return void */ public function woocommerce_layered_nav_init() { if (is_active_widget(false, false, 'yith-woo-ajax-navigation', true) && !is_admin()) { global $_chosen_attributes, $woocommerce; $_chosen_attributes = array(); /* FIX TO WOOCOMMERCE 2.1 */ $attibute_taxonomies = function_exists('wc_get_attribute_taxonomies') ? $attribute_taxonomies = wc_get_attribute_taxonomies() : ($attribute_taxonomies = $woocommerce->get_attribute_taxonomies()); if ($attribute_taxonomies) { foreach ($attribute_taxonomies as $tax) { $attribute = wc_sanitize_taxonomy_name($tax->attribute_name); /* FIX TO WOOCOMMERCE 2.1 */ if (function_exists('wc_attribute_taxonomy_name')) { $taxonomy = wc_attribute_taxonomy_name($attribute); } else { $taxonomy = $woocommerce->attribute_taxonomy_name($attribute); } $name = 'filter_' . $attribute; $query_type_name = 'query_type_' . $attribute; if (!empty($_GET[$name]) && taxonomy_exists($taxonomy)) { $_chosen_attributes[$taxonomy]['terms'] = explode(',', $_GET[$name]); if (empty($_GET[$query_type_name]) || !in_array(strtolower($_GET[$query_type_name]), array('and', 'or'))) { $_chosen_attributes[$taxonomy]['query_type'] = apply_filters('woocommerce_layered_nav_default_query_type', 'and'); } else { $_chosen_attributes[$taxonomy]['query_type'] = strtolower($_GET[$query_type_name]); } } } } if (version_compare(preg_replace('/-beta-([0-9]+)/', '', $woocommerce->version), '2.1', '<')) { add_filter('loop_shop_post_in', 'woocommerce_layered_nav_query'); } else { add_filter('loop_shop_post_in', array(WC()->query, 'layered_nav_query')); } } }
public function color_layered_nav_init() { if (!is_active_widget(false, false, 'woocommerce_layered_nav', true) && !is_admin()) { global $_chosen_attributes; $_chosen_attributes = array(); $attribute_taxonomies = wc_get_attribute_taxonomies(); if ($attribute_taxonomies) { foreach ($attribute_taxonomies as $tax) { if ($tax->attribute_name == "color") { $attribute = wc_sanitize_taxonomy_name($tax->attribute_name); $taxonomy = wc_attribute_taxonomy_name($attribute); $name = 'filter_' . $attribute; $query_type_name = 'query_type_' . $attribute; $taxonomy_exists = in_array($taxonomy, wc_get_attribute_taxonomy_names()); if (!empty($_GET[$name]) && $taxonomy_exists) { $_chosen_attributes[$taxonomy]['terms'] = explode(',', $_GET[$name]); if (empty($_GET[$query_type_name]) || !in_array(strtolower($_GET[$query_type_name]), array('and', 'or'))) { $_chosen_attributes[$taxonomy]['query_type'] = apply_filters('woocommerce_layered_nav_default_query_type', 'and'); } else { $_chosen_attributes[$taxonomy]['query_type'] = strtolower($_GET[$query_type_name]); } } } } } $wc_query = new WC_Query(); add_filter('loop_shop_post_in', array($wc_query, 'layered_nav_query')); } }
/** * Sort the data for CSV output first * * @param int $product_id * @param array $headers * @param array $body * @param array $items */ public static function output_csv($product_id, $headers, $body, $items) { $headers['quantity'] = __('Quantity', 'wcvendors'); $new_body = array(); foreach ($body as $i => $order) { // Remove comments unset($body[$i]['comments']); // Remove all numeric keys in each order (these are the meta values we are redoing into new lines) foreach ($order as $key => $col) { if (is_int($key)) { unset($order[$key]); } } // New order row $new_row = $body[$i]; // Remove order to redo unset($body[$i]); $order = new WC_Order($i); foreach ($items[$i]['items'] as $item) { $product_id = !empty($item['variation_id']) ? $item['variation_id'] : $item['product_id']; $new_row_with_meta = $new_row; // Add the qty row $new_row_with_meta[] = $item['qty']; $item_meta = $item['name']; if ($metadata = $order->has_meta($item['product_id'])) { foreach ($metadata as $meta) { // Skip hidden core fields if (in_array($meta['meta_key'], apply_filters('woocommerce_hidden_order_itemmeta', array('_qty', '_tax_class', '_product_id', '_variation_id', '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax', WC_Vendors::$pv_options->get_option('sold_by_label'))))) { continue; } // Skip serialised meta if (is_serialized($meta['meta_value'])) { continue; } // Get attribute data if (taxonomy_exists(wc_sanitize_taxonomy_name($meta['meta_key']))) { $term = get_term_by('slug', $meta['meta_value'], wc_sanitize_taxonomy_name($meta['meta_key'])); $meta['meta_key'] = wc_attribute_label(wc_sanitize_taxonomy_name($meta['meta_key'])); $meta['meta_value'] = isset($term->name) ? $term->name : $meta['meta_value']; } else { $meta['meta_key'] = apply_filters('woocommerce_attribute_label', wc_attribute_label($meta['meta_key'], $_product), $meta['meta_key']); } $item_meta .= wp_kses_post(rawurldecode($meta['meta_key'])) . ':' . wp_kses_post(wpautop(make_clickable(rawurldecode($meta['meta_value'])))); } } $new_row_with_meta['product'] = $item_meta; $new_body[] = $new_row_with_meta; } } $headers = apply_filters('wcvendors_csv_headers', $headers, $product_id, $items); $body = apply_filters('wcvendors_csv_body', $new_body, $product_id, $items); WCV_Export_CSV::download($headers, $body, $product_id); }
/** * Get a product attributes label. * * @param mixed $name * @return string */ function wc_attribute_label($name) { global $wpdb; if (taxonomy_is_product_attribute($name)) { $name = wc_sanitize_taxonomy_name(str_replace('pa_', '', $name)); $label = $wpdb->get_var($wpdb->prepare("SELECT attribute_label FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name)); if (!$label) { $label = ucfirst($name); } } else { $label = ucwords(str_replace('-', ' ', $name)); } return apply_filters('woocommerce_attribute_label', $label, $name); }
/** * Get a product attributes label. * * @param string $name * @param object $product object Optional * @return string */ function wc_attribute_label($name, $product = '') { global $wpdb; if (taxonomy_is_product_attribute($name)) { $name = wc_sanitize_taxonomy_name(str_replace('pa_', '', $name)); $label = $wpdb->get_var($wpdb->prepare("SELECT attribute_label FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name)); if (!$label) { $label = $name; } } elseif ($product && ($attributes = $product->get_attributes()) && isset($attributes[sanitize_title($name)]['name'])) { // Attempt to get label from product, as entered by the user $label = $attributes[sanitize_title($name)]['name']; } else { $label = str_replace('-', ' ', $name); } return apply_filters('woocommerce_attribute_label', $label, $name, $product); }
/** * When running the WP importer, ensure attributes exist. * * WordPress import should work - however, it fails to import custom product attribute taxonomies. * This code grabs the file before it is imported and ensures the taxonomies are created. */ public function post_importer_compatibility() { global $wpdb; if (empty($_POST['import_id']) || !class_exists('WXR_Parser')) { return; } $id = absint($_POST['import_id']); $file = get_attached_file($id); $parser = new WXR_Parser(); $import_data = $parser->parse($file); if (isset($import_data['posts'])) { $posts = $import_data['posts']; if ($posts && sizeof($posts) > 0) { foreach ($posts as $post) { if ('product' === $post['post_type']) { if (!empty($post['terms'])) { foreach ($post['terms'] as $term) { if (strstr($term['domain'], 'pa_')) { if (!taxonomy_exists($term['domain'])) { $attribute_name = wc_sanitize_taxonomy_name(str_replace('pa_', '', $term['domain'])); // Create the taxonomy if (!in_array($attribute_name, wc_get_attribute_taxonomies())) { $attribute = array('attribute_label' => $attribute_name, 'attribute_name' => $attribute_name, 'attribute_type' => 'select', 'attribute_orderby' => 'menu_order', 'attribute_public' => 0); $wpdb->insert($wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute); delete_transient('wc_attribute_taxonomies'); } // Register the taxonomy now so that the import works! register_taxonomy($term['domain'], apply_filters('woocommerce_taxonomy_objects_' . $term['domain'], array('product')), apply_filters('woocommerce_taxonomy_args_' . $term['domain'], array('hierarchical' => true, 'show_ui' => false, 'query_var' => true, 'rewrite' => false))); } } } } } } } } }
/** * wcj_get_order_item_meta_info. * * from woocommerce\includes\admin\meta-boxes\views\html-order-item-meta.php * * @version 2.5.9 * @since 2.5.9 */ function wcj_get_order_item_meta_info($item_id, $item, $_order, $exclude_wcj_meta = false, $_product = null) { $meta_info = ''; if ($metadata = $_order->has_meta($item_id)) { $meta_info = array(); foreach ($metadata as $meta) { // Skip hidden core fields if (in_array($meta['meta_key'], apply_filters('woocommerce_hidden_order_itemmeta', array('_qty', '_tax_class', '_product_id', '_variation_id', '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax', 'method_id', 'cost')))) { continue; } if ($exclude_wcj_meta && ('wcj' === substr($meta['meta_key'], 0, 3) || '_wcj' === substr($meta['meta_key'], 0, 4))) { continue; } // Skip serialised meta if (is_serialized($meta['meta_value'])) { continue; } // Get attribute data if (taxonomy_exists(wc_sanitize_taxonomy_name($meta['meta_key']))) { $term = get_term_by('slug', $meta['meta_value'], wc_sanitize_taxonomy_name($meta['meta_key'])); $meta['meta_key'] = wc_attribute_label(wc_sanitize_taxonomy_name($meta['meta_key'])); $meta['meta_value'] = isset($term->name) ? $term->name : $meta['meta_value']; } else { $the_product = null; if (is_object($_product)) { $the_product = $_product; } elseif (is_object($item)) { $the_product = $_order->get_product_from_item($item); } $meta['meta_key'] = is_object($the_product) ? wc_attribute_label($meta['meta_key'], $the_product) : $meta['meta_key']; } $meta_info[] = wp_kses_post(rawurldecode($meta['meta_key'])) . ': ' . wp_kses_post(rawurldecode($meta['meta_value'])); } $meta_info = implode(', ', $meta_info); } return $meta_info; }
/** * Layered Nav Init * * @global array $_chosen_attributes * * @return false if not layered nav filter */ public function layeredNavInit() { if (!(is_active_widget(false, false, 'woocommerce_layered_nav', true) && !is_admin())) { return false; } global $_chosen_attributes; $attributes = wc_get_attribute_taxonomies(); foreach ($attributes as $tax) { $attribute = wc_sanitize_taxonomy_name($tax->attribute_name); $taxonomy = wc_attribute_taxonomy_name($attribute); $name = 'filter_' . $attribute; if (!(!empty($_GET[$name]) && taxonomy_exists($taxonomy))) { continue; } $terms = explode(',', $_GET[$name]); $termsTranslations = array(); foreach ($terms as $ID) { $translation = pll_get_term($ID); $termsTranslations[] = $translation ? $translation : $ID; } $_GET[$name] = implode(',', $termsTranslations); $_chosen_attributes[$taxonomy]['terms'] = $termsTranslations; } }
$wpdb->query("\r\n\tUPDATE {$wpdb->posts} as posts\r\n\tLEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID\r\n\tLEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )\r\n\tLEFT JOIN {$wpdb->terms} AS term USING( term_id )\r\n\tSET posts.post_status = 'wc-on-hold'\r\n\tWHERE posts.post_type = 'shop_order'\r\n\tAND posts.post_status = 'publish'\r\n\tAND tax.taxonomy = 'shop_order_status'\r\n\tAND\tterm.slug LIKE 'on-hold%';\r\n\t"); $wpdb->query("\r\n\tUPDATE {$wpdb->posts} as posts\r\n\tLEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID\r\n\tLEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )\r\n\tLEFT JOIN {$wpdb->terms} AS term USING( term_id )\r\n\tSET posts.post_status = 'wc-completed'\r\n\tWHERE posts.post_type = 'shop_order'\r\n\tAND posts.post_status = 'publish'\r\n\tAND tax.taxonomy = 'shop_order_status'\r\n\tAND\tterm.slug LIKE 'completed%';\r\n\t"); $wpdb->query("\r\n\tUPDATE {$wpdb->posts} as posts\r\n\tLEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID\r\n\tLEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )\r\n\tLEFT JOIN {$wpdb->terms} AS term USING( term_id )\r\n\tSET posts.post_status = 'wc-cancelled'\r\n\tWHERE posts.post_type = 'shop_order'\r\n\tAND posts.post_status = 'publish'\r\n\tAND tax.taxonomy = 'shop_order_status'\r\n\tAND\tterm.slug LIKE 'cancelled%';\r\n\t"); $wpdb->query("\r\n\tUPDATE {$wpdb->posts} as posts\r\n\tLEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID\r\n\tLEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )\r\n\tLEFT JOIN {$wpdb->terms} AS term USING( term_id )\r\n\tSET posts.post_status = 'wc-refunded'\r\n\tWHERE posts.post_type = 'shop_order'\r\n\tAND posts.post_status = 'publish'\r\n\tAND tax.taxonomy = 'shop_order_status'\r\n\tAND\tterm.slug LIKE 'refunded%';\r\n\t"); $wpdb->query("\r\n\tUPDATE {$wpdb->posts} as posts\r\n\tLEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID\r\n\tLEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )\r\n\tLEFT JOIN {$wpdb->terms} AS term USING( term_id )\r\n\tSET posts.post_status = 'wc-failed'\r\n\tWHERE posts.post_type = 'shop_order'\r\n\tAND posts.post_status = 'publish'\r\n\tAND tax.taxonomy = 'shop_order_status'\r\n\tAND\tterm.slug LIKE 'failed%';\r\n\t"); // Update variations which manage stock $update_variations = $wpdb->get_results("\r\n\tSELECT DISTINCT posts.ID AS variation_id, posts.post_parent AS variation_parent FROM {$wpdb->posts} as posts\r\n\tLEFT OUTER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id AND postmeta.meta_key = '_stock'\r\n\tLEFT OUTER JOIN {$wpdb->postmeta} as postmeta2 ON posts.ID = postmeta2.post_id AND postmeta2.meta_key = '_manage_stock'\r\n\tWHERE posts.post_type = 'product_variation'\r\n\tAND postmeta.meta_value IS NOT NULL\r\n\tAND postmeta.meta_value != ''\r\n\tAND postmeta2.meta_value IS NULL\r\n"); foreach ($update_variations as $variation) { $parent_backorders = get_post_meta($variation->variation_parent, '_backorders', true); add_post_meta($variation->variation_id, '_manage_stock', 'yes', true); add_post_meta($variation->variation_id, '_backorders', $parent_backorders ? $parent_backorders : 'no', true); } // Update taxonomy names with correct sanitized names $attribute_taxonomies = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies"); foreach ($attribute_taxonomies as $attribute_taxonomy) { $sanitized_attribute_name = wc_sanitize_taxonomy_name($attribute_taxonomy->attribute_name); if ($sanitized_attribute_name !== $attribute_taxonomy->attribute_name) { if (!$wpdb->get_var($wpdb->prepare("SELECT 1 FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $sanitized_attribute_name))) { // Update attribute $wpdb->update("{$wpdb->prefix}woocommerce_attribute_taxonomies", array('attribute_name' => $sanitized_attribute_name), array('attribute_id' => $attribute_taxonomy->attribute_id)); // Update terms $wpdb->update($wpdb->term_taxonomy, array('taxonomy' => wc_attribute_taxonomy_name($sanitized_attribute_name)), array('taxonomy' => 'pa_' . $attribute_taxonomy->attribute_name)); } } } // add webhook capabilities to shop_manager/administrator role global $wp_roles; if (class_exists('WP_Roles')) { if (!isset($wp_roles)) { $wp_roles = new WP_Roles(); }
/** * @deprecated */ function woocommerce_sanitize_taxonomy_name($taxonomy) { return wc_sanitize_taxonomy_name($taxonomy); }
/** * Edit an attribute * @return bool|WP_Error */ private static function process_edit_attribute() { global $wpdb; $attribute_id = absint($_GET['edit']); check_admin_referer('woocommerce-save-attribute_' . $attribute_id); $attribute = self::get_posted_attribute(); if (empty($attribute['attribute_name']) || empty($attribute['attribute_label'])) { return new WP_Error('error', __('Please, provide an attribute name and slug.', 'woocommerce')); } elseif (($valid_attribute_name = self::valid_attribute_name($attribute['attribute_name'])) && is_wp_error($valid_attribute_name)) { return $valid_attribute_name; } $taxonomy_exists = taxonomy_exists(wc_attribute_taxonomy_name($attribute['attribute_name'])); $old_attribute_name = $wpdb->get_var("SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = {$attribute_id}"); if ($old_attribute_name != $attribute['attribute_name'] && wc_sanitize_taxonomy_name($old_attribute_name) != $attribute['attribute_name'] && $taxonomy_exists) { return new WP_Error('error', sprintf(__('Slug "%s" is already in use. Change it, please.', 'woocommerce'), sanitize_title($attribute['attribute_name']))); } $wpdb->update($wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute, array('attribute_id' => $attribute_id)); do_action('woocommerce_attribute_updated', $attribute_id, $attribute, $old_attribute_name); if ($old_attribute_name != $attribute['attribute_name'] && !empty($old_attribute_name)) { // Update taxonomies in the wp term taxonomy table $wpdb->update($wpdb->term_taxonomy, array('taxonomy' => wc_attribute_taxonomy_name($attribute['attribute_name'])), array('taxonomy' => 'pa_' . $old_attribute_name)); // Update taxonomy ordering term meta $wpdb->update($wpdb->prefix . 'woocommerce_termmeta', array('meta_key' => 'order_pa_' . sanitize_title($attribute['attribute_name'])), array('meta_key' => 'order_pa_' . sanitize_title($old_attribute_name))); // Update product attributes which use this taxonomy $old_attribute_name_length = strlen($old_attribute_name) + 3; $attribute_name_length = strlen($attribute['attribute_name']) + 3; $wpdb->query($wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE( meta_value, %s, %s ) WHERE meta_key = '_product_attributes'", 's:' . $old_attribute_name_length . ':"pa_' . $old_attribute_name . '"', 's:' . $attribute_name_length . ':"pa_' . $attribute['attribute_name'] . '"')); // Update variations which use this taxonomy $wpdb->update($wpdb->postmeta, array('meta_key' => 'attribute_pa_' . sanitize_title($attribute['attribute_name'])), array('meta_key' => 'attribute_pa_' . sanitize_title($old_attribute_name))); } echo '<div class="updated"><p>' . __('Attribute updated successfully', 'woocommerce') . '</p></div>'; flush_rewrite_rules(); delete_transient('wc_attribute_taxonomies'); return true; }
/** * Get the full name for a order/subscription line item, including the items non hidden meta * (i.e. attributes), as a flat string. * * @param array * @return string */ function wcs_get_line_item_name($line_item) { $item_meta_strings = array(); foreach ($line_item['item_meta'] as $meta_key => $meta_value) { $meta_value = $meta_value[0]; // Skip hidden core fields if (in_array($meta_key, apply_filters('woocommerce_hidden_order_itemmeta', array('_qty', '_tax_class', '_product_id', '_variation_id', '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax', '_line_tax_data')))) { continue; } // Skip serialised meta if (is_serialized($meta_value)) { continue; } // Get attribute data if (taxonomy_exists(wc_sanitize_taxonomy_name($meta_key))) { $term = get_term_by('slug', $meta_value, wc_sanitize_taxonomy_name($meta_key)); $meta_key = wc_attribute_label(wc_sanitize_taxonomy_name($meta_key)); $meta_value = isset($term->name) ? $term->name : $meta_value; } else { $meta_key = apply_filters('woocommerce_attribute_label', wc_attribute_label($meta_key), $meta_key); } $item_meta_strings[] = sprintf('%s: %s', rawurldecode($meta_key), rawurldecode($meta_value)); } if (!empty($item_meta_strings)) { $line_item_name = sprintf('%s (%s)', $line_item['name'], implode(', ', $item_meta_strings)); } else { $line_item_name = $line_item['name']; } return apply_filters('wcs_line_item_name', $line_item_name, $line_item); }
<?php foreach ($product_attributes as $attribute) { ?> <tr class="dokan-attribute-options"> <td width="20%"> <?php if ($attribute['is_taxonomy']) { ?> <?php $tax = get_taxonomy($attribute['name']); ?> <input type="text" disabled="disabled" value="<?php echo $tax->label; ?> " class="dokan-form-control dokan-attribute-option-name-label" data-attribute_name="<?php echo wc_sanitize_taxonomy_name(str_replace('pa_', '', $attribute['name'])); ?> "> <input type="hidden" name="attribute_names[]" value="<?php echo esc_attr($attribute['name']); ?> " class="dokan-attribute-option-name"> <?php } else { ?> <input type="text" name="attribute_names[]" value="<?php echo $attribute['name']; ?> " class="dokan-form-control dokan-attribute-option-name"> <?php }
/** * Add Predefined Attribute * * @since 2.3 * * @return json success|$content (array) */ function add_predefined_attribute() { $attr_name = $_POST['name']; $single = isset($_POST['from']) && $_POST['from'] == 'popup' ? 'single-' : ''; $remove_btn = isset($_POST['from']) && $_POST['from'] == 'popup' ? 'single_' : ''; $attribute_taxonomy_name = wc_attribute_taxonomy_name($attr_name); $tax = get_taxonomy($attribute_taxonomy_name); $options = get_terms($attribute_taxonomy_name, 'orderby=name&hide_empty=0'); $att_val = wp_list_pluck($options, 'name'); ob_start(); ?> <tr class="dokan-<?php echo $single; ?> attribute-options"> <td width="20%"> <input type="text" disabled="disabled" value="<?php echo $attr_name; ?> " class="dokan-form-control dokan-<?php echo $single; ?> attribute-option-name-label" data-attribute_name="<?php echo wc_sanitize_taxonomy_name(str_replace('pa_', '', $attribute_taxonomy_name)); ?> "> <input type="hidden" name="attribute_names[]" value="<?php echo esc_attr($attribute_taxonomy_name); ?> " class="dokan-<?php echo $single; ?> attribute-option-name"> <input type="hidden" name="attribute_is_taxonomy[]" value="1"> </td> <td colspan="3"><input type="text" name="attribute_values[]" value="<?php echo implode(',', $att_val); ?> " class="dokan-form-control dokan-<?php echo $single; ?> attribute-option-values"></td> <td><button class="dokan-btn dokan-btn-theme remove_<?php echo $remove_btn; ?> attribute"><i class="fa fa-trash-o"></i></button></td> </tr> <?php $content = ob_get_clean(); wp_send_json_success($content); }
echo $product->get_title(); global $wpdb; if ($metadata = $this->order->has_meta($item_id)) { foreach ($metadata as $meta) { // Skip hidden core fields if (in_array($meta['meta_key'], apply_filters('woocommerce_hidden_order_itemmeta', array('_qty', '_tax_class', '_product_id', '_variation_id', '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax')))) { continue; } // Skip serialised meta if (is_serialized($meta['meta_value'])) { continue; } // Get attribute data if (taxonomy_exists(wc_sanitize_taxonomy_name($meta['meta_key']))) { $term = get_term_by('slug', $meta['meta_value'], wc_sanitize_taxonomy_name($meta['meta_key'])); $meta['meta_key'] = wc_attribute_label(wc_sanitize_taxonomy_name($meta['meta_key'])); $meta['meta_value'] = isset($term->name) ? $term->name : $meta['meta_value']; } else { $meta['meta_key'] = apply_filters('woocommerce_attribute_label', wc_attribute_label($meta['meta_key'], $product), $meta['meta_key']); } echo '<div class="item-attribute"><span style="font-weight: bold;">' . wp_kses_post(rawurldecode($meta['meta_key'])) . ': </span>' . wp_kses_post(rawurldecode($meta['meta_value'])) . '</div>'; } } ?> </td> <?php if ($this->template_options['bewpi_show_sku']) { echo '<td>'; echo $product->get_sku() != '' ? $product->get_sku() : '-'; echo '</td>'; }
global $wpdb; if ($metadata = $order->has_meta($item_id)) { echo '<table cellspacing="0" class="display_meta">'; foreach ($metadata as $meta) { // Skip hidden core fields if (in_array($meta['meta_key'], apply_filters('woocommerce_hidden_order_itemmeta', array('_qty', '_tax_class', '_product_id', '_variation_id', '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax')))) { continue; } // Skip serialised meta if (is_serialized($meta['meta_value'])) { continue; } // Get attribute data if (taxonomy_exists(wc_sanitize_taxonomy_name($meta['meta_key']))) { $term = get_term_by('slug', $meta['meta_value'], wc_sanitize_taxonomy_name($meta['meta_key'])); $attribute_name = str_replace('pa_', '', wc_sanitize_taxonomy_name($meta['meta_key'])); $attribute = $wpdb->get_var($wpdb->prepare("\r\n\t\t\t\t\t\t\t\t\t\tSELECT attribute_label\r\n\t\t\t\t\t\t\t\t\t\tFROM {$wpdb->prefix}woocommerce_attribute_taxonomies\r\n\t\t\t\t\t\t\t\t\t\tWHERE attribute_name = %s;\r\n\t\t\t\t\t\t\t\t\t", $attribute_name)); $meta['meta_key'] = !is_wp_error($attribute) && $attribute ? $attribute : $attribute_name; $meta['meta_value'] = isset($term->name) ? $term->name : $meta['meta_value']; } echo '<tr><th>' . wp_kses_post(rawurldecode($meta['meta_key'])) . ':</th><td>' . wp_kses_post(wpautop(make_clickable(rawurldecode($meta['meta_value'])))) . '</td></tr>'; } echo '</table>'; } ?> </div> <div class="edit" style="display: none;"> <table class="meta" cellspacing="0"> <tbody class="meta_items"> <?php if ($metadata = $order->has_meta($item_id)) {
<?php if (!$attr_dropdown && is_array($options)) { ?> <div class="ts-product-attribute"> <?php if (isset($_REQUEST['attribute_' . sanitize_title($attribute_name)])) { $selected_value = $_REQUEST['attribute_' . sanitize_title($attribute_name)]; } elseif (isset($selected_attributes[sanitize_title($attribute_name)])) { $selected_value = $selected_attributes[sanitize_title($attribute_name)]; } else { $selected_value = ''; } // Get terms if this is a taxonomy - ordered if (taxonomy_exists($attribute_name)) { $is_attr_color = false; $attribute_color = wc_sanitize_taxonomy_name('color'); if ($attribute_name == wc_attribute_taxonomy_name($attribute_color)) { $is_attr_color = true; } $terms = wc_get_product_terms($post->ID, $attribute_name, array('fields' => 'all')); foreach ($terms as $term) { if (!in_array($term->slug, $options)) { continue; } if ($is_attr_color) { $datas = get_metadata('woocommerce_term', $term->term_id, 'ts_product_color_config', true); if (strlen($datas) > 0) { $datas = unserialize($datas); } else { $datas = array('ts_color_color' => "#ffffff", 'ts_color_image' => 0); }
function porto_woocommerce_term_metadata_ajax($value, $object_id, $meta_key, $single) { if ($meta_key === 'display_type') { $params = array('count', 'orderby', 'min_price', 'max_price'); foreach ($params as $param) { if (!empty($_GET[$param])) { return 'products'; } } $attribute_taxonomies = wc_get_attribute_taxonomies(); if ($attribute_taxonomies) { foreach ($attribute_taxonomies as $tax) { $attribute = wc_sanitize_taxonomy_name($tax->attribute_name); $taxonomy = wc_attribute_taxonomy_name($attribute); $name = 'filter_' . $attribute; if (!empty($_GET[$name]) && taxonomy_exists($taxonomy)) { return 'products'; } } } $pagenum = get_query_var('paged') ? intval(get_query_var('paged')) : 0; if ($pagenum) { return 'products'; } } return $value; }
function create_taxonomy($attr_name, $logger) { global $woocommerce; if (!taxonomy_exists(wc_attribute_taxonomy_name($attr_name))) { // Grab the submitted data $attribute_name = isset($attr_name) ? wc_sanitize_taxonomy_name(stripslashes((string) $attr_name)) : ''; $attribute_label = ucwords(stripslashes((string) $attr_name)); $attribute_type = 'select'; $attribute_orderby = 'menu_order'; // if ( in_array( $attribute_name, $this->reserved_terms ) ) { // $attribute_name .= 's'; // } if (in_array($attribute_name, $this->reserved_terms)) { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Slug “%s” is not allowed because it is a reserved term. Change it, please.', 'pmxi_plugin'), wc_attribute_taxonomy_name($attribute_name))); } else { // Register the taxonomy now so that the import works! $domain = wc_attribute_taxonomy_name($attr_name); if (strlen($domain) <= 32) { $this->wpdb->insert($this->wpdb->prefix . 'woocommerce_attribute_taxonomies', array('attribute_label' => $attribute_label, 'attribute_name' => $attribute_name, 'attribute_type' => $attribute_type, 'attribute_orderby' => $attribute_orderby)); register_taxonomy($domain, apply_filters('woocommerce_taxonomy_objects_' . $domain, array('product')), apply_filters('woocommerce_taxonomy_args_' . $domain, array('hierarchical' => true, 'show_ui' => false, 'query_var' => true, 'rewrite' => false))); delete_transient('wc_attribute_taxonomies'); $attribute_taxonomies = $this->wpdb->get_results("SELECT * FROM " . $this->wpdb->prefix . "woocommerce_attribute_taxonomies"); set_transient('wc_attribute_taxonomies', $attribute_taxonomies); apply_filters('woocommerce_attribute_taxonomies', $attribute_taxonomies); $logger and call_user_func($logger, sprintf(__('- <b>CREATED</b>: Taxonomy attribute “%s” have been successfully created.', 'pmxi_plugin'), wc_attribute_taxonomy_name($attribute_name))); } else { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Taxonomy “%s” name is more than 32 characters. Change it, please.', 'pmxi_plugin'), $attr_name)); } } } }
function wc_update_220_attributes() { global $wpdb; // Update taxonomy names with correct sanitized names $attribute_taxonomies = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies"); foreach ($attribute_taxonomies as $attribute_taxonomy) { $sanitized_attribute_name = wc_sanitize_taxonomy_name($attribute_taxonomy->attribute_name); if ($sanitized_attribute_name !== $attribute_taxonomy->attribute_name) { if (!$wpdb->get_var($wpdb->prepare("SELECT 1=1 FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $sanitized_attribute_name))) { // Update attribute $wpdb->update("{$wpdb->prefix}woocommerce_attribute_taxonomies", array('attribute_name' => $sanitized_attribute_name), array('attribute_id' => $attribute_taxonomy->attribute_id)); // Update terms $wpdb->update($wpdb->term_taxonomy, array('taxonomy' => wc_attribute_taxonomy_name($sanitized_attribute_name)), array('taxonomy' => 'pa_' . $attribute_taxonomy->attribute_name)); } } } }
/** * Update a single term from a taxonomy. * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Request|WP_Error */ public function update_item($request) { global $wpdb; $id = (int) $request['id']; $format = array('%s', '%s', '%s', '%s', '%d'); $args = array('attribute_label' => $request['name'], 'attribute_name' => $request['slug'], 'attribute_type' => $request['type'], 'attribute_orderby' => $request['order_by'], 'attribute_public' => $request['has_archives']); $i = 0; foreach ($args as $key => $value) { if (empty($value) && !is_bool($value)) { unset($args[$key]); unset($format[$i]); } $i++; } // Set the attribute slug. if (!empty($args['attribute_name'])) { $args['attribute_name'] = preg_replace('/^pa\\_/', '', wc_sanitize_taxonomy_name(stripslashes($args['attribute_name']))); $valid_slug = $this->validate_attribute_slug($args['attribute_name'], false); if (is_wp_error($valid_slug)) { return $valid_slug; } } $update = $wpdb->update($wpdb->prefix . 'woocommerce_attribute_taxonomies', $args, array('attribute_id' => $id), $format, array('%d')); // Checks for errors. if (false === $update) { return new WP_Error('woocommerce_rest_cannot_edit', __('Could not edit the attribute.', 'woocommerce'), array('status' => 400)); } $attribute = $this->get_attribute($id); if (is_wp_error($attribute)) { return $attribute; } $this->update_additional_fields_for_object($attribute, $request); /** * Fires after a single product attribute is created or updated via the REST API. * * @param stdObject $attribute Inserted attribute object. * @param WP_REST_Request $request Request object. * @param boolean $creating True when creating attribute, false when updating. */ do_action('woocommerce_rest_insert_product_attribute', $attribute, $request, false); $request->set_param('context', 'edit'); $response = $this->prepare_item_for_response($attribute, $request); // Clear transients. flush_rewrite_rules(); delete_transient('wc_attribute_taxonomies'); return rest_ensure_response($response); }
/** * Add group admin panel * * Shows the interface for adding new groups */ public static function add_group() { if (wc_crm_notice_count('error') == 0) { $_POST = array(); } // Grab the submitted data $group_name = isset($_POST['group_name']) ? (string) stripslashes($_POST['group_name']) : ''; $group_slug = isset($_POST['group_slug']) ? wc_sanitize_taxonomy_name(stripslashes((string) $_POST['group_slug'])) : ''; $group_type = isset($_POST['group_type']) ? (string) stripslashes($_POST['group_type']) : ''; $group_total_spent_mark = isset($_POST['group_total_spent_mark']) ? (string) stripslashes($_POST['group_total_spent_mark']) : ''; $group_total_spent = isset($_POST['group_total_spent']) ? (string) stripslashes($_POST['group_total_spent']) : ''; $group_user_role = isset($_POST['group_user_role']) ? (string) stripslashes($_POST['group_user_role']) : ''; $group_customer_status = isset($_POST['group_customer_status']) ? $_POST['group_customer_status'] : array(); $group_product_categories = isset($_POST['group_product_categories']) ? $_POST['group_product_categories'] : array(); $group_order_status = isset($_POST['group_order_status']) ? $_POST['group_order_status'] : array(); $group_last_order = isset($_POST['group_last_order']) ? (string) stripslashes($_POST['group_last_order']) : ''; $group_last_order_from = isset($_POST['group_last_order_from']) ? (string) stripslashes($_POST['group_last_order_from']) : ''; $group_last_order_to = isset($_POST['group_last_order_to']) ? (string) stripslashes($_POST['group_last_order_to']) : ''; ?> <div class="wrap woocommerce"> <div class="icon32 icon32-groups" id="icon-woocommerce"><br/></div> <h2><?php _e('Customer Groups', 'woocommerce'); ?> </h2> <?php wc_crm_print_notices(); ?> <br class="clear" /> <div id="col-container"> <div id="col-right"> <div class="col-wrap"> <form action="admin.php?page=wc_crm-groups" method="post"> <?php WC_CRM()->tables['groups']->prepare_items(); WC_CRM()->tables['groups']->display(); ?> </form> </div> </div> <div id="col-left"> <div class="col-wrap"> <div class="form-wrap"> <h3><?php _e('Add New group', 'wc_crm'); ?> </h3> <p><?php _e('Groups are used to organise your customers. Please Note: you cannot rename a group later.', 'wc_crm'); ?> </p> <form action="admin.php?page=wc_crm-groups" method="post" style=" padding-bottom: 150px;"> <div class="form-field"> <label for="f_group_name"><?php _e('Name', 'woocommerce'); ?> </label> <input name="group_name" id="f_group_name" type="text" value="<?php echo $group_name; ?> " /> <p class="description"><?php _e('Name for the group.', 'wc_crm'); ?> </p> </div> <div class="form-field"> <label for="f_group_slug"><?php _e('Slug', 'woocommerce'); ?> </label> <input name="group_slug" id="f_group_slug" type="text" value="<?php echo $group_slug; ?> " maxlength="28" /> <p class="description"><?php _e('Unique slug/reference for the group; must be shorter than 28 characters.', 'wc_crm'); ?> </p> </div> <div class="form-field"> <label for="f_group_type"><?php _e('Type', 'wc_crm'); ?> </label> <select name="group_type" id="f_group_type"> <option value="dynamic" <?php selected($group_type, 'dynamic'); ?> ><?php _e('Dynamic', 'wc_crm'); ?> </option> <option value="static" <?php selected($group_type, 'static'); ?> ><?php _e('Static', 'wc_crm'); ?> </option> <?php do_action('wc_crm_customer_group_types'); ?> </select> <p class="description"><?php _e('Determines how you select group for customers.', 'wc_crm'); ?> </p> </div> <div class="form-field dynamic_group_type"> <label for="group_total_spent"><?php _e('Total Spent', 'wc_crm'); ?> </label> <select name="group_total_spent_mark" id="group_total_spent_mark"> <option value="equal" <?php selected($group_total_spent_mark, 'equal'); ?> ><?php _e('=', 'wc_crm'); ?> </option> <option value="greater" <?php selected($group_total_spent_mark, 'greater'); ?> ><?php _e('>', 'wc_crm'); ?> </option> <option value="less" <?php selected($group_total_spent_mark, 'less'); ?> ><?php _e('<', 'wc_crm'); ?> </option> <option value="greater_or_equal" <?php selected($group_total_spent_mark, 'greater_or_equal'); ?> ><?php _e('≥', 'wc_crm'); ?> </option> <option value="less_or_equal" <?php selected($group_total_spent_mark, 'less_or_equal'); ?> ><?php _e('≤', 'wc_crm'); ?> </option> </select> <input type="number" step="any" id="group_total_spent" name="group_total_spent" value="<?php echo $group_total_spent; ?> "> </div> <div class="form-field dynamic_group_type"> <label for="group_user_role"><?php _e('User Role', 'wc_crm'); ?> </label> <select name="group_user_role" id="group_user_role" class="wc-enhanced-select"> <option value="any"> <?php _e('Any', 'wc_crm'); ?> </option> <option value="guest"> <?php _e('Guest', 'wc_crm'); ?> </option> <?php global $wp_roles; foreach ($wp_roles->role_names as $role => $name) { ?> <option value="<?php echo strtolower($name); ?> " <?php selected($group_user_role, strtolower($name)); ?> > <?php _e($name, 'wc_crm'); ?> </option> <?php } ?> </select> </div> <div class="form-field dynamic_group_type"> <label for="group_customer_status"><?php _e('Customer Status', 'wc_crm'); ?> </label> <select name="group_customer_status[]" id="group_customer_status" multiple="multiple" data-placeholder="<?php _e('Choose a Customer Status...', 'wc_crm'); ?> " class="wc-enhanced-select"> <?php $statuses = wc_crm_get_statuses(); foreach ($statuses as $status) { ?> <option value="<?php echo strtolower($status->status_slug); ?> " <?php echo in_array(strtolower($status->status_slug), $group_customer_status) ? 'selected="selected"' : ''; ?> > <?php echo $status->status_name; ?> </option> <?php } ?> </select> </div> <div class="form-field dynamic_group_type"> <label for="group_product_categories"><?php _e('Product Category', 'wc_crm'); ?> </label> <select name="group_product_categories[]" id="group_product_categories" multiple="multiple" data-placeholder="<?php _e('Choose a Product Category...', 'wc_crm'); ?> " class="wc-enhanced-select"> <?php $all_cat = get_terms(array('product_cat'), array('orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false)); if (!empty($all_cat)) { foreach ($all_cat as $cat) { ?> <option value="<?php echo $cat->term_id; ?> " <?php echo in_array($cat->term_id, $group_product_categories) ? 'selected="selected"' : ''; ?> > <?php echo $cat->name; ?> </option> <?php } } ?> </select> </div> <div class="form-field dynamic_group_type"> <label for="group_order_status"><?php _e('Order Status', 'wc_crm'); ?> </label> <select name="group_order_status[]" id="group_order_status" multiple="multiple" data-placeholder="<?php _e('Choose a Product Category...', 'wc_crm'); ?> " class="wc-enhanced-select"> <?php $wc_statuses = wc_get_order_statuses(); if (!empty($wc_statuses)) { foreach ($wc_statuses as $key => $status_name) { ?> <option value="<?php echo $key; ?> " <?php echo in_array($key, $group_order_status) ? 'selected="selected"' : ''; ?> > <?php echo $status_name; ?> </option> <?php } } ?> </select> </div> <div class="form-field dynamic_group_type"> <label for="group_last_order"><?php _e('Last Order', 'wc_crm'); ?> </label> <div class="wrap_date"> <select name="group_last_order" id="group_last_order"> <option value="between" <?php selected($group_last_order, 'between'); ?> ><?php _e('Between', 'wc_crm'); ?> </option> <option value="before" <?php selected($group_last_order, 'before'); ?> ><?php _e('Before', 'wc_crm'); ?> </option> <option value="after" <?php selected($group_last_order, 'after'); ?> ><?php _e('After', 'wc_crm'); ?> </option> </select> </div> <div class="wrap_date"> <input type="text" id="group_last_order_from" name="group_last_order_from" value="<?php echo $group_last_order_from; ?> "> <i class="ico_calendar"></i> </div> <div class="wrap_date group_last_order_between" style="height: 30px; line-height: 30px; padding: 0 10px;"> to </div> <div class="wrap_date group_last_order_between"> <input type="text" id="group_last_order_to" name="group_last_order_to" value="<?php echo $group_last_order_to; ?> "> <i class="ico_calendar"></i> </div> <div class="clear"></div> </div> <p class="submit"><input type="submit" name="wc_crm_add_new_group" id="submit" class="button" value="<?php _e('Add group', 'wc_crm'); ?> "></p> <?php wp_nonce_field('wc-crm-add-new-group'); ?> </form> </div> </div> </div> </div> <script type="text/javascript"> /* <![CDATA[ */ jQuery('a.delete').click(function(){ var answer = confirm ("<?php _e('Are you sure you want to delete this group?', 'wc_crm'); ?> "); if (answer) return true; return false; }); /* ]]> */ </script> </div> <?php }
/** * Handles output of the attributes page in admin. * * Shows the created attributes and lets you add new ones or edit existing ones. * The added attributes are stored in the database and can be used for layered navigation. */ public static function output() { global $wpdb; // Action to perform: add, edit, delete or none $action = ''; if (!empty($_POST['add_new_attribute'])) { $action = 'add'; } elseif (!empty($_POST['save_attribute']) && !empty($_GET['edit'])) { $action = 'edit'; } elseif (!empty($_GET['delete'])) { $action = 'delete'; } // Add or edit an attribute if ('add' === $action || 'edit' === $action) { // Security check if ('add' === $action) { check_admin_referer('woocommerce-add-new_attribute'); } if ('edit' === $action) { $attribute_id = absint($_GET['edit']); check_admin_referer('woocommerce-save-attribute_' . $attribute_id); } // Grab the submitted data $attribute_label = isset($_POST['attribute_label']) ? (string) stripslashes($_POST['attribute_label']) : ''; $attribute_name = isset($_POST['attribute_name']) ? wc_sanitize_taxonomy_name(stripslashes((string) $_POST['attribute_name'])) : ''; $attribute_type = isset($_POST['attribute_type']) ? (string) stripslashes($_POST['attribute_type']) : ''; $attribute_orderby = isset($_POST['attribute_orderby']) ? (string) stripslashes($_POST['attribute_orderby']) : ''; // Auto-generate the label or slug if only one of both was provided if (!$attribute_label) { $attribute_label = ucfirst($attribute_name); } if (!$attribute_name) { $attribute_name = wc_sanitize_taxonomy_name(stripslashes($attribute_label)); } // Forbidden attribute names // http://codex.wordpress.org/Function_Reference/register_taxonomy#Reserved_Terms $reserved_terms = array('attachment', 'attachment_id', 'author', 'author_name', 'calendar', 'cat', 'category', 'category__and', 'category__in', 'category__not_in', 'category_name', 'comments_per_page', 'comments_popup', 'cpage', 'day', 'debug', 'error', 'exact', 'feed', 'hour', 'link_category', 'm', 'minute', 'monthnum', 'more', 'name', 'nav_menu', 'nopaging', 'offset', 'order', 'orderby', 'p', 'page', 'page_id', 'paged', 'pagename', 'pb', 'perm', 'post', 'post__in', 'post__not_in', 'post_format', 'post_mime_type', 'post_status', 'post_tag', 'post_type', 'posts', 'posts_per_archive_page', 'posts_per_page', 'preview', 'robots', 's', 'search', 'second', 'sentence', 'showposts', 'static', 'subpost', 'subpost_id', 'tag', 'tag__and', 'tag__in', 'tag__not_in', 'tag_id', 'tag_slug__and', 'tag_slug__in', 'taxonomy', 'tb', 'term', 'type', 'w', 'withcomments', 'withoutcomments', 'year'); // Error checking if (!$attribute_name || !$attribute_label || !$attribute_type) { $error = __('Please, provide an attribute name, slug and type.', 'woocommerce'); } elseif (strlen($attribute_name) >= 28) { $error = sprintf(__('Slug “%s” is too long (28 characters max). Shorten it, please.', 'woocommerce'), sanitize_title($attribute_name)); } elseif (in_array($attribute_name, $reserved_terms)) { $error = sprintf(__('Slug “%s” is not allowed because it is a reserved term. Change it, please.', 'woocommerce'), sanitize_title($attribute_name)); } else { $taxonomy_exists = taxonomy_exists(wc_attribute_taxonomy_name($attribute_name)); if ('add' === $action && $taxonomy_exists) { $error = sprintf(__('Slug “%s” is already in use. Change it, please.', 'woocommerce'), sanitize_title($attribute_name)); } if ('edit' === $action) { $old_attribute_name = $wpdb->get_var("SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = {$attribute_id}"); if ($old_attribute_name != $attribute_name && wc_sanitize_taxonomy_name($old_attribute_name) != $attribute_name && $taxonomy_exists) { $error = sprintf(__('Slug “%s” is already in use. Change it, please.', 'woocommerce'), sanitize_title($attribute_name)); } } } // Show the error message if any if (!empty($error)) { echo '<div id="woocommerce_errors" class="error fade"><p>' . $error . '</p></div>'; } else { // Add new attribute if ('add' === $action) { $attribute = array('attribute_label' => $attribute_label, 'attribute_name' => $attribute_name, 'attribute_type' => $attribute_type, 'attribute_orderby' => $attribute_orderby); $wpdb->insert($wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute); do_action('woocommerce_attribute_added', $wpdb->insert_id, $attribute); $action_completed = true; } // Edit existing attribute if ('edit' === $action) { $attribute = array('attribute_label' => $attribute_label, 'attribute_name' => $attribute_name, 'attribute_type' => $attribute_type, 'attribute_orderby' => $attribute_orderby); $wpdb->update($wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute, array('attribute_id' => $attribute_id)); do_action('woocommerce_attribute_updated', $attribute_id, $attribute, $old_attribute_name); if ($old_attribute_name != $attribute_name && !empty($old_attribute_name)) { // Update taxonomies in the wp term taxonomy table $wpdb->update($wpdb->term_taxonomy, array('taxonomy' => wc_attribute_taxonomy_name($attribute_name)), array('taxonomy' => 'pa_' . $old_attribute_name)); // Update taxonomy ordering term meta $wpdb->update($wpdb->prefix . 'woocommerce_termmeta', array('meta_key' => 'order_pa_' . sanitize_title($attribute_name)), array('meta_key' => 'order_pa_' . sanitize_title($old_attribute_name))); // Update product attributes which use this taxonomy $old_attribute_name_length = strlen($old_attribute_name) + 3; $attribute_name_length = strlen($attribute_name) + 3; $wpdb->query("\r\n\t\t\t\t\t\t\tUPDATE {$wpdb->postmeta}\r\n\t\t\t\t\t\t\tSET meta_value = REPLACE( meta_value, 's:{$old_attribute_name_length}:\"pa_{$old_attribute_name}\"', 's:{$attribute_name_length}:\"pa_{$attribute_name}\"' )\r\n\t\t\t\t\t\t\tWHERE meta_key = '_product_attributes'"); // Update variations which use this taxonomy $wpdb->update($wpdb->postmeta, array('meta_key' => 'attribute_pa_' . sanitize_title($attribute_name)), array('meta_key' => 'attribute_pa_' . sanitize_title($old_attribute_name))); } $action_completed = true; } flush_rewrite_rules(); } } // Delete an attribute if ('delete' === $action) { // Security check $attribute_id = absint($_GET['delete']); check_admin_referer('woocommerce-delete-attribute_' . $attribute_id); $attribute_name = $wpdb->get_var("SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = {$attribute_id}"); if ($attribute_name && $wpdb->query("DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = {$attribute_id}")) { $taxonomy = wc_attribute_taxonomy_name($attribute_name); if (taxonomy_exists($taxonomy)) { $terms = get_terms($taxonomy, 'orderby=name&hide_empty=0'); foreach ($terms as $term) { wp_delete_term($term->term_id, $taxonomy); } } do_action('woocommerce_attribute_deleted', $attribute_id, $attribute_name, $taxonomy); $action_completed = true; } } // If an attribute was added, edited or deleted: clear cache if (!empty($action_completed)) { delete_transient('wc_attribute_taxonomies'); } // Show admin interface if (!empty($_GET['edit'])) { self::edit_attribute(); } else { self::add_attribute(); } }
/** * Creates Custom Attribute for woocommerce. * @param unknown $lable * @param string $name * @param string $type * @param string $orderby * @return boolean */ public function createAttribute($lable, $name = '', $type = 'select', $orderby = 'menu_order') { global $wpdb, $permalinks; if ($this->attribute_exist($lable)) { $this->created_attribute[$lable] = wc_attribute_taxonomy_name($lable); return true; } else { $attribute_label = stripslashes($lable); $attribute_name = ''; $attribute_type = 'select'; $attribute_orderby = 'menu_order'; if (!$attribute_label) { $attribute_label = ucfirst($attribute_name); } if (!$attribute_name) { $attribute_name = wc_sanitize_taxonomy_name(stripslashes($attribute_label)); } $attribute = array('attribute_label' => $attribute_label, 'attribute_name' => $attribute_name, 'attribute_type' => $attribute_type, 'attribute_orderby' => $attribute_orderby); $wpdb->insert($wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute); $transient_name = 'wc_attribute_taxonomies'; $attribute_taxonomies = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies"); set_transient($transient_name, $attribute_taxonomies); $this->created_attribute[$lable] = wc_attribute_taxonomy_name($attribute_name); do_action('woocommerce_attribute_added', $wpdb->insert_id, $attribute); $action_completed = true; $name = wc_attribute_taxonomy_name($lable); register_taxonomy($name, 'product', array('hierarchical' => true, 'update_count_callback' => '_update_post_term_count', 'labels' => array('name' => $label, 'singular_name' => $label, 'search_items' => sprintf(__('Search %s', 'woocommerce'), $label), 'all_items' => sprintf(__('All %s', 'woocommerce'), $label), 'parent_item' => sprintf(__('Parent %s', 'woocommerce'), $label), 'parent_item_colon' => sprintf(__('Parent %s:', 'woocommerce'), $label), 'edit_item' => sprintf(__('Edit %s', 'woocommerce'), $label), 'update_item' => sprintf(__('Update %s', 'woocommerce'), $label), 'add_new_item' => sprintf(__('Add New %s', 'woocommerce'), $label), 'new_item_name' => sprintf(__('New %s', 'woocommerce'), $label)), 'show_ui' => false, 'query_var' => true, 'capabilities' => array('manage_terms' => 'manage_product_terms', 'edit_terms' => 'edit_product_terms', 'delete_terms' => 'delete_product_terms', 'assign_terms' => 'assign_product_terms'), 'show_in_nav_menus' => apply_filters('woocommerce_attribute_show_in_nav_menus', false, $name), 'rewrite' => array('slug' => (empty($permalinks['attribute_base']) ? '' : trailingslashit($permalinks['attribute_base'])) . sanitize_title($name), 'with_front' => false, 'hierarchical' => true))); return true; } }
function create_taxonomy($attr_name, $logger) { global $woocommerce; if (!taxonomy_exists(wc_attribute_taxonomy_name($attr_name))) { // Grab the submitted data $attribute_name = isset($attr_name) ? wc_sanitize_taxonomy_name(stripslashes((string) $attr_name)) : ''; $attribute_label = ucwords(stripslashes((string) $attr_name)); $attribute_type = 'select'; $attribute_orderby = 'menu_order'; $reserved_terms = array('attachment', 'attachment_id', 'author', 'author_name', 'calendar', 'cat', 'category', 'category__and', 'category__in', 'category__not_in', 'category_name', 'comments_per_page', 'comments_popup', 'cpage', 'day', 'debug', 'error', 'exact', 'feed', 'hour', 'link_category', 'm', 'minute', 'monthnum', 'more', 'name', 'nav_menu', 'nopaging', 'offset', 'order', 'orderby', 'p', 'page', 'page_id', 'paged', 'pagename', 'pb', 'perm', 'post', 'post__in', 'post__not_in', 'post_format', 'post_mime_type', 'post_status', 'post_tag', 'post_type', 'posts', 'posts_per_archive_page', 'posts_per_page', 'preview', 'robots', 's', 'search', 'second', 'sentence', 'showposts', 'static', 'subpost', 'subpost_id', 'tag', 'tag__and', 'tag__in', 'tag__not_in', 'tag_id', 'tag_slug__and', 'tag_slug__in', 'taxonomy', 'tb', 'term', 'type', 'w', 'withcomments', 'withoutcomments', 'year'); if (in_array($attribute_name, $reserved_terms)) { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Slug “%s” is not allowed because it is a reserved term. Change it, please.', 'pmxi_plugin'), wc_attribute_taxonomy_name($attribute_name))); } else { // Register the taxonomy now so that the import works! $domain = wc_attribute_taxonomy_name($attr_name); if (strlen($domain) <= 32) { $this->wpdb->insert($this->wpdb->prefix . 'woocommerce_attribute_taxonomies', array('attribute_label' => $attribute_label, 'attribute_name' => $attribute_name, 'attribute_type' => $attribute_type, 'attribute_orderby' => $attribute_orderby)); register_taxonomy($domain, apply_filters('woocommerce_taxonomy_objects_' . $domain, array('product')), apply_filters('woocommerce_taxonomy_args_' . $domain, array('hierarchical' => true, 'show_ui' => false, 'query_var' => true, 'rewrite' => false))); delete_transient('wc_attribute_taxonomies'); $attribute_taxonomies = $this->wpdb->get_results("SELECT * FROM " . $this->wpdb->prefix . "woocommerce_attribute_taxonomies"); set_transient('wc_attribute_taxonomies', $attribute_taxonomies); apply_filters('woocommerce_attribute_taxonomies', $attribute_taxonomies); $logger and call_user_func($logger, sprintf(__('- <b>CREATED</b>: Taxonomy attribute “%s” have been successfully created.', 'pmxi_plugin'), wc_attribute_taxonomy_name($attribute_name))); } else { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Taxonomy “%s” name is more than 32 characters. Change it, please.', 'pmxi_plugin'), $attr_name)); } } } }
/** * Layered Nav Init. */ public static function get_layered_nav_chosen_attributes() { if (!is_array(self::$_chosen_attributes)) { self::$_chosen_attributes = array(); if ($attribute_taxonomies = wc_get_attribute_taxonomies()) { foreach ($attribute_taxonomies as $tax) { $attribute = wc_sanitize_taxonomy_name($tax->attribute_name); $taxonomy = wc_attribute_taxonomy_name($attribute); $filter_terms = !empty($_GET['filter_' . $attribute]) ? explode(',', wc_clean($_GET['filter_' . $attribute])) : array(); if (empty($filter_terms) || !taxonomy_exists($taxonomy)) { continue; } $query_type = !empty($_GET['query_type_' . $attribute]) && in_array($_GET['query_type_' . $attribute], array('and', 'or')) ? wc_clean($_GET['query_type_' . $attribute]) : ''; self::$_chosen_attributes[$taxonomy]['terms'] = array_map('sanitize_title', $filter_terms); // Ensures correct encoding self::$_chosen_attributes[$taxonomy]['query_type'] = $query_type ? $query_type : apply_filters('woocommerce_layered_nav_default_query_type', 'and'); } } } return self::$_chosen_attributes; }
/** * Parses the WXR file and prepares us for the task of processing parsed data * * @param string $file Path to the WXR file for importing */ function import_start($file) { global $wpdb; if (!is_file($file)) { echo '<p><strong>' . __('Sorry, there has been an error.', 'wc_customer_relationship_manager') . '</strong><br />'; echo __('The file does not exist, please try again.', 'wc_customer_relationship_manager') . '</p>'; die; } if (in_array('user_email', $_POST['import_options'])) { $this->key_email = array_search('user_email', $_POST['import_options']); } if (empty($this->key_email) && in_array('billing_email', $_POST['import_options'])) { $this->key_email = array_search('billing_email', $_POST['import_options']); } if (empty($this->key_email) && $this->key_email !== 0) { echo '<p><strong>' . __('Sorry, there has been an error.', 'wc_customer_relationship_manager') . '</strong><br />'; echo __('Please select user email and please try again.', 'wc_customer_relationship_manager') . '</p>'; wp_import_cleanup($this->id); wp_cache_flush(); die; } $import_data = $this->parse($file); if (is_wp_error($import_data)) { echo '<p><strong>' . __('Sorry, there has been an error.', 'wc_customer_relationship_manager') . '</strong><br />'; echo esc_html($import_data->get_error_message()) . '</p>'; wp_import_cleanup($this->id); wp_cache_flush(); die; } if (in_array('first_name', $_POST['import_options'])) { $this->key_fname = array_search('first_name', $_POST['import_options']); } if (empty($this->key_fname) && in_array('billing_first_name', $_POST['import_options'])) { $this->key_fname = array_search('billing_first_name', $_POST['import_options']); } if (in_array('last_name', $_POST['import_options'])) { $this->key_lname = array_search('last_name', $_POST['import_options']); } if (empty($this->key_lname) && in_array('billing_last_name', $_POST['import_options'])) { $this->key_lname = array_search('billing_last_name', $_POST['import_options']); } if (in_array('user_nicename', $_POST['import_options'])) { $this->key_nice = array_search('user_nicename', $_POST['import_options']); } if (in_array('user_role', $_POST['import_options'])) { $this->key_role = array_search('user_role', $_POST['import_options']); } if (in_array('customer_status', $_POST['import_options'])) { $this->key_status = array_search('customer_status', $_POST['import_options']); } $skiped = false; while (($data = fgetcsv($import_data, 1000, ",")) !== FALSE) { if (isset($_POST['skip_first']) && $_POST['skip_first'] == 'yes' && !$skiped) { $skiped = true; continue; } $user_email = trim($data[$this->key_email]); if (empty($user_email) || email_exists($user_email)) { $this->not_import[] = $data; continue; } $nickname = ''; if (empty($this->key_nice)) { if (isset($data[$this->key_fname])) { $nickname .= sanitize_title($data[$this->key_fname]); } if (isset($data[$this->key_lname])) { $nickname .= '_' . sanitize_title($data[$this->key_lname]); } } else { $nickname .= sanitize_title($data[$this->key_nice]); } $user_login = ''; if (in_array('user_login', $_POST['import_options'])) { $key = array_search('user_login', $_POST['import_options']); $user_login = $data[$key]; } else { $user_login = $this->get_user_login($user_email, $nickname); } //$password = wp_generate_password(); add_filter('pre_option_woocommerce_registration_generate_password', 'wcrm_enable_generate_password'); $user_id = wc_create_new_customer($user_email, $user_login); remove_filter('pre_option_woocommerce_registration_generate_password', 'wcrm_enable_generate_password'); if (!empty($user_id) && !is_wp_error($user_id)) { if (empty($this->key_role) && isset($_POST['customer_role'])) { wp_update_user(array('ID' => $user_id, 'role' => $_POST['customer_role'])); } if (empty($this->key_status) && isset($_POST['customer_status'])) { $status = $_POST['customer_status']; wc_crm_change_customer_status($status, array($user_id)); } foreach ($_POST['import_options'] as $f_key => $meta_key) { if (empty($meta_key)) { continue; } if ($meta_key == 'user_login' || $meta_key == 'user_email') { continue; } if ($meta_key == 'url') { wp_update_user(array('ID' => $user_id, 'user_url' => $data[$f_key])); continue; } if ($meta_key == 'display_name') { wp_update_user(array('ID' => $user_id, 'display_name' => $data[$f_key])); continue; } if ($meta_key == 'wcrm_custom_meta') { $custom_meta_key = $_POST['import_options_custom_meta'][$f_key]; update_user_meta($user_id, $custom_meta_key, $data[$f_key]); continue; } if ($meta_key == 'user_nicename') { wp_update_user(array('ID' => $user_id, 'user_nicename' => $data[$f_key])); continue; } if ($meta_key == 'user_role') { wp_update_user(array('ID' => $user_id, 'role' => $data[$f_key])); continue; } if ($meta_key == 'customer_status') { $status = $this->check_customer_status($data[$f_key]); if (!$status) { $status = $_POST['customer_status']; } wc_crm_change_customer_status($status, array($user_id)); continue; } if ($meta_key == 'industry') { $industries = wc_crm_get_industries(); if (!in_array($data[$f_key], $industries)) { continue; } } if ($meta_key == 'user_group') { //global $wpdb $groups = $data[$f_key]; $groups = explode(',', $groups); if (!empty($groups)) { $group_ids = array(); foreach ($groups as $group_name) { $group_slug = wc_sanitize_taxonomy_name(stripslashes($group_name)); $group_exists = wc_crm_group_exists($group_slug); if (!$group_exists) { $group = array('group_name' => $group_name, 'group_slug' => $group_slug, 'group_type' => 'static'); $wpdb->insert($wpdb->prefix . 'wc_crm_groups', $group); $group_ids[] = $wpdb->insert_id; $this->groups_added[] = $group_name; do_action('wc_crm_group_added', $wpdb->insert_id, $group); } else { $group_ids[] = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->prefix}wc_crm_groups WHERE group_slug = %s LIMIT 1", $group_slug)); } } wc_crm_update_user_groups($group_ids, $user_email); } continue; } update_user_meta($user_id, $meta_key, $data[$f_key]); } $this->row++; } } }
/** * Edit a product attribute * * @since 2.4.0 * @param int $id the attribute ID * @param array $data * @return array */ public function edit_product_attribute($id, $data) { global $wpdb; try { if (!isset($data['product_attribute'])) { throw new WC_API_Exception('woocommerce_api_missing_product_attribute_data', sprintf(__('No %1$s data specified to edit %1$s', 'woocommerce'), 'product_attribute'), 400); } $id = absint($id); $data = $data['product_attribute']; // Check permissions if (!current_user_can('manage_product_terms')) { throw new WC_API_Exception('woocommerce_api_user_cannot_edit_product_attribute', __('You do not have permission to edit product attributes', 'woocommerce'), 401); } $data = apply_filters('woocommerce_api_edit_product_attribute_data', $data, $this); $attribute = $this->get_product_attribute($id); if (is_wp_error($attribute)) { return $attribute; } $attribute_name = isset($data['name']) ? $data['name'] : $attribute['product_attribute']['name']; $attribute_type = isset($data['type']) ? $data['type'] : $attribute['product_attribute']['type']; $attribute_order_by = isset($data['order_by']) ? $data['order_by'] : $attribute['product_attribute']['order_by']; if (isset($data['slug'])) { $attribute_slug = wc_sanitize_taxonomy_name(stripslashes($data['slug'])); } else { $attribute_slug = $attribute['product_attribute']['slug']; } $attribute_slug = preg_replace('/^pa\\_/', '', $attribute_slug); if (isset($data['has_archives'])) { $attribute_public = true === $data['has_archives'] ? 1 : 0; } else { $attribute_public = $attribute['product_attribute']['has_archives']; } // Validate the attribute data $this->validate_attribute_data($attribute_name, $attribute_slug, $attribute_type, $attribute_order_by, false); $update = $wpdb->update($wpdb->prefix . 'woocommerce_attribute_taxonomies', array('attribute_label' => $attribute_name, 'attribute_name' => $attribute_slug, 'attribute_type' => $attribute_type, 'attribute_orderby' => $attribute_order_by, 'attribute_public' => $attribute_public), array('attribute_id' => $id), array('%s', '%s', '%s', '%s', '%d'), array('%d')); // Checks for an error in the product creation if (false === $update) { throw new WC_API_Exception('woocommerce_api_cannot_edit_product_attribute', __('Could not edit the attribute', 'woocommerce'), 400); } do_action('woocommerce_api_edit_product_attribute', $id, $data); // Clear transients delete_transient('wc_attribute_taxonomies'); return $this->get_product_attribute($id); } catch (WC_API_Exception $e) { return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); } }
/** * Get a product attributes label. * * @param string $name * @param object $product object Optional * @return string */ function wc_attribute_label($name, $product = '') { global $wpdb; if (taxonomy_is_product_attribute($name)) { $name = wc_sanitize_taxonomy_name(str_replace('pa_', '', $name)); $all_labels = wp_list_pluck(wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name'); $label = isset($all_labels[$name]) ? $all_labels[$name] : $name; } elseif ($product) { if ($product->is_type('variation')) { $product = wc_get_product($product->get_parent_id()); } // Attempt to get label from product, as entered by the user. if (($attributes = $product->get_attributes()) && isset($attributes[sanitize_title($name)])) { $label = $attributes[sanitize_title($name)]->get_name(); } else { $label = $name; } } else { $label = $name; } return apply_filters('woocommerce_attribute_label', $label, $name, $product); }