This commit is contained in:
Mike Jolley 2011-09-04 01:02:44 +01:00
parent 71efa91820
commit 6344d22b11
9 changed files with 320 additions and 359 deletions

View File

@ -21,6 +21,15 @@ function woocommerce_admin_init() {
}
add_action('admin_init', 'woocommerce_admin_init');
/**
* Admin Scripts
*/
function woocommerce_admin_scripts() {
wp_register_script( 'woocommerce_admin', woocommerce::plugin_url() . '/assets/js/woocommerce_admin.js', array('jquery', 'jquery-ui-widget'), '1.0' );
wp_enqueue_script('woocommerce_admin');
}
add_action('admin_print_scripts', 'woocommerce_admin_scripts');
/**
* Admin Menus
*

View File

@ -15,12 +15,12 @@ class woocommerce {
public static $errors = array(); // Stores store errors
public static $messages = array(); // Stores store messages
public static $attribute_taxonomies; // Stores the attribute taxonomies used in the store
public static $plugin_url;
public static $plugin_path;
public static $query;
/** constructor */
private function __construct () {
private function __construct() {
// Vars
if (isset($_SESSION['errors'])) self::$errors = $_SESSION['errors'];
@ -29,6 +29,16 @@ class woocommerce {
unset($_SESSION['messages']);
unset($_SESSION['errors']);
// Query vars
self::$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);
}

View File

@ -1,11 +1,92 @@
<?php
/**
* Layered Navigation Widget
* Layered Navigation Widget and related functions
*
* @package WooCommerce
* @category Widgets
* @author WooThemes
*/
/**
* Layered Nav Init
*/
add_action('init', 'woocommerce_layered_nav_init', 1);
function woocommerce_layered_nav_init() {
global $_chosen_attributes, $wpdb;
$_chosen_attributes = array();
$attribute_taxonomies = woocommerce::$attribute_taxonomies;
if ( $attribute_taxonomies ) :
foreach ($attribute_taxonomies as $tax) :
$attribute = strtolower(sanitize_title($tax->attribute_name));
$taxonomy = woocommerce::attribute_name($attribute);
$name = 'filter_' . $attribute;
if (isset($_GET[$name]) && taxonomy_exists($taxonomy)) $_chosen_attributes[$taxonomy] = explode(',', $_GET[$name] );
endforeach;
endif;
}
/**
* Layered Nav post filter
*/
add_filter('loop-shop-posts-in', 'woocommerce_layered_nav_query');
function woocommerce_layered_nav_query( $filtered_posts ) {
global $_chosen_attributes, $wpdb;
if (sizeof($_chosen_attributes)>0) :
$matched_products = array();
$filtered = false;
foreach ($_chosen_attributes as $attribute => $values) :
if (sizeof($values)>0) :
foreach ($values as $value) :
$posts = get_objects_in_term( $value, $attribute );
if (!is_wp_error($posts) && (sizeof($matched_products)>0 || $filtered)) :
$matched_products = array_intersect($posts, $matched_products);
elseif (!is_wp_error($posts)) :
$matched_products = $posts;
endif;
$filtered = true;
endforeach;
endif;
endforeach;
if ($filtered) :
woocommerce::$query['layered_nav_post__in'] = $matched_products;
woocommerce::$query['layered_nav_post__in'][] = 0;
if (sizeof($filtered_posts)==0) :
$filtered_posts = $matched_products;
$filtered_posts[] = 0;
else :
$filtered_posts = array_intersect($filtered_posts, $matched_products);
$filtered_posts[] = 0;
endif;
endif;
endif;
return (array) $filtered_posts;
}
/**
* Layered Nav Widget
*/
class WooCommerce_Widget_Layered_Nav extends WP_Widget {
/** Variables to setup the widget. */
@ -36,7 +117,7 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget {
if (!is_tax( 'product_cat' ) && !is_post_type_archive('product') && !is_tax( 'product_tag' )) return;
global $_chosen_attributes, $wpdb, $woocommerce_query;
global $_chosen_attributes, $wpdb;
$title = $instance['title'];
$taxonomy = woocommerce::attribute_name($instance['attribute']);
@ -66,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;

View File

@ -1,6 +1,6 @@
<?php
/**
* Price Filter Widget
* Price Filter Widget and related functions
*
* Generates a range slider to filter products by price
*
@ -9,25 +9,74 @@
* @author WooThemes
*/
/**
* Price filter Init
*/
add_action('init', 'woocommerce_price_filter_init');
function woocommerce_price_filter_init() {
unset($_SESSION['min_price']);
unset($_SESSION['max_price']);
if (isset($_GET['min_price'])) :
$_SESSION['min_price'] = $_GET['min_price'];
endif;
if (isset($_GET['max_price'])) :
$_SESSION['max_price'] = $_GET['max_price'];
endif;
}
add_action('init', 'woocommerce_price_filter_init');
/**
* Price Filter post filter
*/
add_filter('loop-shop-posts-in', 'woocommerce_price_filter');
function woocommerce_price_filter( $filtered_posts ) {
if (isset($_GET['max_price']) && isset($_GET['min_price'])) :
$matched_products = array();
$matched_products_query = get_posts(array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'price',
'value' => array( $_GET['min_price'], $_GET['max_price'] ),
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
)
)
));
if ($matched_products_query) :
foreach ($matched_products_query as $product) :
$matched_products[] = $product->ID;
if ($product->post_parent>0) $matched_products[] = $product->post_parent;
endforeach;
endif;
// Filter the id's
if (sizeof($filtered_posts)==0) :
$filtered_posts = $matched_products;
$filtered_posts[] = 0;
else :
$filtered_posts = array_intersect($filtered_posts, $matched_products);
$filtered_posts[] = 0;
endif;
endif;
return (array) $filtered_posts;
}
/**
* Price Filter post Widget
*/
class WooCommerce_Widget_Price_Filter extends WP_Widget {
/** Variables to setup the widget. */
@ -58,7 +107,7 @@ class WooCommerce_Widget_Price_Filter extends WP_Widget {
if (!is_tax( 'product_cat' ) && !is_post_type_archive('product') && !is_tax( 'product_tag' )) return;
global $_chosen_attributes, $wpdb, $woocommerce_query;
global $_chosen_attributes, $wpdb;
$title = $instance['title'];
$title = apply_filters('widget_title', $title, $instance, $this->id_base);
@ -83,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
)
)"));

View File

@ -17,7 +17,6 @@ if (!session_id()) session_start();
**/
load_plugin_textdomain('woothemes', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/');
/**
* Constants
**/
@ -25,7 +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
**/
@ -43,7 +41,6 @@ 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
**/
@ -55,14 +52,10 @@ if (is_admin()) :
**/
register_activation_hook( __FILE__, 'install_woocommerce' );
function woocommerce_update_check() {
if (get_site_option('woocommerce_db_version') != WOOCOMMERCE_VERSION) install_woocommerce();
}
add_action('init', 'woocommerce_update_check');
if (get_site_option('woocommerce_db_version') != WOOCOMMERCE_VERSION) add_action('init', 'install_woocommerce');
endif;
/**
* Include classes
*/
@ -78,22 +71,25 @@ include_once( 'classes/product_variation.class.php' );
include_once( 'classes/tax.class.php' );
include_once( 'classes/validation.class.php' );
/**
* Include shipping modules
* Include core shipping modules
*/
include_once( 'classes/shipping/shipping-flat_rate.php' );
include_once( 'classes/shipping/shipping-free_shipping.php' );
/**
* Include payment gateways
* Include core payment gateways
*/
include_once( 'classes/gateways/gateway-banktransfer.php' );
include_once( 'classes/gateways/gateway-cheque.php' );
include_once( 'classes/gateways/gateway-moneybookers.php' );
include_once( 'classes/gateways/gateway-paypal.php' );
//include_once( 'classes/gateways/gateway-payfast.php' );
/**
* Queue addon gateways and shipping methods
**/
add_action('plugins_loaded', 'woocommerce_shipping::init', 1); // Load shipping methods - some may be added by plugins
add_action('plugins_loaded', 'woocommerce_payment_gateways::init', 1); // Load payment methods - some may be added by plugins
/**
* Init class singletons
@ -103,57 +99,25 @@ $woocommerce_customer = woocommerce_customer::get(); // Customer class, so
$woocommerce_shipping = woocommerce_shipping::get(); // Shipping class. loads and stores shipping methods
$woocommerce_payment_gateways = woocommerce_payment_gateways::get(); // Payment gateways class. loads and stores payment methods
$woocommerce_cart = woocommerce_cart::get(); // Cart class, stores the cart contents
/**
* Add post thumbnail support to wordpress
**/
if ( !current_theme_supports( 'post-thumbnails' ) ) :
add_theme_support( 'post-thumbnails' );
add_action( 'init', 'woocommerce_remove_post_type_thumbnail_support' );
endif;
function woocommerce_remove_post_type_thumbnail_support() {
remove_post_type_support( 'post', 'thumbnail' );
remove_post_type_support( 'page', 'thumbnail' );
}
/**
* Filters and hooks
**/
add_action('init', 'woocommerce_init', 0);
add_action('plugins_loaded', 'woocommerce_shipping::init', 1); // Load shipping methods - some may be added by plugins
add_action('plugins_loaded', 'woocommerce_payment_gateways::init', 1); // Load payment methods - some may be added by plugins
if (get_option('woocommerce_force_ssl_checkout')=='yes') add_action( 'wp_head', 'woocommerce_force_ssl');
add_action( 'wp_footer', 'woocommerce_demo_store' );
add_action( 'wp_footer', 'woocommerce_sharethis' );
/**
* IIS compat fix/fallback
**/
if (!isset($_SERVER['REQUEST_URI'])) {
$_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],1 );
if (isset($_SERVER['QUERY_STRING'])) { $_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING']; }
}
/**
* Init WooCommerce
**/
add_action('init', 'woocommerce_init', 0);
function woocommerce_init() {
ob_start();
woocommerce_post_type();
// Constants
if (!defined('WOOCOMMERCE_USE_CSS')) :
if (get_option('woocommerce_frontend_css')=='no') define('WOOCOMMERCE_USE_CSS', false);
else define('WOOCOMMERCE_USE_CSS', true);
// Post thumbnail support
if ( !current_theme_supports( 'post-thumbnails' ) ) :
add_theme_support( 'post-thumbnails' );
remove_post_type_support( 'post', 'thumbnail' );
remove_post_type_support( 'page', 'thumbnail' );
endif;
// Image sizes
add_image_size( 'shop_thumbnail', woocommerce::get_var('shop_thumbnail_image_width'), woocommerce::get_var('shop_thumbnail_image_height'), 'true' );
add_image_size( 'shop_catalog', woocommerce::get_var('shop_catalog_image_width'), woocommerce::get_var('shop_catalog_image_height'), 'true' );
@ -162,33 +126,58 @@ function woocommerce_init() {
// Include template functions here so they are pluggable by themes
include_once( 'woocommerce_template_functions.php' );
ob_start();
$css = file_exists(get_stylesheet_directory() . '/woocommerce/style.css') ? get_stylesheet_directory_uri() . '/woocommerce/style.css' : woocommerce::plugin_url() . '/assets/css/woocommerce.css';
if (WOOCOMMERCE_USE_CSS) wp_register_style('woocommerce_frontend_styles', $css );
if (is_admin()) :
wp_register_style('woocommerce_admin_styles', woocommerce::plugin_url() . '/assets/css/admin.css');
wp_enqueue_style('woocommerce_admin_styles');
wp_enqueue_style('jquery-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
wp_enqueue_style( 'woocommerce_admin_styles', woocommerce::plugin_url() . '/assets/css/admin.css' );
wp_enqueue_style( 'jquery-ui-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css' );
else :
wp_register_style( 'woocommerce_fancybox_styles', woocommerce::plugin_url() . '/assets/css/fancybox.css' );
wp_register_style( 'jqueryui_styles', woocommerce::plugin_url() . '/assets/css/ui.css' );
wp_enqueue_style('woocommerce_frontend_styles');
wp_enqueue_style('woocommerce_fancybox_styles');
wp_enqueue_style('jqueryui_styles');
// Optional front end css
if ((defined('WOOCOMMERCE_USE_CSS') && WOOCOMMERCE_USE_CSS) || (!defined('WOOCOMMERCE_USE_CSS') && get_option('woocommerce_frontend_css')=='yes')) :
$css = file_exists(get_stylesheet_directory() . '/woocommerce/style.css') ? get_stylesheet_directory_uri() . '/woocommerce/style.css' : woocommerce::plugin_url() . '/assets/css/woocommerce.css';
wp_register_style('woocommerce_frontend_styles', $css );
wp_enqueue_style( 'woocommerce_frontend_styles' );
endif;
wp_enqueue_style( 'woocommerce_fancybox_styles', woocommerce::plugin_url() . '/assets/css/fancybox.css' );
wp_enqueue_style( 'jquery-ui-style', woocommerce::plugin_url() . '/assets/css/ui.css' );
endif;
}
function woocommerce_admin_scripts() {
wp_register_script( 'woocommerce_admin', woocommerce::plugin_url() . '/assets/js/woocommerce_admin.js', array('jquery', 'jquery-ui-widget'), '1.0' );
wp_enqueue_script('woocommerce_admin');
}
add_action('admin_print_scripts', 'woocommerce_admin_scripts');
/**
* Set up Roles & Capabilities
**/
add_action('init', 'woocommerce_init_roles');
function woocommerce_init_roles() {
global $wp_roles;
if (class_exists('WP_Roles')) if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles();
if (is_object($wp_roles)) :
// Customer role
add_role('customer', __('Customer', 'woothemes'), array(
'read' => true,
'edit_posts' => false,
'delete_posts' => false
));
// Shop manager role
add_role('shop_manager', __('Shop Manager', 'woothemes'), array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
));
// Main Shop capabilities
$wp_roles->add_cap( 'administrator', 'manage_woocommerce' );
$wp_roles->add_cap( 'shop_manager', 'manage_woocommerce' );
endif;
}
/**
* Enqueue frontend scripts
**/
function woocommerce_frontend_scripts() {
wp_register_script( 'woocommerce', woocommerce::plugin_url() . '/assets/js/woocommerce.js', 'jquery', '1.0' );
@ -234,72 +223,42 @@ function woocommerce_frontend_scripts() {
}
add_action('template_redirect', 'woocommerce_frontend_scripts');
/**
* Demo Banner
*
* Adds a demo store banner to the site if enabled
**/
function woocommerce_demo_store() {
if (get_option('woocommerce_demo_store')=='yes') :
echo '<p class="demo_store">'.__('This is a demo store for testing purposes &mdash; no orders shall be fulfilled.', 'woothemes').'</p>';
endif;
}
/**
* Sharethis
*
* Adds social sharing code
**/
function woocommerce_sharethis() {
if (is_single() && get_option('woocommerce_sharethis')) :
if (is_ssl()) :
$sharethis = 'https://ws.sharethis.com/button/buttons.js';
else :
$sharethis = 'http://w.sharethis.com/button/buttons.js';
endif;
echo '<script type="text/javascript">var switchTo5x=true;</script><script type="text/javascript" src="'.$sharethis.'"></script><script type="text/javascript">stLight.options({publisher:"'.get_option('woocommerce_sharethis').'"});</script>';
endif;
}
/**
* WooCommerce conditionals
**/
function is_cart() {
if (is_page(get_option('woocommerce_cart_page_id'))) return true;
return false;
if (is_page(get_option('woocommerce_cart_page_id'))) return true; else return false;
}
function is_checkout() {
if (
is_page(get_option('woocommerce_checkout_page_id'))
) return true;
return false;
if (is_page(get_option('woocommerce_checkout_page_id'))) return true; else return false;
}
if (!function_exists('is_ajax')) {
function is_ajax() {
if ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ) return true;
return false;
if ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ) return true; else return false;
}
}
/**
* Force SSL (if enabled)
**/
if (get_option('woocommerce_force_ssl_checkout')=='yes') add_action( 'wp', 'woocommerce_force_ssl');
function woocommerce_force_ssl() {
if (is_checkout() && !is_ssl()) :
wp_redirect( str_replace('http:', 'https:', get_permalink(get_option('woocommerce_checkout_page_id'))), 301 );
exit;
endif;
}
/**
* Force SSL for images
**/
add_filter('post_thumbnail_html', 'woocommerce_force_ssl_images');
add_filter('widget_text', 'woocommerce_force_ssl_images');
add_filter('wp_get_attachment_url', 'woocommerce_force_ssl_images');
add_filter('wp_get_attachment_image_attributes', 'woocommerce_force_ssl_images');
add_filter('wp_get_attachment_url', 'woocommerce_force_ssl_images');
function woocommerce_force_ssl_images( $content ) {
if (is_ssl()) :
if (is_array($content)) :
@ -310,12 +269,14 @@ function woocommerce_force_ssl_images( $content ) {
endif;
return $content;
}
add_filter('post_thumbnail_html', 'woocommerce_force_ssl_images');
add_filter('widget_text', 'woocommerce_force_ssl_images');
add_filter('wp_get_attachment_url', 'woocommerce_force_ssl_images');
add_filter('wp_get_attachment_image_attributes', 'woocommerce_force_ssl_images');
add_filter('wp_get_attachment_url', 'woocommerce_force_ssl_images');
/**
* IIS compatability fix/fallback
**/
if (!isset($_SERVER['REQUEST_URI'])) {
$_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],1 );
if (isset($_SERVER['QUERY_STRING'])) { $_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING']; }
}
/**
* Currency
@ -354,7 +315,6 @@ function get_woocommerce_currency_symbol() {
return apply_filters('woocommerce_currency_symbol', $currency_symbol, $currency);
}
/**
* Price Formatting
**/
@ -390,7 +350,6 @@ function woocommerce_price( $price, $args = array() ) {
return $return;
}
/**
* Variation Formatting
**/
@ -430,38 +389,13 @@ function woocommerce_get_formatted_variation( $variation = '', $flat = false ) {
endif;
}
/**
* Letter to number
**/
function woocommerce_let_to_num($v) {
$l = substr($v, -1);
$ret = substr($v, 0, -1);
switch(strtoupper($l)){
case 'P':
$ret *= 1024;
case 'T':
$ret *= 1024;
case 'G':
$ret *= 1024;
case 'M':
$ret *= 1024;
case 'K':
$ret *= 1024;
break;
}
return $ret;
}
/**
* Clean variables
**/
function woocommerce_clean( $var ) {
return strip_tags(stripslashes(trim($var)));
return trim(strip_tags(stripslashes($var)));
}
/**
* Rating field for comments
**/
@ -486,7 +420,6 @@ function woocommerce_check_comment_rating($comment_data) {
}
add_filter('preprocess_comment', 'woocommerce_check_comment_rating', 0);
/**
* Review comments template
**/
@ -517,7 +450,6 @@ function woocommerce_comments($comment, $args, $depth) {
<?php
}
/**
* Exclude order comments from front end
**/
@ -539,37 +471,3 @@ function woocommerce_exclude_order_comments( $clauses ) {
}
if (!is_admin()) add_filter('comments_clauses', 'woocommerce_exclude_order_comments');
/**
* Set up Roles & Capabilities
**/
function woocommerce_init_roles() {
global $wp_roles;
if (class_exists('WP_Roles')) if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles();
if (is_object($wp_roles)) :
// Customer role
add_role('customer', __('Customer', 'woothemes'), array(
'read' => true,
'edit_posts' => false,
'delete_posts' => false
));
// Shop manager role
add_role('shop_manager', __('Shop Manager', 'woothemes'), array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
));
// Main Shop capabilities
$wp_roles->add_cap( 'administrator', 'manage_woocommerce' );
$wp_roles->add_cap( 'shop_manager', 'manage_woocommerce' );
endif;
}
add_action('init', 'woocommerce_init_roles');

View File

@ -15,10 +15,10 @@
* - Restore an order via a link
* - Cancel a pending order
* - Download a file
* - Order Status completed - GIVE DOWNLOADABLE PRODUCT ACCESS TO CUSTOMER
* - Order Status completed - allow customer to access Downloadable product
*
* @package WooCommerce
* @category Emails
* @category Actions
* @author WooThemes
*/

View File

@ -9,36 +9,27 @@
* @author WooThemes
*/
global $woocommerce_query;
$woocommerce_query['unfiltered_product_ids'] = array(); // Unfilted product ids (before layered nav etc)
$woocommerce_query['filtered_product_ids'] = array(); // Filted product ids (after layered nav)
$woocommerce_query['post__in'] = array(); // Product id's that match the layered nav + price filter
$woocommerce_query['meta_query'] = ''; // The meta query for the page
$woocommerce_query['layered_nav_post__in'] = array(); // posts matching layered nav only
$woocommerce_query['layered_nav_product_ids'] = array(); // Stores posts matching layered nav, so price filter can find max price in view
/**
* Query the products, applying sorting/ordering etc. This applies to the main wordpress loop
*/
if (!is_admin()) add_filter( 'parse_query', 'woocommerce_parse_query' );
add_filter( 'parse_query', 'woocommerce_parse_query' );
function woocommerce_parse_query( $q ) {
if (is_admin()) return;
// Apply to main loop only
remove_filter( 'parse_query', 'woocommerce_parse_query' );
// Only apply to product categories, the product post archive, the shop page, and product tags
if (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;
global $woocommerce_query;
$woocommerce_query['meta_query'] = (array) $q->get( 'meta_query' );
$meta_query = (array) $q->get( 'meta_query' );
// Visibility
if ( is_search() ) $in = array( 'visible', 'search' ); else $in = array( 'visible', 'catalog' );
$woocommerce_query['meta_query'][] = array(
$meta_query[] = array(
'key' => 'visibility',
'value' => $in,
'compare' => 'IN'
@ -46,7 +37,7 @@ function woocommerce_parse_query( $q ) {
// In stock
if (get_option('woocommerce_hide_out_of_stock_items')=='yes') :
$woocommerce_query['meta_query'][] = array(
$meta_query[] = array(
'key' => 'stock_status',
'value' => 'instock',
'compare' => '='
@ -76,7 +67,7 @@ function woocommerce_parse_query( $q ) {
endswitch;
// Get a list of post id's which match the current filters set (in the layered nav and price filter)
$woocommerce_query['post__in'] = array_unique(apply_filters('loop-shop-posts-in', array()));
$post__in = array_unique(apply_filters('loop-shop-posts-in', array()));
// Ordering query vars
$q->set( 'orderby', $orderby );
@ -85,9 +76,13 @@ function woocommerce_parse_query( $q ) {
// Query vars that affect posts shown
$q->set( 'post_type', 'product' );
$q->set( 'meta_query', $woocommerce_query['meta_query'] );
$q->set( 'post__in', $woocommerce_query['post__in'] );
$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
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);
@ -98,7 +93,9 @@ function woocommerce_parse_query( $q ) {
*/
function woocommerce_get_products_in_view() {
global $woocommerce_query, $wp_query;
global $wp_query;
$unfiltered_product_ids = array();
// Get all visible posts, regardless of filters
$products = get_posts(
@ -108,150 +105,28 @@ 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']
)
)
);
// Add posts to array
foreach ($products as $p) $woocommerce_query['unfiltered_product_ids'][] = $p->ID;
foreach ($products as $p) $unfiltered_product_ids[] = $p->ID;
// Store the variable
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;
}
/**
* Layered Nav Init
*/
add_action('init', 'woocommerce_layered_nav_init', 1);
function woocommerce_layered_nav_init() {
global $_chosen_attributes, $wpdb;
$_chosen_attributes = array();
$attribute_taxonomies = woocommerce::$attribute_taxonomies;
if ( $attribute_taxonomies ) :
foreach ($attribute_taxonomies as $tax) :
$attribute = strtolower(sanitize_title($tax->attribute_name));
$taxonomy = woocommerce::attribute_name($attribute);
$name = 'filter_' . $attribute;
if (isset($_GET[$name]) && taxonomy_exists($taxonomy)) $_chosen_attributes[$taxonomy] = explode(',', $_GET[$name] );
endforeach;
endif;
}
/**
* Layered Nav
*/
add_filter('loop-shop-posts-in', 'woocommerce_layered_nav_query');
function woocommerce_layered_nav_query( $filtered_posts ) {
global $_chosen_attributes, $wpdb, $woocommerce_query;
if (sizeof($_chosen_attributes)>0) :
$matched_products = array();
$filtered = false;
foreach ($_chosen_attributes as $attribute => $values) :
if (sizeof($values)>0) :
foreach ($values as $value) :
$posts = get_objects_in_term( $value, $attribute );
if (!is_wp_error($posts) && (sizeof($matched_products)>0 || $filtered)) :
$matched_products = array_intersect($posts, $matched_products);
elseif (!is_wp_error($posts)) :
$matched_products = $posts;
endif;
$filtered = true;
endforeach;
endif;
endforeach;
if ($filtered) :
$woocommerce_query['layered_nav_post__in'] = $matched_products;
$woocommerce_query['layered_nav_post__in'][] = 0;
if (sizeof($filtered_posts)==0) :
$filtered_posts = $matched_products;
$filtered_posts[] = 0;
else :
$filtered_posts = array_intersect($filtered_posts, $matched_products);
$filtered_posts[] = 0;
endif;
endif;
endif;
return (array) $filtered_posts;
}
/**
* Price Filtering
*/
add_filter('loop-shop-posts-in', 'woocommerce_price_filter');
function woocommerce_price_filter( $filtered_posts ) {
if (isset($_GET['max_price']) && isset($_GET['min_price'])) :
$matched_products = array();
$matched_products_query = get_posts(array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'price',
'value' => array( $_GET['min_price'], $_GET['max_price'] ),
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
)
)
));
if ($matched_products_query) :
foreach ($matched_products_query as $product) :
$matched_products[] = $product->ID;
if ($product->post_parent>0) $matched_products[] = $product->post_parent;
endforeach;
endif;
// Filter the id's
if (sizeof($filtered_posts)==0) :
$filtered_posts = $matched_products;
$filtered_posts[] = 0;
else :
$filtered_posts = array_intersect($filtered_posts, $matched_products);
$filtered_posts[] = 0;
endif;
endif;
return (array) $filtered_posts;
}

View File

@ -89,3 +89,6 @@ function woocommerce_body_classes_check () {
/* Cart */
add_action('cart-collaterals', 'woocommerce_cross_sell_display');
/* Footer */
add_action( 'wp_footer', 'woocommerce_demo_store' );

View File

@ -204,11 +204,36 @@ if (!function_exists('woocommerce_template_single_sharing')) {
<iframe src="https://www.facebook.com/plugins/like.php?href='.urlencode(get_permalink($post->ID)).'&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;" allowTransparency="true"></iframe>
<span class="st_twitter"></span><span class="st_email"></span><span class="st_sharethis"></span><span class="st_plusone_button"></span>
</div>';
add_action( 'wp_footer', 'woocommerce_sharethis_script' );
endif;
}
}
/**
* Sharethis
*
* Adds social sharing code to the footer
**/
if (!function_exists('woocommerce_sharethis_script')) {
function woocommerce_sharethis_script() {
if (is_single() && get_option('woocommerce_sharethis')) :
if (is_ssl()) :
$sharethis = 'https://ws.sharethis.com/button/buttons.js';
else :
$sharethis = 'http://w.sharethis.com/button/buttons.js';
endif;
echo '<script type="text/javascript">var switchTo5x=true;</script><script type="text/javascript" src="'.$sharethis.'"></script><script type="text/javascript">stLight.options({publisher:"'.get_option('woocommerce_sharethis').'"});</script>';
endif;
}
}
/**
* Product Add to cart buttons
**/
@ -909,4 +934,15 @@ function woocommerce_order_review() {
woocommerce_get_template('checkout/review_order.php', false);
}
/**
* Demo Banner
*
* Adds a demo store banner to the site if enabled
**/
function woocommerce_demo_store() {
if (get_option('woocommerce_demo_store')=='yes') :
echo '<p class="demo_store">'.__('This is a demo store for testing purposes &mdash; no orders shall be fulfilled.', 'woothemes').'</p>';
endif;
}