/** * Order term when created (put in position 0). * * @param mixed $term_id * @param mixed $tt_id * @param string $taxonomy */ public function create_term($term_id, $tt_id = '', $taxonomy = '') { if ('product_cat' != $taxonomy && !taxonomy_is_product_attribute($taxonomy)) { return; } $meta_name = taxonomy_is_product_attribute($taxonomy) ? 'order_' . esc_attr($taxonomy) : 'order'; update_woocommerce_term_meta($term_id, $meta_name, 0); }
/** * 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); }
/** * Place a previous order again. */ public static function order_again() { // Nothing to do if (!isset($_GET['order_again']) || !is_user_logged_in() || !isset($_GET['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'woocommerce-order_again')) { return; } // Clear current cart WC()->cart->empty_cart(); // Load the previous order - Stop if the order does not exist $order = wc_get_order(absint($_GET['order_again'])); if (!$order->get_id()) { return; } if (!$order->has_status(apply_filters('woocommerce_valid_order_statuses_for_order_again', array('completed')))) { return; } // Make sure the user is allowed to order again. By default it check if the // previous order belonged to the current user. if (!current_user_can('order_again', $order->get_id())) { return; } // Copy products from the order to the cart foreach ($order->get_items() as $item) { // Load all product info including variation data $product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $item->get_product_id()); $quantity = $item->get_quantity(); $variation_id = $item->get_variation_id(); $variations = array(); $cart_item_data = apply_filters('woocommerce_order_again_cart_item_data', array(), $item, $order); foreach ($item->get_meta_data() as $meta) { if (taxonomy_is_product_attribute($meta->meta_key)) { $variations[$meta->meta_key] = $meta->meta_value; } elseif (meta_is_product_attribute($meta->meta_key, $meta->meta_value, $product_id)) { $variations[$meta->meta_key] = $meta->meta_value; } } // Add to cart validation if (!apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data)) { continue; } WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variations, $cart_item_data); } do_action('woocommerce_ordered_again', $order->get_id()); // Redirect to cart wc_add_notice(__('The cart has been filled with the items from your previous order.', 'woocommerce')); wp_safe_redirect(wc_get_cart_url()); exit; }
/** * Add term ordering to get_terms. * * It enables the support a 'menu_order' parameter to get_terms for the product_cat taxonomy. * By default it is 'ASC'. It accepts 'DESC' too. * * To disable it, set it ot false (or 0). * * @param array $clauses * @param array $taxonomies * @param array $args * @return array */ function wc_terms_clauses($clauses, $taxonomies, $args) { global $wpdb; // No sorting when menu_order is false. if (isset($args['menu_order']) && false == $args['menu_order']) { return $clauses; } // No sorting when orderby is non default. if (isset($args['orderby']) && 'name' !== $args['orderby']) { return $clauses; } // No sorting in admin when sorting by a column. if (is_admin() && isset($_GET['orderby'])) { return $clauses; } // No need to filter counts if (strpos('COUNT(*)', $clauses['fields']) !== false) { return $clauses; } // Wordpress should give us the taxonomies asked when calling the get_terms function. Only apply to categories and pa_ attributes. $found = false; foreach ((array) $taxonomies as $taxonomy) { if (taxonomy_is_product_attribute($taxonomy) || in_array($taxonomy, apply_filters('woocommerce_sortable_taxonomies', array('product_cat')))) { $found = true; break; } } if (!$found) { return $clauses; } // Meta name. if (!empty($taxonomies[0]) && taxonomy_is_product_attribute($taxonomies[0])) { $meta_name = 'order_' . esc_attr($taxonomies[0]); } else { $meta_name = 'order'; } // Query fields. $clauses['fields'] = 'DISTINCT ' . $clauses['fields']; // Query join. if (get_option('db_version') < 34370) { $clauses['join'] .= " LEFT JOIN {$wpdb->woocommerce_termmeta} AS tm ON (t.term_id = tm.woocommerce_term_id AND tm.meta_key = '" . esc_sql($meta_name) . "') "; } else { $clauses['join'] .= " LEFT JOIN {$wpdb->termmeta} AS tm ON (t.term_id = tm.term_id AND tm.meta_key = '" . esc_sql($meta_name) . "') "; } // Default to ASC. if (!isset($args['menu_order']) || !in_array(strtoupper($args['menu_order']), array('ASC', 'DESC'))) { $args['menu_order'] = 'ASC'; } $order = "ORDER BY tm.meta_value+0 " . $args['menu_order']; if ($clauses['orderby']) { $clauses['orderby'] = str_replace('ORDER BY', $order . ',', $clauses['orderby']); } else { $clauses['orderby'] = $order; } return $clauses; }
/** * Highlights the correct top level admin menu item for post type add screens. */ public function menu_highlight() { global $parent_file, $submenu_file, $post_type; switch ($post_type) { case 'shop_order': case 'shop_coupon': $parent_file = 'woocommerce'; break; case 'product': $screen = get_current_screen(); if (taxonomy_is_product_attribute($screen->taxonomy)) { $submenu_file = 'product_attributes'; $parent_file = 'edit.php?post_type=product'; } break; } }
function attribute_label($name, $key = NULL) { global $wpdb; $label = false; $name = str_replace('attribute_', '', $name); if (taxonomy_is_product_attribute($name)) { $name = woocommerce_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 { if ($key) { $label = $wpdb->get_var("SELECT meta_key FROM " . $wpdb->prefix . "woocommerce_order_itemmeta WHERE meta_value = '{$key}' limit 1"); } if (!$label) { $label = ucfirst($name); } } return apply_filters('woocommerce_attribute_label', $label, $name); }
/** * Highlights the correct top level admin menu item for post type add screens. * * @access public * @return void */ public function menu_highlight() { global $menu, $submenu, $parent_file, $submenu_file, $self, $post_type, $taxonomy; $to_highlight_types = array('shop_order', 'shop_coupon'); if (isset($post_type)) { if (in_array($post_type, $to_highlight_types)) { $submenu_file = 'edit.php?post_type=' . esc_attr($post_type); $parent_file = 'woocommerce'; } if ('product' == $post_type) { $screen = get_current_screen(); if ($screen->base == 'edit-tags' && taxonomy_is_product_attribute($taxonomy)) { $submenu_file = 'product_attributes'; $parent_file = 'edit.php?post_type=' . esc_attr($post_type); } } } if (isset($submenu['woocommerce']) && isset($submenu['woocommerce'][1])) { $submenu['woocommerce'][0] = $submenu['woocommerce'][1]; unset($submenu['woocommerce'][1]); } // Sort out Orders menu when on the top level if (!current_user_can('manage_woocommerce')) { foreach ($menu as $key => $menu_item) { if (strpos($menu_item[0], _x('Orders', 'Admin menu name', 'woocommerce')) === 0) { $menu_name = _x('Orders', 'Admin menu name', 'woocommerce'); $menu_name_count = ''; if ($order_count = wc_processing_order_count()) { $menu_name_count = " <span class='awaiting-mod update-plugins count-{$order_count}'><span class='processing-count'>" . number_format_i18n($order_count) . "</span></span>"; } $menu[$key][0] = $menu_name . $menu_name_count; $submenu['edit.php?post_type=shop_order'][5][0] = $menu_name; break; } } } }
/** * Highlights the correct top level admin menu item for post type add screens. * * @return void */ public function menu_highlight() { global $menu, $submenu, $parent_file, $submenu_file, $self, $post_type, $taxonomy; $to_highlight_types = array('shop_order', 'shop_coupon'); if (isset($post_type)) { if (in_array($post_type, $to_highlight_types)) { $submenu_file = 'edit.php?post_type=' . esc_attr($post_type); $parent_file = 'woocommerce'; } if ('product' == $post_type) { $screen = get_current_screen(); if ($screen->base == 'edit-tags' && taxonomy_is_product_attribute($taxonomy)) { $submenu_file = 'product_attributes'; $parent_file = 'edit.php?post_type=' . esc_attr($post_type); } } } if (isset($submenu['woocommerce']) && isset($submenu['woocommerce'][1])) { $submenu['woocommerce'][0] = $submenu['woocommerce'][1]; unset($submenu['woocommerce'][1]); } if (isset($submenu['woocommerce']) && current_user_can('manage_woocommerce')) { foreach ($submenu['woocommerce'] as $key => $menu_item) { if (0 === strpos($menu_item[0], _x('Orders', 'Admin menu name', 'woocommerce'))) { $menu_name = _x('Orders', 'Admin menu name', 'woocommerce'); if ($order_count = wc_processing_order_count()) { $menu_name .= ' <span class="awaiting-mod update-plugins count-' . $order_count . '"><span class="processing-count">' . number_format_i18n($order_count) . '</span></span>'; } $submenu['woocommerce'][$key][0] = $menu_name; break; } } } }
/** * Set up cart item meta data for a to complete a subscription renewal via the cart. * * @since 2.0 */ protected function setup_cart($subscription, $cart_item_data) { WC()->cart->empty_cart(true); $success = true; foreach ($subscription->get_items() as $item_id => $line_item) { // Load all product info including variation data $product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $line_item['product_id']); $quantity = (int) $line_item['qty']; $variation_id = (int) $line_item['variation_id']; $variations = array(); foreach ($line_item['item_meta'] as $meta_name => $meta_value) { if (taxonomy_is_product_attribute($meta_name)) { $variations[$meta_name] = $meta_value[0]; } elseif (meta_is_product_attribute($meta_name, $meta_value[0], $product_id)) { $variations[$meta_name] = $meta_value[0]; } } $product = get_product($line_item['product_id']); // The notice displayed when a subscription product has been deleted and the custoemr attempts to manually renew or make a renewal payment for a failed recurring payment for that product/subscription // translators: placeholder is an item name $product_deleted_error_message = apply_filters('woocommerce_subscriptions_renew_deleted_product_error_message', __('The %s product has been deleted and can no longer be renewed. Please choose a new product or contact us for assistance.', 'woocommerce-subscriptions')); // Display error message for deleted products if (false === $product) { wc_add_notice(sprintf($product_deleted_error_message, $line_item['name']), 'error'); // Make sure we don't actually need the variation ID (if the product was a variation, it will have a variation ID; however, if the product has changed from a simple subscription to a variable subscription, there will be no variation_id) } elseif ($product->is_type(array('variable-subscription')) && !empty($line_item['variation_id'])) { $variation = get_product($variation_id); // Display error message for deleted product variations if (false === $variation) { wc_add_notice(sprintf($product_deleted_error_message, $line_item['name']), 'error'); } } if (wcs_is_subscription($subscription)) { $cart_item_data['subscription_line_item_id'] = $item_id; } $cart_item_key = WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variations, apply_filters('woocommerce_order_again_cart_item_data', array($this->cart_item_key => $cart_item_data), $line_item, $subscription)); $success = $success && (bool) $cart_item_key; } // If a product linked to a subscription failed to be added to the cart prevent partially paying for the order by removing all cart items. if (!$success && wcs_is_subscription($subscription)) { wc_add_notice(sprintf(esc_html__('Subscription #%d has not been added to the cart.', 'woocommerce-subscriptions'), $subscription->id), 'error'); WC()->cart->empty_cart(true); } do_action('woocommerce_setup_cart_for_' . $this->cart_item_key, $subscription, $cart_item_data); }
/** * Set the sort order of a term * * @access public * @param int $term_id * @param int $index * @param string $taxonomy * @param bool $recursive (default: false) * @return int */ function woocommerce_set_term_order($term_id, $index, $taxonomy, $recursive = false) { global $wpdb; $term_id = (int) $term_id; $index = (int) $index; // Meta name if (taxonomy_is_product_attribute($taxonomy)) { $meta_name = 'order_' . esc_attr($taxonomy); } else { $meta_name = 'order'; } update_woocommerce_term_meta($term_id, $meta_name, $index); if (!$recursive) { return $index; } $children = get_terms($taxonomy, "parent={$term_id}&menu_order=ASC&hide_empty=0"); foreach ($children as $term) { $index++; $index = woocommerce_set_term_order($term->term_id, $index, $taxonomy, true); } clean_term_cache($term_id, $taxonomy); return $index; }
/** * 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); }
/** * Place a previous order again. * * @access public * @return void */ function woocommerce_order_again() { global $woocommerce; // Nothing to do if (!isset($_GET['order_again']) || !is_user_logged_in() || get_option('woocommerce_allow_customers_to_reorder') == 'no') { return; } // Nonce security check if (!$woocommerce->verify_nonce('order_again', '_GET')) { return; } // Clear current cart $woocommerce->cart->empty_cart(); // Load the previous order - Stop if the order does not exist $order = new WC_Order((int) $_GET['order_again']); if (empty($order->id)) { return; } if ($order->status != 'completed') { return; } // Make sure the previous order belongs to the current customer if ($order->user_id != get_current_user_id()) { return; } // Copy products from the order to the cart foreach ($order->get_items() as $item) { // Load all product info including variation data $product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $item['product_id']); $quantity = (int) $item['qty']; $variation_id = (int) $item['variation_id']; $variations = array(); $cart_item_data = apply_filters('woocommerce_order_again_cart_item_data', array(), $item, $order); foreach ($item['item_meta'] as $meta_name => $meta_value) { if (taxonomy_is_product_attribute($meta_name)) { $variations[$meta_name] = $meta_value[0]; } } // Add to cart validation if (!apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data)) { continue; } $woocommerce->cart->add_to_cart($product_id, $quantity, $variation_id, $variations, $cart_item_data); } do_action('woocommerce_ordered_again', $order->id); // Redirect to cart $woocommerce->add_message(__('The cart has been filled with the items from your previous order.', 'woocommerce')); wp_safe_redirect($woocommerce->cart->get_cart_url()); exit; }