Moved queries into class
This commit is contained in:
parent
16565712a5
commit
f4531a279f
|
@ -28,9 +28,7 @@ function woocommerce_attributes() {
|
|||
if ($attribute_name && strlen($attribute_name)<30 && $attribute_type && !taxonomy_exists( $woocommerce->attribute_taxonomy_name($attribute_name) )) :
|
||||
|
||||
$wpdb->insert( $wpdb->prefix . "woocommerce_attribute_taxonomies", array( 'attribute_name' => $attribute_name, 'attribute_label' => $attribute_label, 'attribute_type' => $attribute_type ), array( '%s', '%s' ) );
|
||||
|
||||
update_option('woocommerce_update_rewrite_rules', '1');
|
||||
|
||||
|
||||
wp_safe_redirect( get_admin_url() . 'admin.php?page=attributes' );
|
||||
exit;
|
||||
|
||||
|
|
|
@ -71,8 +71,6 @@ function woocommerce_import_start() {
|
|||
'rewrite' => array( 'slug' => strtolower(sanitize_title($nicename)), 'with_front' => false, 'hierarchical' => true ),
|
||||
)
|
||||
);
|
||||
|
||||
update_option('woocommerce_update_rewrite_rules', '1');
|
||||
|
||||
endif;
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ function woocommerce_admin_init() {
|
|||
include_once( 'admin-dashboard.php' );
|
||||
include_once( 'admin-import.php' );
|
||||
include_once( 'admin-post-types.php' );
|
||||
include_once( 'admin-taxonomies.php' );
|
||||
include_once( 'writepanels/writepanels-init.php' );
|
||||
}
|
||||
add_action('admin_init', 'woocommerce_admin_init');
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
/**
|
||||
* Functions used for custom post types in admin
|
||||
*
|
||||
* These functions control columns in admin, and other admin interface bits
|
||||
* These functions control columns in admin, and other admin interface bits.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category Admin
|
||||
|
|
|
@ -0,0 +1,190 @@
|
|||
<?php
|
||||
/**
|
||||
* Functions used for taxonomies in admin
|
||||
*
|
||||
* These functions control admin interface bits like category ordering.
|
||||
*
|
||||
* @author WooThemes
|
||||
* @category Admin
|
||||
* @package WooCommerce
|
||||
*/
|
||||
|
||||
/**
|
||||
* Categories ordering
|
||||
*/
|
||||
|
||||
/**
|
||||
* Add a table to $wpdb to benefit from wordpress meta api
|
||||
*/
|
||||
add_action('init','taxonomy_metadata_wpdbfix');
|
||||
add_action('switch_blog','taxonomy_metadata_wpdbfix');
|
||||
|
||||
function taxonomy_metadata_wpdbfix() {
|
||||
global $wpdb;
|
||||
$wpdb->woocommerce_termmeta = "{$wpdb->prefix}woocommerce_termmeta";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add product_cat 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)
|
||||
*
|
||||
*/
|
||||
add_filter( 'terms_clauses', 'woocommerce_terms_clauses', 10, 3);
|
||||
|
||||
function woocommerce_terms_clauses($clauses, $taxonomies, $args ) {
|
||||
global $wpdb;
|
||||
|
||||
// wordpress should give us the taxonomies asked when calling the get_terms function
|
||||
if( !in_array('product_cat', (array)$taxonomies) ) return $clauses;
|
||||
|
||||
// query order
|
||||
if( isset($args['menu_order']) && !$args['menu_order']) return $clauses; // menu_order is false so we do not add order clause
|
||||
|
||||
// query fields
|
||||
if( strpos('COUNT(*)', $clauses['fields']) === false ) $clauses['fields'] .= ', tm.* ';
|
||||
|
||||
//query join
|
||||
$clauses['join'] .= " LEFT JOIN {$wpdb->woocommerce_termmeta} AS tm ON (t.term_id = tm.woocommerce_term_id AND tm.meta_key = 'order') ";
|
||||
|
||||
// default to ASC
|
||||
if( ! isset($args['menu_order']) || ! in_array( strtoupper($args['menu_order']), array('ASC', 'DESC')) ) $args['menu_order'] = 'ASC';
|
||||
|
||||
$order = "ORDER BY CAST(tm.meta_value AS SIGNED) " . $args['menu_order'];
|
||||
|
||||
if ( $clauses['orderby'] ):
|
||||
$clauses['orderby'] = str_replace ('ORDER BY', $order . ',', $clauses['orderby'] );
|
||||
else:
|
||||
$clauses['orderby'] = $order;
|
||||
endif;
|
||||
|
||||
return $clauses;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reorder on category insertion
|
||||
*
|
||||
* @param int $term_id
|
||||
*/
|
||||
add_action("create_product_cat", 'woocommerce_create_product_cat');
|
||||
|
||||
function woocommerce_create_product_cat ($term_id) {
|
||||
|
||||
$next_id = null;
|
||||
|
||||
$term = get_term($term_id, 'product_cat');
|
||||
|
||||
// gets the sibling terms
|
||||
$siblings = get_terms('product_cat', "parent={$term->parent}&menu_order=ASC&hide_empty=0");
|
||||
|
||||
foreach ($siblings as $sibling) {
|
||||
if( $sibling->term_id == $term_id ) continue;
|
||||
$next_id = $sibling->term_id; // first sibling term of the hierachy level
|
||||
break;
|
||||
}
|
||||
|
||||
// reorder
|
||||
woocommerce_order_categories ( $term, $next_id );
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete terms metas on deletion
|
||||
*
|
||||
* @param int $term_id
|
||||
*/
|
||||
add_action("delete_product_cat", 'woocommerce_delete_product_cat');
|
||||
|
||||
function woocommerce_delete_product_cat($term_id) {
|
||||
|
||||
$term_id = (int) $term_id;
|
||||
|
||||
if(!$term_id) return;
|
||||
|
||||
global $wpdb;
|
||||
$wpdb->query("DELETE FROM {$wpdb->woocommerce_termmeta} WHERE `woocommerce_term_id` = " . $term_id);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Move a category before the a given element of its hierarchy level
|
||||
*
|
||||
* @param object $the_term
|
||||
* @param int $next_id the id of the next slibling element in save hierachy level
|
||||
* @param int $index
|
||||
* @param int $terms
|
||||
*/
|
||||
function woocommerce_order_categories ( $the_term, $next_id, $index=0, $terms=null ) {
|
||||
|
||||
if( ! $terms ) $terms = get_terms('product_cat', 'menu_order=ASC&hide_empty=0&parent=0');
|
||||
if( empty( $terms ) ) return $index;
|
||||
|
||||
$id = $the_term->term_id;
|
||||
|
||||
$term_in_level = false; // flag: is our term to order in this level of terms
|
||||
|
||||
foreach ($terms as $term) {
|
||||
|
||||
if( $term->term_id == $id ) { // our term to order, we skip
|
||||
$term_in_level = true;
|
||||
continue; // our term to order, we skip
|
||||
}
|
||||
// the nextid of our term to order, lets move our term here
|
||||
if(null !== $next_id && $term->term_id == $next_id) {
|
||||
$index++;
|
||||
$index = woocommerce_set_category_order($id, $index, true);
|
||||
}
|
||||
|
||||
// set order
|
||||
$index++;
|
||||
$index = woocommerce_set_category_order($term->term_id, $index);
|
||||
|
||||
// if that term has children we walk through them
|
||||
$children = get_terms('product_cat', "parent={$term->term_id}&menu_order=ASC&hide_empty=0");
|
||||
if( !empty($children) ) {
|
||||
$index = woocommerce_order_categories ( $the_term, $next_id, $index, $children );
|
||||
}
|
||||
}
|
||||
|
||||
// no nextid meaning our term is in last position
|
||||
if( $term_in_level && null === $next_id )
|
||||
$index = woocommerce_set_category_order($id, $index+1, true);
|
||||
|
||||
return $index;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the sort order of a category
|
||||
*
|
||||
* @param int $term_id
|
||||
* @param int $index
|
||||
* @param bool $recursive
|
||||
*/
|
||||
function woocommerce_set_category_order ($term_id, $index, $recursive=false) {
|
||||
global $wpdb;
|
||||
|
||||
$term_id = (int) $term_id;
|
||||
$index = (int) $index;
|
||||
|
||||
update_metadata('woocommerce_term', $term_id, 'order', $index);
|
||||
|
||||
if( ! $recursive ) return $index;
|
||||
|
||||
$children = get_terms('product_cat', "parent=$term_id&menu_order=ASC&hide_empty=0");
|
||||
|
||||
foreach ( $children as $term ) {
|
||||
$index ++;
|
||||
$index = woocommerce_set_category_order ($term->term_id, $index, true);
|
||||
}
|
||||
|
||||
return $index;
|
||||
|
||||
}
|
|
@ -15,9 +15,9 @@ class woocommerce {
|
|||
var $attribute_taxonomies; // Stores the attribute taxonomies used in the store
|
||||
var $plugin_url;
|
||||
var $plugin_path;
|
||||
var $query;
|
||||
|
||||
// Class instances
|
||||
var $query;
|
||||
var $customer;
|
||||
var $shipping;
|
||||
var $cart;
|
||||
|
@ -28,6 +28,7 @@ class woocommerce {
|
|||
function __construct() {
|
||||
|
||||
// Load class instances
|
||||
$this->query = &new woocommerce_query(); // Query class, handles front-end queries and loops
|
||||
$this->customer = &new woocommerce_customer(); // Customer class, sorts out session data such as location
|
||||
$this->shipping = &new woocommerce_shipping(); // Shipping class. loads and stores shipping methods
|
||||
$this->cart = &new woocommerce_cart(); // Cart class, stores the cart contents
|
||||
|
@ -37,16 +38,6 @@ class woocommerce {
|
|||
// Load messages
|
||||
$this->load_messages();
|
||||
|
||||
// Define query vars
|
||||
$this->query = array(
|
||||
'unfiltered_product_ids' => array(), // Unfilted product ids (before layered nav etc)
|
||||
'filtered_product_ids' => array(), // Filted product ids (after layered nav)
|
||||
'post__in' => array(), // Product id's that match the layered nav + price filter
|
||||
'meta_query' => '', // The meta query for the page
|
||||
'layered_nav_post__in' => array(), // posts matching layered nav only
|
||||
'layered_nav_product_ids' => array() // Stores posts matching layered nav, so price filter can find max price in view
|
||||
);
|
||||
|
||||
// Hooks
|
||||
add_filter('wp_redirect', array(&$this, 'redirect'), 1, 2);
|
||||
add_action( 'woocommerce_before_single_product', array(&$this, 'show_messages'), 10);
|
||||
|
|
|
@ -0,0 +1,147 @@
|
|||
<?php
|
||||
/**
|
||||
* Contains the query functions for WooCommerce which alter the front-end post queries and loops.
|
||||
*
|
||||
* @class woocommerce
|
||||
* @package WooCommerce
|
||||
* @category Class
|
||||
* @author WooThemes
|
||||
*/
|
||||
class woocommerce_query {
|
||||
|
||||
var $unfiltered_product_ids = array(); // Unfilted product ids (before layered nav etc)
|
||||
var $filtered_product_ids = array(); // Filted product ids (after layered nav)
|
||||
var $post__in = array(); // Product id's that match the layered nav + price filter
|
||||
var $meta_query = ''; // The meta query for the page
|
||||
var $layered_nav_post__in = array(); // posts matching layered nav only
|
||||
var $layered_nav_product_ids = array(); // Stores posts matching layered nav, so price filter can find max price in view
|
||||
|
||||
/** constructor */
|
||||
function __construct() {
|
||||
add_filter( 'parse_query', array( &$this, 'parse_query') );
|
||||
add_action('wp', array( &$this, 'remove_parse_query') );
|
||||
}
|
||||
|
||||
/**
|
||||
* Query the products, applying sorting/ordering etc. This applies to the main wordpress loop
|
||||
*/
|
||||
function parse_query( $q ) {
|
||||
|
||||
if (is_admin()) return;
|
||||
|
||||
// Only apply to product categories, the product post archive, the shop page, and product tags
|
||||
if ( ( isset( $q->query_vars['suppress_filters'] ) && true == $q->query_vars['suppress_filters'] ) || (!$q->is_tax( 'product_cat' ) && !$q->is_post_type_archive( 'product' ) && !$q->is_page( get_option('woocommerce_shop_page_id') ) && !$q->is_tax( 'product_tag' ))) return;
|
||||
|
||||
$meta_query = (array) $q->get( 'meta_query' );
|
||||
|
||||
// Visibility
|
||||
if ( is_search() ) $in = array( 'visible', 'search' ); else $in = array( 'visible', 'catalog' );
|
||||
|
||||
$meta_query[] = array(
|
||||
'key' => 'visibility',
|
||||
'value' => $in,
|
||||
'compare' => 'IN'
|
||||
);
|
||||
|
||||
// In stock
|
||||
if (get_option('woocommerce_hide_out_of_stock_items')=='yes') :
|
||||
$meta_query[] = array(
|
||||
'key' => 'stock_status',
|
||||
'value' => 'instock',
|
||||
'compare' => '='
|
||||
);
|
||||
endif;
|
||||
|
||||
// Ordering
|
||||
if (isset($_POST['orderby']) && $_POST['orderby'] != '') $_SESSION['orderby'] = $_POST['orderby'];
|
||||
$current_order = (isset($_SESSION['orderby'])) ? $_SESSION['orderby'] : 'title';
|
||||
|
||||
switch ($current_order) :
|
||||
case 'date' :
|
||||
$orderby = 'date';
|
||||
$order = 'desc';
|
||||
$meta_key = '';
|
||||
break;
|
||||
case 'price' :
|
||||
$orderby = 'meta_value_num';
|
||||
$order = 'asc';
|
||||
$meta_key = 'price';
|
||||
break;
|
||||
default :
|
||||
$orderby = 'title';
|
||||
$order = 'asc';
|
||||
$meta_key = '';
|
||||
break;
|
||||
endswitch;
|
||||
|
||||
// Get a list of post id's which match the current filters set (in the layered nav and price filter)
|
||||
$post__in = array_unique(apply_filters('loop-shop-posts-in', array()));
|
||||
|
||||
// Ordering query vars
|
||||
$q->set( 'orderby', $orderby );
|
||||
$q->set( 'order', $order );
|
||||
$q->set( 'meta_key', $meta_key );
|
||||
|
||||
// Query vars that affect posts shown
|
||||
$q->set( 'post_type', 'product' );
|
||||
$q->set( 'meta_query', $meta_query );
|
||||
$q->set( 'post__in', $post__in );
|
||||
$q->set( 'posts_per_page', apply_filters('loop_shop_per_page', get_option('posts_per_page')) );
|
||||
|
||||
// Store variables
|
||||
$this->post__in = $post__in;
|
||||
$this->meta_query = $meta_query;
|
||||
|
||||
// We're on a shop page so queue the woocommerce_get_products_in_view function
|
||||
add_action('wp', array( &$this, 'get_products_in_view' ), 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove parse_query so it only applies to main loop
|
||||
*/
|
||||
function remove_parse_query() {
|
||||
remove_filter( 'parse_query', array( &$this, 'parse_query') );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an unpaginated list all product ID's (both filtered and unfiltered)
|
||||
*/
|
||||
function get_products_in_view() {
|
||||
|
||||
global $wp_query;
|
||||
|
||||
$unfiltered_product_ids = array();
|
||||
|
||||
// Get all visible posts, regardless of filters
|
||||
$unfiltered_product_ids = get_posts(
|
||||
array_merge(
|
||||
$wp_query->query,
|
||||
array(
|
||||
'post_type' => 'product',
|
||||
'numberposts' => -1,
|
||||
'post_status' => 'publish',
|
||||
'meta_query' => $this->meta_query,
|
||||
'fields' => 'ids'
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// Store the variable
|
||||
$this->unfiltered_product_ids = $unfiltered_product_ids;
|
||||
|
||||
// Also store filtered posts ids...
|
||||
if (sizeof($this->post__in)>0) :
|
||||
$this->filtered_product_ids = array_intersect($this->unfiltered_product_ids, $this->post__in);
|
||||
else :
|
||||
$this->filtered_product_ids = $this->unfiltered_product_ids;
|
||||
endif;
|
||||
|
||||
// And filtered post ids which just take layered nav into consideration (to find max price in the price widget)
|
||||
if (sizeof($this->layered_nav_post__in)>0) :
|
||||
$this->layered_nav_product_ids = array_intersect($this->unfiltered_product_ids, $this->layered_nav_post__in);
|
||||
else :
|
||||
$this->layered_nav_product_ids = $this->unfiltered_product_ids;
|
||||
endif;
|
||||
}
|
||||
|
||||
}
|
|
@ -66,8 +66,8 @@ function woocommerce_layered_nav_query( $filtered_posts ) {
|
|||
|
||||
if ($filtered) :
|
||||
|
||||
$woocommerce->query['layered_nav_post__in'] = $matched_products;
|
||||
$woocommerce->query['layered_nav_post__in'][] = 0;
|
||||
$woocommerce->query->layered_nav_post__in = $matched_products;
|
||||
$woocommerce->query->layered_nav_post__in[] = 0;
|
||||
|
||||
if (sizeof($filtered_posts)==0) :
|
||||
$filtered_posts = $matched_products;
|
||||
|
@ -147,7 +147,7 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget {
|
|||
|
||||
$_products_in_term = get_objects_in_term( $term->term_id, $taxonomy );
|
||||
|
||||
$count = sizeof(array_intersect($_products_in_term, $woocommerce->query['filtered_product_ids']));
|
||||
$count = sizeof(array_intersect($_products_in_term, $woocommerce->query->filtered_product_ids));
|
||||
|
||||
if ($count>0) $found = true;
|
||||
|
||||
|
|
|
@ -132,9 +132,9 @@ class WooCommerce_Widget_Price_Filter extends WP_Widget {
|
|||
FROM $wpdb->posts
|
||||
LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id
|
||||
WHERE meta_key = 'price' AND (
|
||||
$wpdb->posts.ID IN (".implode(',', $woocommerce->query['layered_nav_product_ids']).")
|
||||
$wpdb->posts.ID IN (".implode(',', $woocommerce->query->layered_nav_product_ids).")
|
||||
OR (
|
||||
$wpdb->posts.post_parent IN (".implode(',', $woocommerce->query['layered_nav_product_ids']).")
|
||||
$wpdb->posts.post_parent IN (".implode(',', $woocommerce->query->layered_nav_product_ids).")
|
||||
AND $wpdb->posts.post_parent != 0
|
||||
)
|
||||
)"));
|
||||
|
|
|
@ -24,23 +24,6 @@ if (!defined('WOOCOMMERCE_TEMPLATE_URL')) define('WOOCOMMERCE_TEMPLATE_URL', 'wo
|
|||
if (!defined("WOOCOMMERCE_VERSION")) define("WOOCOMMERCE_VERSION", "1.0");
|
||||
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
|
||||
|
||||
/**
|
||||
* Include core files
|
||||
**/
|
||||
include_once( 'woocommerce_taxonomy.php' );
|
||||
include_once( 'classes/woocommerce.class.php' );
|
||||
include_once( 'widgets/widgets-init.php' );
|
||||
include_once( 'shortcodes/shortcodes-init.php' );
|
||||
include_once( 'woocommerce_actions.php' );
|
||||
include_once( 'woocommerce_emails.php' );
|
||||
include_once( 'woocommerce_query.php' );
|
||||
include_once( 'woocommerce_template_actions.php' );
|
||||
include_once( 'woocommerce_templates.php' );
|
||||
include_once( 'classes/gateways/gateways.class.php' );
|
||||
include_once( 'classes/gateways/gateway.class.php' );
|
||||
include_once( 'classes/shipping/shipping.class.php' );
|
||||
include_once( 'classes/shipping/shipping_method.class.php' );
|
||||
|
||||
/**
|
||||
* Include admin area
|
||||
**/
|
||||
|
@ -57,8 +40,19 @@ if (is_admin()) :
|
|||
endif;
|
||||
|
||||
/**
|
||||
* Include classes
|
||||
*/
|
||||
* Include core files
|
||||
**/
|
||||
include_once( 'woocommerce_taxonomy.php' );
|
||||
include_once( 'widgets/widgets-init.php' );
|
||||
include_once( 'shortcodes/shortcodes-init.php' );
|
||||
include_once( 'woocommerce_actions.php' );
|
||||
include_once( 'woocommerce_emails.php' );
|
||||
include_once( 'woocommerce_template_actions.php' );
|
||||
include_once( 'woocommerce_templates.php' );
|
||||
include_once( 'classes/gateways/gateways.class.php' );
|
||||
include_once( 'classes/gateways/gateway.class.php' );
|
||||
include_once( 'classes/shipping/shipping.class.php' );
|
||||
include_once( 'classes/shipping/shipping_method.class.php' );
|
||||
include_once( 'classes/cart.class.php' );
|
||||
include_once( 'classes/checkout.class.php' );
|
||||
include_once( 'classes/countries.class.php' );
|
||||
|
@ -70,6 +64,8 @@ include_once( 'classes/product.class.php' );
|
|||
include_once( 'classes/product_variation.class.php' );
|
||||
include_once( 'classes/tax.class.php' );
|
||||
include_once( 'classes/validation.class.php' );
|
||||
include_once( 'classes/woocommerce_query.class.php' );
|
||||
include_once( 'classes/woocommerce.class.php' );
|
||||
|
||||
/**
|
||||
* Include core shipping modules
|
||||
|
|
|
@ -79,8 +79,8 @@ function woocommerce_parse_query( $q ) {
|
|||
$q->set( 'posts_per_page', apply_filters('loop_shop_per_page', get_option('posts_per_page')) );
|
||||
|
||||
// Store variables
|
||||
$woocommerce->query['post__in'] = $post__in;
|
||||
$woocommerce->query['meta_query'] = $meta_query;
|
||||
$woocommerce->query->post__in = $post__in;
|
||||
$woocommerce->query->meta_query = $meta_query;
|
||||
|
||||
// We're on a shop page so queue the woocommerce_get_products_in_view function
|
||||
add_action('wp', 'woocommerce_get_products_in_view', 2);
|
||||
|
@ -112,26 +112,26 @@ function woocommerce_get_products_in_view() {
|
|||
'post_type' => 'product',
|
||||
'numberposts' => -1,
|
||||
'post_status' => 'publish',
|
||||
'meta_query' => $woocommerce->query['meta_query'],
|
||||
'meta_query' => $woocommerce->query->meta_query,
|
||||
'fields' => 'ids'
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// Store the variable
|
||||
$woocommerce->query['unfiltered_product_ids'] = $unfiltered_product_ids;
|
||||
$woocommerce->query->unfiltered_product_ids = $unfiltered_product_ids;
|
||||
|
||||
// Also store filtered posts ids...
|
||||
if (sizeof($woocommerce->query['post__in'])>0) :
|
||||
$woocommerce->query['filtered_product_ids'] = array_intersect($woocommerce->query['unfiltered_product_ids'], $woocommerce->query['post__in']);
|
||||
if (sizeof($woocommerce->query->post__in)>0) :
|
||||
$woocommerce->query->filtered_product_ids = array_intersect($woocommerce->query->unfiltered_product_ids, $woocommerce->query->post__in);
|
||||
else :
|
||||
$woocommerce->query['filtered_product_ids'] = $woocommerce->query['unfiltered_product_ids'];
|
||||
$woocommerce->query->filtered_product_ids = $woocommerce->query->unfiltered_product_ids;
|
||||
endif;
|
||||
|
||||
// And filtered post ids which just take layered nav into consideration (to find max price in the price widget)
|
||||
if (sizeof($woocommerce->query['layered_nav_post__in'])>0) :
|
||||
$woocommerce->query['layered_nav_product_ids'] = array_intersect($woocommerce->query['unfiltered_product_ids'], $woocommerce->query['layered_nav_post__in']);
|
||||
if (sizeof($woocommerce->query->layered_nav_post__in)>0) :
|
||||
$woocommerce->query->layered_nav_product_ids = array_intersect($woocommerce->query->unfiltered_product_ids, $woocommerce->query->layered_nav_post__in);
|
||||
else :
|
||||
$woocommerce->query['layered_nav_product_ids'] = $woocommerce->query['unfiltered_product_ids'];
|
||||
$woocommerce->query->layered_nav_product_ids = $woocommerce->query->unfiltered_product_ids;
|
||||
endif;
|
||||
}
|
|
@ -269,184 +269,4 @@ function woocommerce_post_type() {
|
|||
'show_in_nav_menus' => false,
|
||||
)
|
||||
);
|
||||
|
||||
if (get_option('woocommerce_update_rewrite_rules')=='1') :
|
||||
// Re-generate rewrite rules
|
||||
global $wp_rewrite;
|
||||
$wp_rewrite->flush_rules();
|
||||
update_option('woocommerce_update_rewrite_rules', '0');
|
||||
endif;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Categories ordering
|
||||
*/
|
||||
|
||||
/**
|
||||
* Add a table to $wpdb to benefit from wordpress meta api
|
||||
*/
|
||||
function taxonomy_metadata_wpdbfix() {
|
||||
global $wpdb;
|
||||
$wpdb->woocommerce_termmeta = "{$wpdb->prefix}woocommerce_termmeta";
|
||||
}
|
||||
add_action('init','taxonomy_metadata_wpdbfix');
|
||||
add_action('switch_blog','taxonomy_metadata_wpdbfix');
|
||||
|
||||
/**
|
||||
* Add product_cat 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)
|
||||
*
|
||||
*/
|
||||
function woocommerce_terms_clauses($clauses, $taxonomies, $args ) {
|
||||
global $wpdb;
|
||||
|
||||
// wordpress should give us the taxonomies asked when calling the get_terms function
|
||||
if( !in_array('product_cat', (array)$taxonomies) ) return $clauses;
|
||||
|
||||
// query order
|
||||
if( isset($args['menu_order']) && !$args['menu_order']) return $clauses; // menu_order is false so we do not add order clause
|
||||
|
||||
// query fields
|
||||
if( strpos('COUNT(*)', $clauses['fields']) === false ) $clauses['fields'] .= ', tm.* ';
|
||||
|
||||
//query join
|
||||
$clauses['join'] .= " LEFT JOIN {$wpdb->woocommerce_termmeta} AS tm ON (t.term_id = tm.woocommerce_term_id AND tm.meta_key = 'order') ";
|
||||
|
||||
// default to ASC
|
||||
if( ! isset($args['menu_order']) || ! in_array( strtoupper($args['menu_order']), array('ASC', 'DESC')) ) $args['menu_order'] = 'ASC';
|
||||
|
||||
$order = "ORDER BY CAST(tm.meta_value AS SIGNED) " . $args['menu_order'];
|
||||
|
||||
if ( $clauses['orderby'] ):
|
||||
$clauses['orderby'] = str_replace ('ORDER BY', $order . ',', $clauses['orderby'] );
|
||||
else:
|
||||
$clauses['orderby'] = $order;
|
||||
endif;
|
||||
|
||||
return $clauses;
|
||||
}
|
||||
add_filter( 'terms_clauses', 'woocommerce_terms_clauses', 10, 3);
|
||||
|
||||
/**
|
||||
* Reorder on category insertion
|
||||
*
|
||||
* @param int $term_id
|
||||
*/
|
||||
function woocommerce_create_product_cat ($term_id) {
|
||||
|
||||
$next_id = null;
|
||||
|
||||
$term = get_term($term_id, 'product_cat');
|
||||
|
||||
// gets the sibling terms
|
||||
$siblings = get_terms('product_cat', "parent={$term->parent}&menu_order=ASC&hide_empty=0");
|
||||
|
||||
foreach ($siblings as $sibling) {
|
||||
if( $sibling->term_id == $term_id ) continue;
|
||||
$next_id = $sibling->term_id; // first sibling term of the hierachy level
|
||||
break;
|
||||
}
|
||||
|
||||
// reorder
|
||||
woocommerce_order_categories ( $term, $next_id );
|
||||
|
||||
}
|
||||
add_action("create_product_cat", 'woocommerce_create_product_cat');
|
||||
|
||||
/**
|
||||
* Delete terms metas on deletion
|
||||
*
|
||||
* @param int $term_id
|
||||
*/
|
||||
function woocommerce_delete_product_cat($term_id) {
|
||||
|
||||
$term_id = (int) $term_id;
|
||||
|
||||
if(!$term_id) return;
|
||||
|
||||
global $wpdb;
|
||||
$wpdb->query("DELETE FROM {$wpdb->woocommerce_termmeta} WHERE `woocommerce_term_id` = " . $term_id);
|
||||
|
||||
}
|
||||
add_action("delete_product_cat", 'woocommerce_delete_product_cat');
|
||||
|
||||
/**
|
||||
* Move a category before the a given element of its hierarchy level
|
||||
*
|
||||
* @param object $the_term
|
||||
* @param int $next_id the id of the next slibling element in save hierachy level
|
||||
* @param int $index
|
||||
* @param int $terms
|
||||
*/
|
||||
function woocommerce_order_categories ( $the_term, $next_id, $index=0, $terms=null ) {
|
||||
|
||||
if( ! $terms ) $terms = get_terms('product_cat', 'menu_order=ASC&hide_empty=0&parent=0');
|
||||
if( empty( $terms ) ) return $index;
|
||||
|
||||
$id = $the_term->term_id;
|
||||
|
||||
$term_in_level = false; // flag: is our term to order in this level of terms
|
||||
|
||||
foreach ($terms as $term) {
|
||||
|
||||
if( $term->term_id == $id ) { // our term to order, we skip
|
||||
$term_in_level = true;
|
||||
continue; // our term to order, we skip
|
||||
}
|
||||
// the nextid of our term to order, lets move our term here
|
||||
if(null !== $next_id && $term->term_id == $next_id) {
|
||||
$index++;
|
||||
$index = woocommerce_set_category_order($id, $index, true);
|
||||
}
|
||||
|
||||
// set order
|
||||
$index++;
|
||||
$index = woocommerce_set_category_order($term->term_id, $index);
|
||||
|
||||
// if that term has children we walk through them
|
||||
$children = get_terms('product_cat', "parent={$term->term_id}&menu_order=ASC&hide_empty=0");
|
||||
if( !empty($children) ) {
|
||||
$index = woocommerce_order_categories ( $the_term, $next_id, $index, $children );
|
||||
}
|
||||
}
|
||||
|
||||
// no nextid meaning our term is in last position
|
||||
if( $term_in_level && null === $next_id )
|
||||
$index = woocommerce_set_category_order($id, $index+1, true);
|
||||
|
||||
return $index;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the sort order of a category
|
||||
*
|
||||
* @param int $term_id
|
||||
* @param int $index
|
||||
* @param bool $recursive
|
||||
*/
|
||||
function woocommerce_set_category_order ($term_id, $index, $recursive=false) {
|
||||
global $wpdb;
|
||||
|
||||
$term_id = (int) $term_id;
|
||||
$index = (int) $index;
|
||||
|
||||
update_metadata('woocommerce_term', $term_id, 'order', $index);
|
||||
|
||||
if( ! $recursive ) return $index;
|
||||
|
||||
$children = get_terms('product_cat', "parent=$term_id&menu_order=ASC&hide_empty=0");
|
||||
|
||||
foreach ( $children as $term ) {
|
||||
$index ++;
|
||||
$index = woocommerce_set_category_order ($term->term_id, $index, true);
|
||||
}
|
||||
|
||||
return $index;
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue