2011-12-09 17:01:56 +00:00
< ? php
/**
* WooCommerce Core Functions
2012-08-08 08:58:36 +00:00
*
* Functions available on both the front - end and admin .
2011-12-09 17:01:56 +00:00
*
2012-08-14 15:30:23 +00:00
* @ author WooThemes
* @ category Core
* @ package WooCommerce / Functions
* @ version 1.6 . 4
2011-12-09 17:01:56 +00:00
*/
2012-08-08 08:58:36 +00:00
2012-10-15 10:57:58 +00:00
if ( ! defined ( 'ABSPATH' ) ) exit ; // Exit if accessed directly
2012-11-08 16:57:53 +00:00
/**
* Hooks used in admin and frontend
*/
add_filter ( 'woocommerce_coupon_code' , 'sanitize_text_field' );
add_filter ( 'woocommerce_coupon_code' , 'strtolower' ); // Coupons case-insensitive by default
2012-11-29 16:48:40 +00:00
add_filter ( 'woocommerce_stock_amount' , 'intval' ); // Stock amounts are integers by default
2012-11-08 16:57:53 +00:00
2012-11-21 18:07:45 +00:00
/**
2012-11-27 16:22:47 +00:00
* Main function for returning products , uses the WC_Product_Factory class .
*
2012-11-21 18:07:45 +00:00
* @ access public
* @ param mixed $the_product Post object or post ID of the product .
2012-11-22 11:48:16 +00:00
* @ param array $args ( default : array ()) Contains all arguments to be used to get this product .
2012-11-21 18:07:45 +00:00
* @ return void
*/
2012-11-22 11:48:16 +00:00
function get_product ( $the_product = false , $args = array () ) {
2012-11-22 13:00:25 +00:00
global $woocommerce ;
return $woocommerce -> product_factory -> get_product ( $the_product , $args );
2012-11-21 18:07:45 +00:00
}
2012-12-05 15:01:29 +00:00
function woocommerce_sanitize_taxonomy_name ( $taxonomy ) {
return str_replace ( array ( ' ' , '_' ), '-' , strtolower ( $taxonomy ) );
}
2012-12-20 12:35:36 +00:00
/**
* woocommerce_get_attachment_image_attributes function .
*
* @ access public
* @ param mixed $attr
* @ return void
*/
function woocommerce_get_attachment_image_attributes ( $attr ) {
if ( strstr ( $attr [ 'src' ], 'woocommerce_uploads/' ) )
$attr [ 'src' ] = woocommerce_placeholder_img_src ();
return $attr ;
}
add_filter ( 'wp_get_attachment_image_attributes' , 'woocommerce_get_attachment_image_attributes' );
function woocommerce_prepare_attachment_for_js ( $response ) {
if ( isset ( $response [ 'url' ] ) && strstr ( $response [ 'url' ], 'woocommerce_uploads/' ) ) {
$response [ 'full' ][ 'url' ] = woocommerce_placeholder_img_src ();
if ( $response [ 'sizes' ] ) {
foreach ( $response [ 'sizes' ] as $size => $value ) {
$response [ 'sizes' ][ $size ][ 'url' ] = woocommerce_placeholder_img_src ();
}
}
}
return $response ;
}
add_filter ( 'wp_prepare_attachment_for_js' , 'woocommerce_prepare_attachment_for_js' );
2012-04-10 12:45:07 +00:00
/**
* woocommerce_get_dimension function .
2012-08-08 08:58:36 +00:00
*
2012-04-10 12:45:07 +00:00
* Normalise dimensions , unify to cm then convert to wanted unit value
*
* Usage : woocommerce_get_dimension ( 55 , 'in' );
*
* @ access public
* @ param mixed $dim
2012-08-14 15:30:23 +00:00
* @ param mixed $to_unit 'in' , 'm' , 'cm' , 'm'
* @ return float
2012-04-10 12:45:07 +00:00
*/
function woocommerce_get_dimension ( $dim , $to_unit ) {
2012-08-08 08:58:36 +00:00
2012-04-18 12:18:59 +00:00
$from_unit = strtolower ( get_option ( 'woocommerce_dimension_unit' ) );
2012-04-10 12:45:07 +00:00
$to_unit = strtolower ( $to_unit );
2012-08-08 08:58:36 +00:00
2012-04-10 12:45:07 +00:00
// Unify all units to cm first
if ( $from_unit !== $to_unit ) {
switch ( $from_unit ) {
case 'in' :
$dim *= 2.54 ;
break ;
case 'm' :
$dim *= 100 ;
break ;
case 'mm' :
$dim *= 0.1 ;
break ;
2012-04-18 12:18:59 +00:00
case 'yd' :
$dim *= 0.010936133 ;
break ;
2012-04-10 12:45:07 +00:00
}
// Output desired unit
switch ( $to_unit ) {
case 'in' :
$dim *= 0.3937 ;
break ;
case 'm' :
$dim *= 0.01 ;
break ;
case 'mm' :
$dim *= 10 ;
break ;
2012-04-18 12:18:59 +00:00
case 'yd' :
$dim *= 91.44 ;
break ;
2012-04-10 12:45:07 +00:00
}
}
2012-04-10 12:50:21 +00:00
return ( $dim < 0 ) ? 0 : $dim ;
2012-04-10 12:45:07 +00:00
}
2012-08-14 15:30:23 +00:00
2012-04-10 12:45:07 +00:00
/**
* woocommerce_get_weight function .
2012-08-08 08:58:36 +00:00
*
2012-04-10 12:45:07 +00:00
* Normalise weights , unify to cm then convert to wanted unit value
*
* Usage : woocommerce_get_weight ( 55 , 'kg' );
*
* @ access public
* @ param mixed $weight
2012-08-14 15:30:23 +00:00
* @ param mixed $to_unit 'g' , 'kg' , 'lbs'
* @ return float
2012-04-10 12:45:07 +00:00
*/
function woocommerce_get_weight ( $weight , $to_unit ) {
2012-08-08 08:58:36 +00:00
2012-04-10 12:45:07 +00:00
$from_unit = strtolower ( get_option ( 'woocommerce_weight_unit' ) );
$to_unit = strtolower ( $to_unit );
2012-08-08 08:58:36 +00:00
2012-04-10 12:45:07 +00:00
//Unify all units to kg first
if ( $from_unit !== $to_unit ) {
2012-08-08 08:58:36 +00:00
2012-04-10 12:45:07 +00:00
switch ( $from_unit ) {
case 'g' :
$weight *= 0.001 ;
break ;
case 'lbs' :
2012-06-29 19:24:43 +00:00
$weight *= 0.4536 ;
break ;
case 'oz' :
$weight *= 0.0283 ;
2012-04-10 12:45:07 +00:00
break ;
}
// Output desired unit
switch ( $to_unit ) {
case 'g' :
$weight *= 1000 ;
break ;
case 'lbs' :
2012-06-29 19:24:43 +00:00
$weight *= 2.2046 ;
break ;
case 'oz' :
$weight *= 35.274 ;
2012-04-10 12:45:07 +00:00
break ;
}
}
2012-04-10 12:50:21 +00:00
return ( $weight < 0 ) ? 0 : $weight ;
2012-04-10 12:45:07 +00:00
}
2012-02-24 16:23:08 +00:00
/**
2012-11-27 15:37:01 +00:00
* Get the placeholder image URL for products etc
2012-08-14 15:30:23 +00:00
*
* @ access public
* @ return string
*/
2012-02-24 16:23:08 +00:00
function woocommerce_placeholder_img_src () {
global $woocommerce ;
2012-08-08 08:58:36 +00:00
return apply_filters ( 'woocommerce_placeholder_img_src' , $woocommerce -> plugin_url () . '/assets/images/placeholder.png' );
2012-02-24 16:23:08 +00:00
}
2012-08-08 08:58:36 +00:00
2012-08-14 15:30:23 +00:00
2012-11-27 15:37:01 +00:00
/**
* Get the placeholder image
*
* @ access public
* @ return string
*/
function woocommerce_placeholder_img ( $size = 'shop_thumbnail' ) {
global $woocommerce ;
2012-11-27 16:22:47 +00:00
2012-11-27 15:37:01 +00:00
$dimensions = $woocommerce -> get_image_size ( $size );
2012-11-27 16:22:47 +00:00
2012-11-27 15:37:01 +00:00
return apply_filters ( 'woocommerce_placeholder_img' , '<img src="' . woocommerce_placeholder_img_src () . '" alt="Placeholder" width="' . $dimensions [ 'width' ] . '" height="' . $dimensions [ 'height' ] . '" />' );
}
2012-12-30 15:51:37 +00:00
/**
* woocommerce_lostpassword_url function .
*
* @ access public
* @ param mixed $url
* @ return void
*/
function woocommerce_lostpassword_url ( $url ) {
$id = woocommerce_get_page_id ( 'lost_password' );
if ( $id != - 1 )
$url = get_permalink ( $id );
return $url ;
}
add_filter ( 'lostpassword_url' , 'woocommerce_lostpassword_url' );
2011-12-10 17:28:32 +00:00
/**
2012-08-14 15:30:23 +00:00
* Send HTML emails from WooCommerce
*
* @ access public
* @ param mixed $to
* @ param mixed $subject
* @ param mixed $message
* @ param string $headers ( default : " Content-Type: text/html \r \n " )
* @ param string $attachments ( default : " " )
* @ return void
*/
2011-12-10 17:28:32 +00:00
function woocommerce_mail ( $to , $subject , $message , $headers = " Content-Type: text/html \r \n " , $attachments = " " ) {
global $woocommerce ;
2012-08-08 08:58:36 +00:00
2011-12-10 17:28:32 +00:00
$mailer = $woocommerce -> mailer ();
2012-08-08 08:58:36 +00:00
2011-12-10 17:28:32 +00:00
$mailer -> send ( $to , $subject , $message , $headers , $attachments );
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'woocommerce_get_page_id' ) ) {
2012-08-14 15:30:23 +00:00
/**
* WooCommerce page IDs
*
2012-12-30 15:51:37 +00:00
* retrieve page ids - used for myaccount , edit_address , change_password , shop , cart , checkout , pay , view_order , thanks , terms
2012-08-14 15:30:23 +00:00
*
* returns - 1 if no page is found
*
* @ access public
* @ param string $page
* @ return int
*/
2012-01-06 17:14:31 +00:00
function woocommerce_get_page_id ( $page ) {
2012-01-25 20:13:54 +00:00
$page = apply_filters ( 'woocommerce_get_' . $page . '_page_id' , get_option ( 'woocommerce_' . $page . '_page_id' ));
2012-08-14 15:30:23 +00:00
return ( $page ) ? $page : - 1 ;
2012-01-06 17:14:31 +00:00
}
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'woocommerce_empty_cart' ) ) {
2012-08-14 15:30:23 +00:00
/**
* WooCommerce clear cart
*
* Clears the cart session when called
*
* @ access public
* @ return void
*/
2012-02-01 19:11:36 +00:00
function woocommerce_empty_cart () {
global $woocommerce ;
2012-08-08 08:58:36 +00:00
2012-08-14 15:30:23 +00:00
if ( ! isset ( $woocommerce -> cart ) || $woocommerce -> cart == '' )
$woocommerce -> cart = new WC_Cart ();
2012-08-08 08:58:36 +00:00
2012-03-04 12:37:41 +00:00
$woocommerce -> cart -> empty_cart ( false );
}
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'woocommerce_disable_admin_bar' ) ) {
2012-08-14 15:30:23 +00:00
/**
* WooCommerce disable admin bar
*
* @ access public
* @ param bool $show_admin_bar
* @ return bool
*/
2012-07-23 18:18:07 +00:00
function woocommerce_disable_admin_bar ( $show_admin_bar ) {
2012-11-05 16:50:24 +00:00
if ( get_option ( 'woocommerce_lock_down_admin' ) == 'yes' && ! ( current_user_can ( 'edit_posts' ) || current_user_can ( 'manage_woocommerce' ) ) ) {
2012-07-23 18:18:07 +00:00
$show_admin_bar = false ;
2012-03-16 11:18:53 +00:00
}
2012-08-08 08:58:36 +00:00
2012-07-23 18:18:07 +00:00
return $show_admin_bar ;
2012-03-12 09:43:22 +00:00
}
}
2012-08-14 15:30:23 +00:00
2012-03-04 12:37:41 +00:00
/**
* Load the cart upon login
2012-08-14 15:30:23 +00:00
*
* @ access public
* @ param mixed $user_login
* @ param mixed $user
* @ return void
*/
2012-03-04 12:37:41 +00:00
function woocommerce_load_persistent_cart ( $user_login , $user ) {
global $woocommerce ;
2012-08-08 08:58:36 +00:00
2012-03-04 12:37:41 +00:00
$saved_cart = get_user_meta ( $user -> ID , '_woocommerce_persistent_cart' , true );
2012-08-08 08:58:36 +00:00
2012-09-07 17:26:13 +00:00
if ( $saved_cart )
if ( empty ( $woocommerce -> session -> cart ) || ! is_array ( $woocommerce -> session -> cart ) || sizeof ( $woocommerce -> session -> cart ) == 0 )
$woocommerce -> session -> cart = $saved_cart [ 'cart' ];
2012-02-01 19:11:36 +00:00
}
2011-12-09 17:01:56 +00:00
/**
2011-12-09 19:55:09 +00:00
* is_woocommerce - Returns true if on a page which uses WooCommerce templates ( cart and checkout are standard pages with shortcodes and thus are not included )
2012-08-14 16:06:10 +00:00
*
* @ access public
* @ return bool
*/
2011-12-09 17:01:56 +00:00
function is_woocommerce () {
2012-08-14 16:06:10 +00:00
return ( is_shop () || is_product_category () || is_product_tag () || is_product () ) ? true : false ;
2011-12-09 17:01:56 +00:00
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'is_shop' ) ) {
2012-08-14 16:06:10 +00:00
/**
* is_shop - Returns true when viewing the product type archive ( shop ) .
*
* @ access public
* @ return bool
*/
2011-12-09 17:01:56 +00:00
function is_shop () {
2012-08-14 16:06:10 +00:00
return ( is_post_type_archive ( 'product' ) || is_page ( woocommerce_get_page_id ( 'shop' ) ) ) ? true : false ;
2011-12-09 17:01:56 +00:00
}
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'is_product_category' ) ) {
2012-08-14 16:06:10 +00:00
/**
* is_product_category - Returns true when viewing a product category .
*
* @ access public
* @ param string $term ( default : '' ) The term slug your checking for . Leave blank to return true on any .
* @ return bool
*/
2012-04-20 17:36:26 +00:00
function is_product_category ( $term = '' ) {
return is_tax ( 'product_cat' , $term );
2011-12-09 17:01:56 +00:00
}
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'is_product_tag' ) ) {
2012-08-14 16:06:10 +00:00
/**
* is_product_tag - Returns true when viewing a product tag .
*
* @ access public
* @ param string $term ( default : '' ) The term slug your checking for . Leave blank to return true on any .
* @ return bool
*/
2012-04-20 17:36:26 +00:00
function is_product_tag ( $term = '' ) {
return is_tax ( 'product_tag' , $term );
2011-12-09 17:01:56 +00:00
}
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'is_product' ) ) {
2012-08-14 16:06:10 +00:00
/**
* is_product - Returns true when viewing a single product .
*
* @ access public
* @ return bool
*/
2011-12-09 17:01:56 +00:00
function is_product () {
2012-08-14 16:06:10 +00:00
return is_singular ( array ( 'product' ) );
2011-12-09 17:01:56 +00:00
}
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'is_cart' ) ) {
2012-08-14 16:06:10 +00:00
/**
* is_cart - Returns true when viewing the cart page .
*
* @ access public
* @ return bool
*/
2011-12-09 17:01:56 +00:00
function is_cart () {
2012-08-14 16:06:10 +00:00
return is_page ( woocommerce_get_page_id ( 'cart' ) );
2011-12-09 17:01:56 +00:00
}
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'is_checkout' ) ) {
2012-08-14 16:06:10 +00:00
/**
* is_checkout - Returns true when viewing the checkout page .
*
* @ access public
* @ return bool
*/
2011-12-09 17:01:56 +00:00
function is_checkout () {
2012-08-14 16:06:10 +00:00
return ( is_page ( woocommerce_get_page_id ( 'checkout' ) ) || is_page ( woocommerce_get_page_id ( 'pay' ) ) ) ? true : false ;
2011-12-09 17:01:56 +00:00
}
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'is_account_page' ) ) {
2012-08-14 16:06:10 +00:00
/**
* is_account_page - Returns true when viewing an account page .
*
* @ access public
* @ return bool
*/
2011-12-09 17:01:56 +00:00
function is_account_page () {
2012-12-27 18:43:26 +00:00
return is_page ( woocommerce_get_page_id ( 'myaccount' ) ) || is_page ( woocommerce_get_page_id ( 'edit_address' ) ) || is_page ( woocommerce_get_page_id ( 'view_order' ) ) || is_page ( woocommerce_get_page_id ( 'change_password' ) ) || is_page ( woocommerce_get_page_id ( 'lost_password' ) ) || apply_filters ( 'woocommerce_is_account_page' , false ) ? true : false ;
2011-12-09 17:01:56 +00:00
}
}
2012-11-15 20:12:51 +00:00
if ( ! function_exists ( 'is_order_received_page' ) ) {
/**
* is_order_received_page - Returns true when viewing the order received page .
*
* @ access public
* @ return bool
*/
function is_order_received_page () {
return ( is_page ( woocommerce_get_page_id ( 'thanks' ) ) ) ? true : false ;
}
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'is_ajax' ) ) {
2012-08-14 16:06:10 +00:00
/**
* is_ajax - Returns true when the page is loaded via ajax .
*
* @ access public
* @ return bool
*/
2011-12-09 17:01:56 +00:00
function is_ajax () {
2012-08-14 16:06:10 +00:00
if ( defined ( 'DOING_AJAX' ) )
return true ;
return ( isset ( $_SERVER [ 'HTTP_X_REQUESTED_WITH' ] ) && strtolower ( $_SERVER [ 'HTTP_X_REQUESTED_WITH' ] ) == 'xmlhttprequest' ) ? true : false ;
2011-12-09 17:01:56 +00:00
}
}
2012-10-14 12:06:37 +00:00
if ( ! function_exists ( 'is_filtered' ) ) {
/**
* is_filtered - Returns true when filtering products using layered nav or price sliders .
*
* @ access public
* @ return bool
*/
function is_filtered () {
2012-11-27 16:22:47 +00:00
global $_chosen_attributes ;
2012-10-14 12:06:37 +00:00
return ( sizeof ( $_chosen_attributes ) > 0 || ( isset ( $_GET [ 'max_price' ] ) && isset ( $_GET [ 'min_price' ] ) ) ) ? true : false ;
}
}
2011-12-09 17:01:56 +00:00
2012-08-14 16:06:10 +00:00
2011-12-09 17:01:56 +00:00
/**
2012-08-14 16:06:10 +00:00
* Get template part ( for templates like the shop - loop ) .
*
* @ access public
* @ param mixed $slug
* @ param string $name ( default : '' )
* @ return void
2011-12-09 19:55:09 +00:00
*/
function woocommerce_get_template_part ( $slug , $name = '' ) {
global $woocommerce ;
2012-02-19 03:33:49 +00:00
$template = '' ;
2012-08-08 08:58:36 +00:00
2012-02-22 20:50:37 +00:00
// Look in yourtheme/slug-name.php and yourtheme/woocommerce/slug-name.php
2012-08-08 08:58:36 +00:00
if ( $name )
2012-02-19 03:33:49 +00:00
$template = locate_template ( array ( " { $slug } - { $name } .php " , " { $woocommerce -> template_url } { $slug } - { $name } .php " ) );
2012-08-08 08:58:36 +00:00
2012-02-22 20:50:37 +00:00
// Get default slug-name.php
if ( ! $template && $name && file_exists ( $woocommerce -> plugin_path () . " /templates/ { $slug } - { $name } .php " ) )
$template = $woocommerce -> plugin_path () . " /templates/ { $slug } - { $name } .php " ;
// If template file doesn't exist, look in yourtheme/slug.php and yourtheme/woocommerce/slug.php
2012-08-08 08:58:36 +00:00
if ( ! $template )
2012-02-19 03:33:49 +00:00
$template = locate_template ( array ( " { $slug } .php " , " { $woocommerce -> template_url } { $slug } .php " ) );
2012-08-08 08:58:36 +00:00
if ( $template )
2012-02-19 03:33:49 +00:00
load_template ( $template , false );
2011-12-09 17:01:56 +00:00
}
2012-08-14 16:06:10 +00:00
2011-12-09 17:01:56 +00:00
/**
2012-08-14 16:06:10 +00:00
* Get other templates ( e . g . product attributes ) passing attributes and including the file .
*
* @ access public
* @ param mixed $template_name
* @ param array $args ( default : array ())
* @ param string $template_path ( default : '' )
* @ param string $default_path ( default : '' )
* @ return void
2011-12-09 19:55:09 +00:00
*/
2012-05-14 14:36:47 +00:00
function woocommerce_get_template ( $template_name , $args = array (), $template_path = '' , $default_path = '' ) {
2011-12-09 19:55:09 +00:00
global $woocommerce ;
2012-08-08 08:58:36 +00:00
if ( $args && is_array ( $args ) )
2012-02-03 16:17:35 +00:00
extract ( $args );
2012-08-08 08:58:36 +00:00
2012-05-14 14:36:47 +00:00
$located = woocommerce_locate_template ( $template_name , $template_path , $default_path );
2012-08-08 08:58:36 +00:00
2012-03-06 17:34:44 +00:00
do_action ( 'woocommerce_before_template_part' , $template_name , $template_path , $located );
2012-08-08 08:58:36 +00:00
2012-03-06 13:26:10 +00:00
include ( $located );
2012-08-08 08:58:36 +00:00
do_action ( 'woocommerce_after_template_part' , $template_name , $template_path , $located );
2012-01-29 13:36:33 +00:00
}
2012-08-14 16:06:10 +00:00
2012-01-29 13:36:33 +00:00
/**
2012-05-14 14:36:47 +00:00
* Locate a template and return the path for inclusion .
*
* This is the load order :
*
* yourtheme / $template_path / $template_name
* yourtheme / $template_name
* $default_path / $template_name
2012-08-14 16:06:10 +00:00
*
* @ access public
* @ param mixed $template_name
* @ param string $template_path ( default : '' )
* @ param string $default_path ( default : '' )
* @ return string
2012-01-29 13:36:33 +00:00
*/
2012-05-14 14:36:47 +00:00
function woocommerce_locate_template ( $template_name , $template_path = '' , $default_path = '' ) {
2012-01-29 13:36:33 +00:00
global $woocommerce ;
2012-08-08 08:58:36 +00:00
2012-05-14 14:36:47 +00:00
if ( ! $template_path ) $template_path = $woocommerce -> template_url ;
if ( ! $default_path ) $default_path = $woocommerce -> plugin_path () . '/templates/' ;
2012-08-08 08:58:36 +00:00
2012-05-14 14:36:47 +00:00
// Look within passed path within the theme - this is priority
2012-08-08 08:58:36 +00:00
$template = locate_template (
array (
2012-12-23 11:36:00 +00:00
trailingslashit ( $template_path ) . $template_name ,
2012-05-14 14:36:47 +00:00
$template_name
2012-08-08 08:58:36 +00:00
)
2012-05-14 14:36:47 +00:00
);
2012-02-03 16:17:35 +00:00
// Get default template
2012-05-14 14:36:47 +00:00
if ( ! $template )
$template = $default_path . $template_name ;
2012-04-11 10:02:35 +00:00
2012-05-14 14:36:47 +00:00
// Return what we found
2012-03-06 13:26:10 +00:00
return apply_filters ( 'woocommerce_locate_template' , $template , $template_name , $template_path );
2011-12-09 17:01:56 +00:00
}
2012-04-16 15:27:18 +00:00
2012-08-14 16:06:10 +00:00
2012-04-16 15:27:18 +00:00
/**
2012-08-14 16:06:10 +00:00
* Get Currency .
*
* @ access public
* @ return string
*/
2012-04-16 15:27:18 +00:00
function get_woocommerce_currency () {
return apply_filters ( 'woocommerce_currency' , get_option ( 'woocommerce_currency' ) );
}
2012-08-14 16:06:10 +00:00
2011-12-09 17:01:56 +00:00
/**
2012-08-14 16:06:10 +00:00
* Get Currency symbol .
*
* @ access public
* @ param string $currency ( default : '' )
* @ return string
*/
2011-12-14 12:17:05 +00:00
function get_woocommerce_currency_symbol ( $currency = '' ) {
2012-04-16 15:27:18 +00:00
if ( ! $currency ) $currency = get_woocommerce_currency ();
2011-12-09 17:01:56 +00:00
$currency_symbol = '' ;
switch ( $currency ) :
2012-06-06 18:29:47 +00:00
case 'BRL' : $currency_symbol = 'R$' ; break ;
2012-06-06 15:31:35 +00:00
case 'AUD' : $currency_symbol = '$' ; break ;
case 'CAD' : $currency_symbol = '$' ; break ;
case 'MXN' : $currency_symbol = '$' ; break ;
case 'NZD' : $currency_symbol = '$' ; break ;
case 'HKD' : $currency_symbol = '$' ; break ;
case 'SGD' : $currency_symbol = '$' ; break ;
2011-12-09 17:01:56 +00:00
case 'USD' : $currency_symbol = '$' ; break ;
case 'EUR' : $currency_symbol = '€' ; break ;
2012-06-06 15:31:35 +00:00
case 'CNY' : $currency_symbol = '¥' ; break ;
2012-08-09 10:59:14 +00:00
case 'RMB' : $currency_symbol = '¥' ; break ;
2011-12-09 17:01:56 +00:00
case 'JPY' : $currency_symbol = '¥' ; break ;
2012-06-06 15:31:35 +00:00
case 'TRY' : $currency_symbol = 'TL' ; break ;
case 'NOK' : $currency_symbol = 'kr' ; break ;
case 'ZAR' : $currency_symbol = 'R' ; break ;
2011-12-09 17:01:56 +00:00
case 'CZK' : $currency_symbol = 'Kč' ; break ;
2012-06-06 15:31:35 +00:00
case 'MYR' : $currency_symbol = 'RM' ; break ;
case 'DKK' : $currency_symbol = 'kr' ; break ;
case 'HUF' : $currency_symbol = 'Ft' ; break ;
case 'ILS' : $currency_symbol = '₪' ; break ;
case 'PHP' : $currency_symbol = '₱' ; break ;
2012-06-25 14:31:58 +00:00
case 'PLN' : $currency_symbol = 'zł' ; break ;
2012-06-06 15:31:35 +00:00
case 'SEK' : $currency_symbol = 'kr' ; break ;
case 'CHF' : $currency_symbol = 'CHF' ; break ;
case 'TWD' : $currency_symbol = 'NT$' ; break ;
case 'THB' : $currency_symbol = '฿' ; break ;
case 'GBP' : $currency_symbol = '£' ; break ;
2012-08-22 12:04:13 +00:00
case 'RON' : $currency_symbol = 'lei' ; break ;
2012-08-09 10:59:14 +00:00
default : $currency_symbol = '' ; break ;
2011-12-09 17:01:56 +00:00
endswitch ;
2012-04-16 15:27:18 +00:00
return apply_filters ( 'woocommerce_currency_symbol' , $currency_symbol , $currency );
2011-12-09 17:01:56 +00:00
}
2012-08-14 16:06:10 +00:00
2011-12-09 17:01:56 +00:00
/**
2012-08-14 16:06:10 +00:00
* Format the price with a currency symbol .
*
* @ access public
* @ param float $price
* @ param array $args ( default : array ())
* @ return string
*/
2011-12-09 17:01:56 +00:00
function woocommerce_price ( $price , $args = array () ) {
global $woocommerce ;
2012-08-08 08:58:36 +00:00
2012-08-14 16:06:10 +00:00
extract ( shortcode_atts ( array (
2011-12-09 17:01:56 +00:00
'ex_tax_label' => '0'
2012-08-14 16:06:10 +00:00
), $args ) );
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
$return = '' ;
2012-08-14 16:06:10 +00:00
$num_decimals = ( int ) get_option ( 'woocommerce_price_num_decimals' );
$currency_pos = get_option ( 'woocommerce_currency_pos' );
2011-12-09 17:01:56 +00:00
$currency_symbol = get_woocommerce_currency_symbol ();
2012-08-08 08:58:36 +00:00
2012-04-12 10:25:50 +00:00
$price = apply_filters ( 'raw_woocommerce_price' , ( double ) $price );
2012-08-08 08:58:36 +00:00
2012-08-14 16:06:10 +00:00
$price = number_format ( $price , $num_decimals , stripslashes ( get_option ( 'woocommerce_price_decimal_sep' ) ), stripslashes ( get_option ( 'woocommerce_price_thousand_sep' ) ) );
2012-08-08 08:58:36 +00:00
2012-08-14 16:06:10 +00:00
if ( get_option ( 'woocommerce_price_trim_zeros' ) == 'yes' && $num_decimals > 0 )
$price = woocommerce_trim_zeros ( $price );
2012-11-27 16:22:47 +00:00
2012-09-20 13:43:52 +00:00
$return = '<span class="amount">' . sprintf ( get_woocommerce_price_format (), $currency_symbol , $price ) . '</span>' ;
2012-08-08 08:58:36 +00:00
2012-09-20 13:43:52 +00:00
if ( $ex_tax_label && get_option ( 'woocommerce_calc_taxes' ) == 'yes' )
$return .= ' <small>' . $woocommerce -> countries -> ex_tax_or_vat () . '</small>' ;
return $return ;
}
function get_woocommerce_price_format () {
$currency_pos = get_option ( 'woocommerce_currency_pos' );
2012-11-27 16:22:47 +00:00
2012-08-14 16:06:10 +00:00
switch ( $currency_pos ) {
2011-12-09 17:01:56 +00:00
case 'left' :
2012-09-20 13:43:52 +00:00
return '%1$s%2$s' ;
2011-12-09 17:01:56 +00:00
case 'right' :
2012-09-20 13:43:52 +00:00
return '%2$s%1$s' ;
2011-12-09 17:01:56 +00:00
case 'left_space' :
2012-09-20 13:43:52 +00:00
return '%1$s %2$s' ;
2011-12-09 17:01:56 +00:00
case 'right_space' :
2012-09-20 13:43:52 +00:00
return '%2$s %1$s' ;
2012-08-14 16:06:10 +00:00
}
2012-08-08 08:58:36 +00:00
}
2012-08-14 16:06:10 +00:00
2012-01-27 15:00:12 +00:00
/**
2012-08-14 16:06:10 +00:00
* Trim trailing zeros off prices .
*
* @ access public
* @ param mixed $price
* @ return string
*/
2012-01-27 15:00:12 +00:00
function woocommerce_trim_zeros ( $price ) {
2012-08-14 16:06:10 +00:00
return preg_replace ( '/' . preg_quote ( get_option ( 'woocommerce_price_decimal_sep' ), '/' ) . '0++$/' , '' , $price );
2012-01-27 15:00:12 +00:00
}
2012-08-14 16:06:10 +00:00
2012-07-17 18:11:14 +00:00
/**
2012-08-14 16:06:10 +00:00
* Formal decimal numbers - format to 4 dp and remove trailing zeros .
*
* @ access public
* @ param mixed $number
* @ return string
*/
2012-12-20 15:10:27 +00:00
function woocommerce_format_decimal ( $number , $dp = '' ) {
if ( $dp == '' )
$dp = intval ( get_option ( 'woocommerce_price_num_decimals' ) );
$number = number_format ( ( float ) $number , ( int ) $dp , '.' , '' );
2012-11-27 16:22:47 +00:00
2012-09-30 10:14:49 +00:00
if ( strstr ( $number , '.' ) )
$number = rtrim ( rtrim ( $number , '0' ), '.' );
2012-11-27 16:22:47 +00:00
2012-09-30 10:14:49 +00:00
return $number ;
2012-08-21 18:01:56 +00:00
}
/**
* Formal total costs - format to the number of decimal places for the base currency .
*
* @ access public
* @ param mixed $number
* @ return float
*/
function woocommerce_format_total ( $number ) {
2012-12-06 19:49:00 +00:00
return number_format ( ( float ) $number , ( int ) get_option ( 'woocommerce_price_num_decimals' ), '.' , '' );
2012-07-17 18:11:14 +00:00
}
2012-08-14 16:06:10 +00:00
2011-12-09 17:01:56 +00:00
/**
* Clean variables
2012-08-14 16:06:10 +00:00
*
* @ access public
* @ param string $var
* @ return string
*/
2011-12-09 17:01:56 +00:00
function woocommerce_clean ( $var ) {
2012-10-15 19:43:07 +00:00
return sanitize_text_field ( $var );
2011-12-09 17:01:56 +00:00
}
2012-08-14 16:06:10 +00:00
2011-12-16 15:11:41 +00:00
/**
* Merge two arrays
2012-08-14 16:06:10 +00:00
*
* @ access public
* @ param array $a1
* @ param array $a2
* @ return array
*/
function woocommerce_array_overlay ( $a1 , $a2 ) {
foreach ( $a1 as $k => $v ) {
if ( ! array_key_exists ( $k , $a2 ) )
continue ;
if ( is_array ( $v ) && is_array ( $a2 [ $k ] ) ) {
$a1 [ $k ] = woocommerce_array_overlay ( $v , $a2 [ $k ] );
} else {
$a1 [ $k ] = $a2 [ $k ];
2011-12-16 15:11:41 +00:00
}
}
return $a1 ;
}
2012-08-14 16:06:10 +00:00
2012-01-10 14:55:08 +00:00
/**
* Get top term
* http :// wordpress . stackexchange . com / questions / 24794 / get - the - the - top - level - parent - of - a - custom - taxonomy - term
2012-08-14 16:06:10 +00:00
*
* @ access public
* @ param int $term_id
* @ param string $taxonomy
* @ return int
*/
function woocommerce_get_term_top_most_parent ( $term_id , $taxonomy ) {
2012-01-10 14:55:08 +00:00
// start from the current term
2012-08-14 16:06:10 +00:00
$parent = get_term_by ( 'id' , $term_id , $taxonomy );
2012-01-10 14:55:08 +00:00
// climb up the hierarchy until we reach a term with parent = '0'
2012-08-14 16:06:10 +00:00
while ( $parent -> parent != '0' ) {
2012-01-10 14:55:08 +00:00
$term_id = $parent -> parent ;
$parent = get_term_by ( 'id' , $term_id , $taxonomy );
}
return $parent ;
}
2012-08-14 16:06:10 +00:00
2011-12-09 17:01:56 +00:00
/**
* Variation Formatting
*
* Gets a formatted version of variation data or item meta
2012-08-14 16:06:10 +00:00
*
* @ access public
* @ param string $variation ( default : '' )
* @ param bool $flat ( default : false )
* @ return string
*/
2011-12-09 17:01:56 +00:00
function woocommerce_get_formatted_variation ( $variation = '' , $flat = false ) {
global $woocommerce ;
2012-08-14 16:06:10 +00:00
if ( is_array ( $variation ) ) {
2011-12-09 17:01:56 +00:00
2012-08-14 16:06:10 +00:00
if ( ! $flat )
$return = '<dl class="variation">' ;
else
$return = '' ;
2011-12-09 17:01:56 +00:00
$variation_list = array ();
2012-08-14 16:06:10 +00:00
foreach ( $variation as $name => $value ) {
2011-12-09 17:01:56 +00:00
2012-08-14 16:06:10 +00:00
if ( ! $value )
continue ;
2011-12-09 17:01:56 +00:00
// If this is a term slug, get the term's nice name
2012-08-14 16:06:10 +00:00
if ( taxonomy_exists ( esc_attr ( str_replace ( 'attribute_' , '' , $name ) ) ) ) {
$term = get_term_by ( 'slug' , $value , esc_attr ( str_replace ( 'attribute_' , '' , $name ) ) );
if ( ! is_wp_error ( $term ) && $term -> name )
2011-12-09 17:01:56 +00:00
$value = $term -> name ;
2012-08-14 16:06:10 +00:00
} else {
2011-12-09 17:01:56 +00:00
$value = ucfirst ( $value );
2012-08-14 16:06:10 +00:00
}
2011-12-09 17:01:56 +00:00
2012-08-14 16:06:10 +00:00
if ( $flat )
2011-12-09 17:01:56 +00:00
$variation_list [] = $woocommerce -> attribute_label ( str_replace ( 'attribute_' , '' , $name )) . ': ' . $value ;
2012-08-14 16:06:10 +00:00
else
2011-12-09 17:01:56 +00:00
$variation_list [] = '<dt>' . $woocommerce -> attribute_label ( str_replace ( 'attribute_' , '' , $name )) . ':</dt><dd>' . $value . '</dd>' ;
2012-08-14 16:06:10 +00:00
}
2011-12-09 17:01:56 +00:00
2012-08-14 16:06:10 +00:00
if ( $flat )
$return .= implode ( ', ' , $variation_list );
else
$return .= implode ( '' , $variation_list );
2011-12-09 17:01:56 +00:00
2012-08-14 16:06:10 +00:00
if ( ! $flat )
$return .= '</dl>' ;
2011-12-09 17:01:56 +00:00
return $return ;
2012-08-14 16:06:10 +00:00
}
2011-12-09 17:01:56 +00:00
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'woocommerce_rgb_from_hex' ) ) {
2012-08-14 16:06:10 +00:00
/**
* Hex darker / lighter / contrast functions for colours
*
* @ access public
* @ param mixed $color
* @ return string
*/
2012-06-14 11:15:18 +00:00
function woocommerce_rgb_from_hex ( $color ) {
$color = str_replace ( '#' , '' , $color );
// Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF"
$color = preg_replace ( '~^(.)(.)(.)$~' , '$1$1$2$2$3$3' , $color );
2012-08-08 08:58:36 +00:00
2012-06-14 11:15:18 +00:00
$rgb [ 'R' ] = hexdec ( $color { 0 } . $color { 1 } );
$rgb [ 'G' ] = hexdec ( $color { 2 } . $color { 3 } );
$rgb [ 'B' ] = hexdec ( $color { 4 } . $color { 5 } );
return $rgb ;
}
2012-06-14 08:28:17 +00:00
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'woocommerce_hex_darker' ) ) {
2012-08-14 16:06:10 +00:00
/**
* Hex darker / lighter / contrast functions for colours
*
* @ access public
* @ param mixed $color
* @ param int $factor ( default : 30 )
* @ return string
*/
2011-12-10 17:28:32 +00:00
function woocommerce_hex_darker ( $color , $factor = 30 ) {
2012-06-14 08:28:17 +00:00
$base = woocommerce_rgb_from_hex ( $color );
2011-12-10 17:28:32 +00:00
$color = '#' ;
2012-06-14 08:28:17 +00:00
2011-12-10 17:28:32 +00:00
foreach ( $base as $k => $v ) :
$amount = $v / 100 ;
$amount = round ( $amount * $factor );
$new_decimal = $v - $amount ;
2012-08-08 08:58:36 +00:00
2011-12-10 17:28:32 +00:00
$new_hex_component = dechex ( $new_decimal );
if ( strlen ( $new_hex_component ) < 2 ) :
$new_hex_component = " 0 " . $new_hex_component ;
endif ;
$color .= $new_hex_component ;
endforeach ;
2012-08-08 08:58:36 +00:00
return $color ;
2011-12-10 17:28:32 +00:00
}
}
2012-08-14 16:06:10 +00:00
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'woocommerce_hex_lighter' ) ) {
2012-08-14 16:06:10 +00:00
/**
* Hex darker / lighter / contrast functions for colours
*
* @ access public
* @ param mixed $color
* @ param int $factor ( default : 30 )
* @ return string
*/
2011-12-10 17:28:32 +00:00
function woocommerce_hex_lighter ( $color , $factor = 30 ) {
2012-06-14 08:28:17 +00:00
$base = woocommerce_rgb_from_hex ( $color );
2011-12-10 17:28:32 +00:00
$color = '#' ;
2012-06-14 08:28:17 +00:00
2011-12-10 17:28:32 +00:00
foreach ( $base as $k => $v ) :
2012-08-08 08:58:36 +00:00
$amount = 255 - $v ;
$amount = $amount / 100 ;
$amount = round ( $amount * $factor );
$new_decimal = $v + $amount ;
$new_hex_component = dechex ( $new_decimal );
2011-12-10 17:28:32 +00:00
if ( strlen ( $new_hex_component ) < 2 ) :
$new_hex_component = " 0 " . $new_hex_component ;
endif ;
2012-08-08 08:58:36 +00:00
$color .= $new_hex_component ;
2011-12-10 17:28:32 +00:00
endforeach ;
2012-08-08 08:58:36 +00:00
return $color ;
2011-12-10 17:28:32 +00:00
}
}
2012-05-01 18:28:45 +00:00
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'woocommerce_light_or_dark' ) ) {
2012-08-14 16:06:10 +00:00
/**
* Detect if we should use a light or dark colour on a background colour
*
* @ access public
* @ param mixed $color
* @ param string $dark ( default : '#000000' )
* @ param string $light ( default : '#FFFFFF' )
* @ return string
*/
2011-12-10 17:28:32 +00:00
function woocommerce_light_or_dark ( $color , $dark = '#000000' , $light = '#FFFFFF' ) {
2012-05-01 18:28:45 +00:00
//return ( hexdec( $color ) > 0xffffff / 2 ) ? $dark : $light;
$hex = str_replace ( '#' , '' , $color );
$c_r = hexdec ( substr ( $hex , 0 , 2 ) );
$c_g = hexdec ( substr ( $hex , 2 , 2 ) );
$c_b = hexdec ( substr ( $hex , 4 , 2 ) );
$brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000 ;
2012-08-08 08:58:36 +00:00
2012-05-01 18:28:45 +00:00
return $brightness > 155 ? $dark : $light ;
}
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'woocommerce_format_hex' ) ) {
2012-08-14 16:06:10 +00:00
/**
* Format string as hex
*
* @ access public
* @ param string $hex
* @ return string
*/
2012-05-01 18:28:45 +00:00
function woocommerce_format_hex ( $hex ) {
2012-08-08 08:58:36 +00:00
2012-05-01 18:28:45 +00:00
$hex = trim ( str_replace ( '#' , '' , $hex ) );
2012-08-08 08:58:36 +00:00
2012-05-01 18:28:45 +00:00
if ( strlen ( $hex ) == 3 ) {
$hex = $hex [ 0 ] . $hex [ 0 ] . $hex [ 1 ] . $hex [ 1 ] . $hex [ 2 ] . $hex [ 2 ];
}
2012-08-08 08:58:36 +00:00
2012-05-01 18:28:45 +00:00
if ( $hex ) return '#' . $hex ;
2011-12-10 17:28:32 +00:00
}
}
2012-08-14 16:06:10 +00:00
2011-12-09 19:55:09 +00:00
/**
* Exclude order comments from queries and RSS
*
* This code should exclude shop_order comments from queries . Some queries ( like the recent comments widget on the dashboard ) are hardcoded
* and are not filtered , however , the code current_user_can ( 'read_post' , $comment -> comment_post_ID ) should keep them safe since only admin and
* shop managers can view orders anyway .
*
* The frontend view order pages get around this filter by using remove_filter ( 'comments_clauses' , 'woocommerce_exclude_order_comments' );
2012-08-14 16:06:10 +00:00
*
* @ access public
* @ param array $clauses
* @ return array
*/
2011-12-09 19:55:09 +00:00
function woocommerce_exclude_order_comments ( $clauses ) {
2012-07-11 14:53:40 +00:00
global $wpdb , $typenow , $pagenow ;
2012-08-08 08:58:36 +00:00
if ( is_admin () && ( $typenow == 'shop_order' || $pagenow == 'edit-comments.php' ) && current_user_can ( 'manage_woocommerce' ) )
2012-07-09 16:50:21 +00:00
return $clauses ; // Don't hide when viewing orders in admin
2012-08-08 08:58:36 +00:00
2012-07-09 16:50:21 +00:00
if ( ! $clauses [ 'join' ] )
$clauses [ 'join' ] = '' ;
2012-08-08 08:58:36 +00:00
2012-07-10 12:51:27 +00:00
if ( ! strstr ( $clauses [ 'join' ], " JOIN $wpdb->posts " ) )
$clauses [ 'join' ] .= " LEFT JOIN $wpdb->posts ON $wpdb->comments .comment_post_ID = $wpdb->posts .ID " ;
2012-08-08 08:58:36 +00:00
if ( $clauses [ 'where' ] )
2012-07-09 16:50:21 +00:00
$clauses [ 'where' ] .= ' AND ' ;
2012-08-08 08:58:36 +00:00
2012-07-10 12:51:27 +00:00
$clauses [ 'where' ] .= " $wpdb->posts .post_type NOT IN ('shop_order') " ;
2012-08-08 08:58:36 +00:00
return $clauses ;
2011-12-09 19:55:09 +00:00
}
2012-08-14 16:06:10 +00:00
add_filter ( 'comments_clauses' , 'woocommerce_exclude_order_comments' , 10 , 1 );
/**
* Exclude order comments from queries and RSS
*
* @ access public
* @ param string $join
* @ return string
*/
2011-12-21 22:46:29 +00:00
function woocommerce_exclude_order_comments_from_feed_join ( $join ) {
2011-12-09 19:55:09 +00:00
global $wpdb ;
2012-08-08 08:58:36 +00:00
if ( ! $join )
2012-07-10 12:51:27 +00:00
$join = " LEFT JOIN $wpdb->posts ON $wpdb->comments .comment_post_ID = $wpdb->posts .ID " ;
2011-12-21 22:46:29 +00:00
return $join ;
}
2012-08-14 16:06:10 +00:00
add_action ( 'comment_feed_join' , 'woocommerce_exclude_order_comments_from_feed_join' );
/**
* Exclude order comments from queries and RSS
*
* @ access public
* @ param string $where
* @ return string
*/
2011-12-21 22:46:29 +00:00
function woocommerce_exclude_order_comments_from_feed_where ( $where ) {
global $wpdb ;
2012-08-08 08:58:36 +00:00
if ( $where )
2012-07-10 12:51:27 +00:00
$where .= ' AND ' ;
2012-08-08 08:58:36 +00:00
2012-07-10 12:51:27 +00:00
$where .= " $wpdb->posts .post_type NOT IN ('shop_order') " ;
2012-08-08 08:58:36 +00:00
2011-12-09 19:55:09 +00:00
return $where ;
}
2012-08-14 16:06:10 +00:00
add_action ( 'comment_feed_where' , 'woocommerce_exclude_order_comments_from_feed_where' );
2011-12-09 17:01:56 +00:00
2012-02-24 20:04:05 +00:00
2012-08-14 16:06:10 +00:00
/**
* Order Status completed - GIVE DOWNLOADABLE PRODUCT ACCESS TO CUSTOMER
*
* @ access public
* @ param int $order_id
* @ return void
*/
2011-12-09 17:01:56 +00:00
function woocommerce_downloadable_product_permissions ( $order_id ) {
global $wpdb ;
2012-08-08 08:58:36 +00:00
2012-10-16 09:45:33 +00:00
if ( get_post_meta ( $order_id , __ ( 'Download Permissions Granted' , 'woocommerce' ), true ) == 1 ) return ; // Only do this once
2012-08-08 08:58:36 +00:00
2012-01-27 16:38:39 +00:00
$order = new WC_Order ( $order_id );
2012-08-08 08:58:36 +00:00
2012-01-13 00:46:56 +00:00
if ( sizeof ( $order -> get_items ()) > 0 ) foreach ( $order -> get_items () as $item ) :
2012-08-08 08:58:36 +00:00
2012-10-23 16:41:42 +00:00
if ( $item [ 'product_id' ] > 0 ) :
2011-12-09 17:01:56 +00:00
$_product = $order -> get_product_from_item ( $item );
2012-08-08 08:58:36 +00:00
2012-04-20 10:17:40 +00:00
if ( $_product -> exists () && $_product -> is_downloadable () ) :
2012-08-08 08:58:36 +00:00
2012-10-23 16:41:42 +00:00
$product_id = ( $item [ 'variation_id' ] > 0 ) ? $item [ 'variation_id' ] : $item [ 'product_id' ];
2012-08-28 15:21:54 +00:00
$file_download_paths = apply_filters ( 'woocommerce_file_download_paths' , get_post_meta ( $product_id , '_file_paths' , true ), $product_id , $order_id , $item );
2012-12-05 16:28:59 +00:00
if ( ! empty ( $file_download_paths ) ) {
2012-12-05 16:26:03 +00:00
foreach ( $file_download_paths as $download_id => $file_path ) {
woocommerce_downloadable_file_permission ( $download_id , $product_id , $order );
}
2012-08-28 15:21:54 +00:00
}
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
endif ;
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
endif ;
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
endforeach ;
2012-08-08 08:58:36 +00:00
2012-10-16 09:45:33 +00:00
update_post_meta ( $order_id , __ ( 'Download Permissions Granted' , 'woocommerce' ), 1 );
2011-12-09 17:01:56 +00:00
}
2012-08-14 16:06:10 +00:00
add_action ( 'woocommerce_order_status_completed' , 'woocommerce_downloadable_product_permissions' );
2012-08-28 15:21:54 +00:00
/**
* Grant downloadable product access to the file identified by $download_id
2012-11-27 16:22:47 +00:00
*
2012-08-28 15:21:54 +00:00
* @ access public
* @ param string $download_id file identifier
* @ param int $product_id product identifier
* @ param WC_Order $order the order
*/
function woocommerce_downloadable_file_permission ( $download_id , $product_id , $order ) {
global $wpdb ;
$user_email = $order -> billing_email ;
$limit = trim ( get_post_meta ( $product_id , '_download_limit' , true ) );
$expiry = trim ( get_post_meta ( $product_id , '_download_expiry' , true ) );
$limit = empty ( $limit ) ? '' : ( int ) $limit ;
// Default value is NULL in the table schema
$expiry = empty ( $expiry ) ? null : ( int ) $expiry ;
if ( $expiry ) $expiry = date_i18n ( " Y-m-d " , strtotime ( 'NOW + ' . $expiry . ' DAY' ) );
$data = array (
'download_id' => $download_id ,
'product_id' => $product_id ,
'user_id' => $order -> user_id ,
'user_email' => $user_email ,
'order_id' => $order -> id ,
'order_key' => $order -> order_key ,
'downloads_remaining' => $limit ,
'access_granted' => current_time ( 'mysql' ),
'download_count' => 0
);
$format = array (
'%s' ,
'%s' ,
'%s' ,
'%s' ,
'%s' ,
'%s' ,
'%s' ,
'%s' ,
'%d'
);
if ( ! is_null ( $expiry ) ) {
$data [ 'access_expires' ] = $expiry ;
$format [] = '%s' ;
}
// Downloadable product - give access to the customer
$wpdb -> insert ( $wpdb -> prefix . 'woocommerce_downloadable_product_permissions' ,
$data ,
$format
);
}
2012-08-14 16:06:10 +00:00
if ( get_option ( 'woocommerce_downloads_grant_access_after_payment' ) == 'yes' )
add_action ( 'woocommerce_order_status_processing' , 'woocommerce_downloadable_product_permissions' );
2011-12-19 17:29:24 +00:00
/**
* Order Status completed - This is a paying customer
2012-08-14 16:06:10 +00:00
*
* @ access public
* @ param int $order_id
* @ return void
*/
2011-12-19 17:29:24 +00:00
function woocommerce_paying_customer ( $order_id ) {
2012-08-08 08:58:36 +00:00
2012-01-27 16:38:39 +00:00
$order = new WC_Order ( $order_id );
2012-08-08 08:58:36 +00:00
2012-11-28 20:24:01 +00:00
if ( $order -> user_id > 0 ) {
update_user_meta ( $order -> user_id , 'paying_customer' , 1 );
$old_count = absint ( get_user_meta ( $order -> user_id , '_order_count' , true ) );
update_user_meta ( $order -> user_id , '_order_count' , $old_count + 1 );
}
2011-12-19 17:29:24 +00:00
}
2012-11-28 20:24:01 +00:00
add_action ( 'woocommerce_order_status_completed' , 'woocommerce_paying_customer' );
2012-08-14 16:06:10 +00:00
2011-12-09 17:01:56 +00:00
/**
* Filter to allow product_cat in the permalinks for products .
*
2012-08-14 16:06:10 +00:00
* @ access public
2011-12-09 17:01:56 +00:00
* @ param string $permalink The existing permalink URL .
2012-08-14 16:06:10 +00:00
* @ param object $post
* @ return string
2011-12-09 17:01:56 +00:00
*/
2012-10-10 09:21:04 +00:00
function woocommerce_product_post_type_link ( $permalink , $post ) {
2011-12-09 17:01:56 +00:00
// Abort if post is not a product
2012-08-14 16:06:10 +00:00
if ( $post -> post_type !== 'product' )
return $permalink ;
2012-11-27 16:22:47 +00:00
2011-12-09 17:01:56 +00:00
// Abort early if the placeholder rewrite tag isn't in the generated URL
2012-10-10 09:21:04 +00:00
if ( false === strpos ( $permalink , '%' ) )
2012-08-14 16:06:10 +00:00
return $permalink ;
2012-11-27 16:22:47 +00:00
2011-12-09 17:01:56 +00:00
// Get the custom taxonomy terms in use by this post
$terms = get_the_terms ( $post -> ID , 'product_cat' );
2012-08-14 16:06:10 +00:00
if ( empty ( $terms ) ) {
2011-12-09 17:01:56 +00:00
// If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
2012-10-10 09:21:04 +00:00
$product_cat = _x ( 'uncategorized' , 'slug' , 'woocommerce' );
2012-08-14 16:06:10 +00:00
} else {
2011-12-09 17:01:56 +00:00
// Replace the placeholder rewrite tag with the first term's slug
$first_term = array_shift ( $terms );
2012-10-10 09:21:04 +00:00
$product_cat = $first_term -> slug ;
2012-08-14 16:06:10 +00:00
}
2012-11-27 16:22:47 +00:00
2012-10-10 09:21:04 +00:00
$find = array (
'%year%' ,
'%monthnum%' ,
'%day%' ,
'%hour%' ,
'%minute%' ,
'%second%' ,
'%post_id%' ,
'%category%' ,
'%product_cat%'
);
2012-11-27 16:22:47 +00:00
2012-10-10 09:21:04 +00:00
$replace = array (
date_i18n ( 'Y' , strtotime ( $post -> post_date ) ),
date_i18n ( 'm' , strtotime ( $post -> post_date ) ),
date_i18n ( 'd' , strtotime ( $post -> post_date ) ),
date_i18n ( 'H' , strtotime ( $post -> post_date ) ),
date_i18n ( 'i' , strtotime ( $post -> post_date ) ),
date_i18n ( 's' , strtotime ( $post -> post_date ) ),
$post -> ID ,
$product_cat ,
$product_cat
2012-11-27 16:22:47 +00:00
);
2012-10-10 09:21:04 +00:00
$replace = array_map ( 'sanitize_title' , $replace );
2012-11-27 16:22:47 +00:00
2012-10-10 09:21:04 +00:00
$permalink = str_replace ( $find , $replace , $permalink );
2011-12-09 17:01:56 +00:00
return $permalink ;
}
2012-10-10 09:21:04 +00:00
add_filter ( 'post_type_link' , 'woocommerce_product_post_type_link' , 10 , 2 );
2012-08-14 16:06:10 +00:00
2011-12-09 17:01:56 +00:00
/**
* Add term ordering to get_terms
2012-08-08 08:58:36 +00:00
*
2011-12-09 17:01:56 +00:00
* 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
2012-08-08 08:58:36 +00:00
*
2011-12-09 17:01:56 +00:00
* To disable it , set it ot false ( or 0 )
2012-08-08 08:58:36 +00:00
*
2012-08-14 16:06:10 +00:00
* @ access public
* @ param array $clauses
* @ param array $taxonomies
* @ param array $args
* @ return array
2011-12-09 17:01:56 +00:00
*/
2012-08-14 16:06:10 +00:00
function woocommerce_terms_clauses ( $clauses , $taxonomies , $args ) {
2011-12-09 17:01:56 +00:00
global $wpdb , $woocommerce ;
// No sorting when menu_order is false
if ( isset ( $args [ 'menu_order' ]) && $args [ 'menu_order' ] == false ) return $clauses ;
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
// No sorting when orderby is non default
if ( isset ( $args [ 'orderby' ]) && $args [ 'orderby' ] != 'name' ) return $clauses ;
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
// No sorting in admin when sorting by a column
2012-11-15 15:32:58 +00:00
if ( is_admin () && isset ( $_GET [ 'orderby' ]) ) return $clauses ;
2011-12-09 17:01:56 +00:00
// wordpress should give us the taxonomies asked when calling the get_terms function. Only apply to categories and pa_ attributes
$found = false ;
2012-08-14 16:06:10 +00:00
foreach ( ( array ) $taxonomies as $taxonomy ) :
2012-07-16 17:33:02 +00:00
if ( strstr ( $taxonomy , 'pa_' ) || in_array ( $taxonomy , apply_filters ( 'woocommerce_sortable_taxonomies' , array ( 'product_cat' ) ) ) ) :
2011-12-09 17:01:56 +00:00
$found = true ;
break ;
endif ;
endforeach ;
if ( ! $found ) return $clauses ;
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
// Meta name
2012-04-09 13:58:00 +00:00
if ( ! empty ( $taxonomies [ 0 ] ) && strstr ( $taxonomies [ 0 ], 'pa_' ) ) {
2011-12-09 17:01:56 +00:00
$meta_name = 'order_' . esc_attr ( $taxonomies [ 0 ]);
2012-04-09 13:58:00 +00:00
} else {
2011-12-09 17:01:56 +00:00
$meta_name = 'order' ;
2012-04-09 13:58:00 +00:00
}
2011-12-09 17:01:56 +00:00
// query fields
2012-08-14 16:06:10 +00:00
if ( strpos ( 'COUNT(*)' , $clauses [ 'fields' ]) === false ) $clauses [ 'fields' ] .= ', tm.* ' ;
2011-12-09 17:01:56 +00:00
//query join
$clauses [ 'join' ] .= " LEFT JOIN { $wpdb -> woocommerce_termmeta } AS tm ON (t.term_id = tm.woocommerce_term_id AND tm.meta_key = ' " . $meta_name . " ') " ;
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
// default to ASC
2012-08-14 16:06:10 +00:00
if ( ! isset ( $args [ 'menu_order' ]) || ! in_array ( strtoupper ( $args [ 'menu_order' ]), array ( 'ASC' , 'DESC' )) ) $args [ 'menu_order' ] = 'ASC' ;
2011-12-09 17:01:56 +00:00
$order = " ORDER BY CAST(tm.meta_value AS SIGNED) " . $args [ 'menu_order' ];
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
if ( $clauses [ 'orderby' ] ) :
$clauses [ 'orderby' ] = str_replace ( 'ORDER BY' , $order . ',' , $clauses [ 'orderby' ] );
else :
$clauses [ 'orderby' ] = $order ;
endif ;
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
return $clauses ;
}
2012-08-14 16:06:10 +00:00
add_filter ( 'terms_clauses' , 'woocommerce_terms_clauses' , 10 , 3 );
2012-06-10 17:15:02 +00:00
/**
* woocommerce_get_product_terms function .
*
* Gets product terms in the order they are defined in the backend .
2012-08-08 08:58:36 +00:00
*
2012-06-10 17:15:02 +00:00
* @ access public
* @ param mixed $object_id
* @ param mixed $taxonomy
* @ param mixed $fields ids , names , slugs , all
* @ return array
*/
function woocommerce_get_product_terms ( $object_id , $taxonomy , $fields = 'all' ) {
2012-08-08 08:58:36 +00:00
if ( ! taxonomy_exists ( $taxonomy ) )
2012-07-10 13:05:44 +00:00
return array ();
2012-08-08 08:58:36 +00:00
2012-06-10 17:15:02 +00:00
$terms = array ();
2012-11-06 17:05:22 +00:00
$object_terms = get_the_terms ( $object_id , $taxonomy );
2012-06-10 17:15:02 +00:00
$all_terms = array_flip ( get_terms ( $taxonomy , array ( 'menu_order' => 'ASC' , 'fields' => 'ids' ) ) );
2012-08-08 08:58:36 +00:00
2012-06-10 17:15:02 +00:00
switch ( $fields ) {
case 'names' :
foreach ( $object_terms as $term )
$terms [ $all_terms [ $term -> term_id ] ] = $term -> name ;
break ;
case 'ids' :
foreach ( $object_terms as $term )
$terms [ $all_terms [ $term -> term_id ] ] = $term -> term_id ;
break ;
case 'slugs' :
foreach ( $object_terms as $term )
$terms [ $all_terms [ $term -> term_id ] ] = $term -> slug ;
break ;
case 'all' :
foreach ( $object_terms as $term )
$terms [ $all_terms [ $term -> term_id ] ] = $term ;
break ;
}
2012-08-08 08:58:36 +00:00
2012-06-10 17:15:02 +00:00
ksort ( $terms );
2012-08-08 08:58:36 +00:00
2012-06-10 17:15:02 +00:00
return $terms ;
}
2012-08-14 16:06:10 +00:00
2011-12-09 17:01:56 +00:00
/**
* WooCommerce Dropdown categories
2012-08-08 08:58:36 +00:00
*
2011-12-09 17:01:56 +00:00
* Stuck with this until a fix for http :// core . trac . wordpress . org / ticket / 13258
2012-04-05 16:51:28 +00:00
* We use a custom walker , just like WordPress does
2012-08-14 16:06:10 +00:00
*
* @ access public
* @ param int $show_counts ( default : 1 )
* @ param int $hierarchal ( default : 1 )
* @ param int $show_uncategorized ( default : 1 )
* @ return string
2011-12-09 17:01:56 +00:00
*/
2012-07-16 17:10:04 +00:00
function woocommerce_product_dropdown_categories ( $show_counts = 1 , $hierarchal = 1 , $show_uncategorized = 1 ) {
2012-04-05 16:51:28 +00:00
global $wp_query , $woocommerce ;
2012-08-08 08:58:36 +00:00
2012-04-05 16:51:28 +00:00
include_once ( $woocommerce -> plugin_path () . '/classes/walkers/class-product-cat-dropdown-walker.php' );
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
$r = array ();
2012-04-05 16:51:28 +00:00
$r [ 'pad_counts' ] = 1 ;
$r [ 'hierarchal' ] = $hierarchal ;
$r [ 'hide_empty' ] = 1 ;
2012-12-23 12:35:49 +00:00
$r [ 'show_count' ] = $show_counts ;
2012-04-05 16:51:28 +00:00
$r [ 'selected' ] = ( isset ( $wp_query -> query [ 'product_cat' ] ) ) ? $wp_query -> query [ 'product_cat' ] : '' ;
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
$terms = get_terms ( 'product_cat' , $r );
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
if ( ! $terms ) return ;
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
$output = " <select name='product_cat' id='dropdown_product_cat'> " ;
2012-10-16 09:45:33 +00:00
$output .= '<option value="" ' . selected ( isset ( $_GET [ 'product_cat' ] ) ? $_GET [ 'product_cat' ] : '' , '' , false ) . '>' . __ ( 'Select a category' , 'woocommerce' ) . '</option>' ;
2011-12-09 17:01:56 +00:00
$output .= woocommerce_walk_category_dropdown_tree ( $terms , 0 , $r );
2012-08-08 08:58:36 +00:00
2012-07-16 17:10:04 +00:00
if ( $show_uncategorized )
2012-10-16 09:45:33 +00:00
$output .= '<option value="0" ' . selected ( isset ( $_GET [ 'product_cat' ] ) ? $_GET [ 'product_cat' ] : '' , '0' , false ) . '>' . __ ( 'Uncategorized' , 'woocommerce' ) . '</option>' ;
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
$output .= " </select> " ;
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
echo $output ;
}
2012-08-14 16:06:10 +00:00
2011-12-09 17:01:56 +00:00
/**
* Walk the Product Categories .
2012-08-14 16:06:10 +00:00
*
* @ access public
* @ return void
2011-12-09 17:01:56 +00:00
*/
function woocommerce_walk_category_dropdown_tree () {
$args = func_get_args ();
2012-08-08 08:58:36 +00:00
2011-12-09 17:01:56 +00:00
// the user's options are the third parameter
if ( empty ( $args [ 2 ][ 'walker' ]) || ! is_a ( $args [ 2 ][ 'walker' ], 'Walker' ) )
2012-04-05 16:51:28 +00:00
$walker = new WC_Product_Cat_Dropdown_Walker ;
2011-12-09 17:01:56 +00:00
else
$walker = $args [ 2 ][ 'walker' ];
return call_user_func_array ( array ( & $walker , 'walk' ), $args );
}
2012-08-14 16:06:10 +00:00
2011-12-09 19:55:09 +00:00
/**
2012-10-19 17:59:17 +00:00
* WooCommerce Term / Order item Meta API - set table name
2012-08-08 08:58:36 +00:00
*
2012-08-14 16:06:10 +00:00
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
function woocommerce_taxonomy_metadata_wpdbfix () {
global $wpdb ;
2012-10-19 17:59:17 +00:00
$termmeta_name = 'woocommerce_termmeta' ;
$itemmeta_name = 'woocommerce_order_itemmeta' ;
2012-11-27 16:22:47 +00:00
2012-10-19 17:59:17 +00:00
$wpdb -> woocommerce_termmeta = $wpdb -> prefix . $termmeta_name ;
$wpdb -> order_itemmeta = $wpdb -> prefix . $itemmeta_name ;
2012-11-27 16:22:47 +00:00
2012-10-19 17:59:17 +00:00
$wpdb -> tables [] = 'woocommerce_termmeta' ;
$wpdb -> tables [] = 'order_itemmeta' ;
2012-08-08 08:58:36 +00:00
}
2011-12-09 19:55:09 +00:00
2012-08-14 16:06:10 +00:00
add_action ( 'init' , 'woocommerce_taxonomy_metadata_wpdbfix' , 0 );
add_action ( 'switch_blog' , 'woocommerce_taxonomy_metadata_wpdbfix' , 0 );
/**
* WooCommerce Term Meta API - Update term meta
*
* @ access public
* @ param mixed $term_id
* @ param mixed $meta_key
* @ param mixed $meta_value
* @ param string $prev_value ( default : '' )
* @ return bool
*/
function update_woocommerce_term_meta ( $term_id , $meta_key , $meta_value , $prev_value = '' ) {
return update_metadata ( 'woocommerce_term' , $term_id , $meta_key , $meta_value , $prev_value );
2011-12-09 19:55:09 +00:00
}
2012-08-14 16:06:10 +00:00
/**
* WooCommerce Term Meta API - Add term meta
*
* @ access public
* @ param mixed $term_id
* @ param mixed $meta_key
* @ param mixed $meta_value
* @ param bool $unique ( default : false )
* @ return bool
*/
function add_woocommerce_term_meta ( $term_id , $meta_key , $meta_value , $unique = false ){
return add_metadata ( 'woocommerce_term' , $term_id , $meta_key , $meta_value , $unique );
2011-12-09 19:55:09 +00:00
}
2012-08-14 16:06:10 +00:00
/**
* WooCommerce Term Meta API - Delete term meta
*
* @ access public
* @ param mixed $term_id
* @ param mixed $meta_key
* @ param string $meta_value ( default : '' )
* @ param bool $delete_all ( default : false )
* @ return bool
*/
function delete_woocommerce_term_meta ( $term_id , $meta_key , $meta_value = '' , $delete_all = false ) {
return delete_metadata ( 'woocommerce_term' , $term_id , $meta_key , $meta_value , $delete_all );
2011-12-09 19:55:09 +00:00
}
2012-08-14 16:06:10 +00:00
/**
* WooCommerce Term Meta API - Get term meta
*
* @ access public
* @ param mixed $term_id
* @ param mixed $key
* @ param bool $single ( default : true )
* @ return mixed
*/
function get_woocommerce_term_meta ( $term_id , $key , $single = true ) {
return get_metadata ( 'woocommerce_term' , $term_id , $key , $single );
2011-12-11 13:11:00 +00:00
}
2012-08-14 16:06:10 +00:00
2011-12-11 13:11:00 +00:00
/**
* Move a term before the a given element of its hierarchy level
*
2012-08-14 16:06:10 +00:00
* @ access public
* @ param int $the_term
2011-12-11 13:11:00 +00:00
* @ param int $next_id the id of the next slibling element in save hierachy level
2012-08-14 16:06:10 +00:00
* @ param string $taxonomy
* @ param int $index ( default : 0 )
* @ param mixed $terms ( default : null )
* @ return int
2011-12-11 13:11:00 +00:00
*/
2012-08-14 16:06:10 +00:00
function woocommerce_order_terms ( $the_term , $next_id , $taxonomy , $index = 0 , $terms = null ) {
2012-08-08 08:58:36 +00:00
2012-10-11 09:22:03 +00:00
if ( ! $terms ) $terms = get_terms ( $taxonomy , 'menu_order=ASC&hide_empty=0&parent=0' );
2011-12-11 13:11:00 +00:00
if ( empty ( $terms ) ) return $index ;
2012-08-08 08:58:36 +00:00
2011-12-11 13:11:00 +00:00
$id = $the_term -> term_id ;
2012-08-08 08:58:36 +00:00
2011-12-11 13:11:00 +00:00
$term_in_level = false ; // flag: is our term to order in this level of terms
2012-08-08 08:58:36 +00:00
2011-12-11 13:11:00 +00:00
foreach ( $terms as $term ) {
2012-08-08 08:58:36 +00:00
2011-12-11 13:11:00 +00:00
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
2012-08-08 08:58:36 +00:00
if ( null !== $next_id && $term -> term_id == $next_id ) {
2011-12-11 13:11:00 +00:00
$index ++ ;
$index = woocommerce_set_term_order ( $id , $index , $taxonomy , true );
2012-08-08 08:58:36 +00:00
}
2011-12-11 13:11:00 +00:00
// set order
$index ++ ;
$index = woocommerce_set_term_order ( $term -> term_id , $index , $taxonomy );
2012-08-08 08:58:36 +00:00
2011-12-11 13:11:00 +00:00
// if that term has children we walk through them
$children = get_terms ( $taxonomy , " parent= { $term -> term_id } &menu_order=ASC&hide_empty=0 " );
if ( ! empty ( $children ) ) {
2012-08-08 08:58:36 +00:00
$index = woocommerce_order_terms ( $the_term , $next_id , $taxonomy , $index , $children );
2011-12-11 13:11:00 +00:00
}
}
// no nextid meaning our term is in last position
if ( $term_in_level && null === $next_id )
$index = woocommerce_set_term_order ( $id , $index + 1 , $taxonomy , true );
2012-04-24 17:22:18 +00:00
2011-12-11 13:11:00 +00:00
return $index ;
}
2012-08-14 16:06:10 +00:00
2011-12-11 13:11:00 +00:00
/**
* Set the sort order of a term
2012-08-08 08:58:36 +00:00
*
2012-08-14 16:06:10 +00:00
* @ access public
2011-12-11 13:11:00 +00:00
* @ param int $term_id
* @ param int $index
2012-08-14 16:06:10 +00:00
* @ param string $taxonomy
* @ param bool $recursive ( default : false )
* @ return int
2011-12-11 13:11:00 +00:00
*/
2012-08-14 16:06:10 +00:00
function woocommerce_set_term_order ( $term_id , $index , $taxonomy , $recursive = false ) {
2011-12-11 13:11:00 +00:00
global $wpdb ;
2012-08-08 08:58:36 +00:00
2011-12-11 13:11:00 +00:00
$term_id = ( int ) $term_id ;
$index = ( int ) $index ;
2012-08-08 08:58:36 +00:00
2011-12-11 13:11:00 +00:00
// Meta name
if ( strstr ( $taxonomy , 'pa_' )) :
$meta_name = 'order_' . esc_attr ( $taxonomy );
else :
$meta_name = 'order' ;
endif ;
2012-08-08 08:58:36 +00:00
2011-12-11 13:11:00 +00:00
update_woocommerce_term_meta ( $term_id , $meta_name , $index );
2012-08-08 08:58:36 +00:00
2011-12-11 13:11:00 +00:00
if ( ! $recursive ) return $index ;
2012-08-08 08:58:36 +00:00
2011-12-11 13:11:00 +00:00
$children = get_terms ( $taxonomy , " parent= $term_id &menu_order=ASC&hide_empty=0 " );
foreach ( $children as $term ) {
$index ++ ;
2012-08-08 08:58:36 +00:00
$index = woocommerce_set_term_order ( $term -> term_id , $index , $taxonomy , true );
2011-12-11 13:11:00 +00:00
}
2012-11-27 16:22:47 +00:00
2012-10-11 09:22:03 +00:00
clean_term_cache ( $term_id , $taxonomy );
2012-08-08 08:58:36 +00:00
2011-12-11 13:11:00 +00:00
return $index ;
2012-03-23 16:28:27 +00:00
}
/**
* let_to_num function .
2012-08-08 08:58:36 +00:00
*
2012-08-14 16:06:10 +00:00
* This function transforms the php . ini notation for numbers ( like '2M' ) to an integer .
2012-03-23 16:28:27 +00:00
*
* @ access public
* @ param $size
* @ return int
*/
function woocommerce_let_to_num ( $size ) {
$l = substr ( $size , - 1 );
$ret = substr ( $size , 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 ;
}
return $ret ;
2012-04-10 21:37:29 +00:00
}
2012-07-11 17:48:22 +00:00
2012-08-14 16:06:10 +00:00
2012-07-11 17:48:22 +00:00
/**
* woocommerce_customer_bought_product
*
* Checks if a user ( by email ) has bought an item
2012-08-14 16:06:10 +00:00
*
* @ access public
* @ param string $customer_email
* @ param int $user_id
* @ param int $product_id
* @ return bool
*/
2012-07-11 17:48:22 +00:00
function woocommerce_customer_bought_product ( $customer_email , $user_id , $product_id ) {
global $wpdb ;
2012-08-08 08:58:36 +00:00
2012-07-11 17:48:22 +00:00
$emails = array ();
2012-08-08 08:58:36 +00:00
2012-07-11 17:48:22 +00:00
if ( $user_id ) {
$user = get_user_by ( 'id' , $user_id );
$emails [] = $user -> user_email ;
}
2012-08-08 08:58:36 +00:00
if ( is_email ( $customer_email ) )
2012-07-11 17:48:22 +00:00
$emails [] = $customer_email ;
2012-08-08 08:58:36 +00:00
2012-07-11 17:48:22 +00:00
if ( sizeof ( $emails ) == 0 )
return false ;
2012-08-08 08:58:36 +00:00
2012-10-20 14:31:19 +00:00
return $wpdb -> get_var ( $wpdb -> prepare ( "
2012-11-27 16:22:47 +00:00
SELECT COUNT ( order_items . order_item_id )
2012-10-20 14:31:19 +00:00
FROM { $wpdb -> prefix } woocommerce_order_items as order_items
2012-10-23 16:41:42 +00:00
LEFT JOIN { $wpdb -> prefix } woocommerce_order_itemmeta AS itemmeta ON order_items . order_item_id = itemmeta . order_item_id
2012-10-20 14:31:19 +00:00
LEFT JOIN { $wpdb -> postmeta } AS postmeta ON order_items . order_id = postmeta . post_id
LEFT JOIN { $wpdb -> term_relationships } AS rel ON postmeta . post_id = rel . object_ID
LEFT JOIN { $wpdb -> term_taxonomy } AS tax USING ( term_taxonomy_id )
LEFT JOIN { $wpdb -> terms } AS term USING ( term_id )
WHERE term . slug IN ( '" . implode( "' , '", apply_filters( ' woocommerce_reports_order_statuses ', array( ' completed ', ' processing ', ' on - hold ' ) ) ) . "' )
AND tax . taxonomy = 'shop_order_status'
AND (
2012-10-23 16:41:42 +00:00
(
2012-11-27 16:22:47 +00:00
itemmeta . meta_key = '_variation_id'
2012-10-23 16:41:42 +00:00
AND itemmeta . meta_value = % s
2012-11-27 16:22:47 +00:00
) OR (
itemmeta . meta_key = '_product_id'
2012-10-23 16:41:42 +00:00
AND itemmeta . meta_value = % s
)
2012-10-20 14:31:19 +00:00
)
2012-11-27 16:22:47 +00:00
AND (
2012-10-20 14:31:19 +00:00
(
2012-11-27 16:22:47 +00:00
postmeta . meta_key = '_billing_email'
AND postmeta . meta_value IN ( '" . implode( "' , '", array_unique( $emails ) ) . "' )
) OR (
postmeta . meta_key = '_customer_user'
AND postmeta . meta_value = % s AND postmeta . meta_value > 0
2012-10-20 14:31:19 +00:00
)
2012-11-27 16:22:47 +00:00
)
2012-10-20 14:31:19 +00:00
" , $product_id , $product_id , $user_id ) );
2012-07-11 17:48:22 +00:00
}
2012-07-23 12:56:05 +00:00
2012-08-14 16:06:10 +00:00
/**
* Return the count of processing orders .
*
* @ access public
* @ return int
*/
2012-07-23 12:56:05 +00:00
function woocommerce_processing_order_count () {
if ( false === ( $order_count = get_transient ( 'woocommerce_processing_order_count' ) ) ) {
$order_statuses = get_terms ( 'shop_order_status' );
$order_count = false ;
foreach ( $order_statuses as $status ) {
if ( $status -> slug === 'processing' ) {
$order_count += $status -> count ;
break ;
}
}
$order_count = apply_filters ( 'woocommerce_admin_menu_count' , intval ( $order_count ) );
set_transient ( 'woocommerce_processing_order_count' , $order_count );
}
2012-08-08 08:58:36 +00:00
2012-07-23 12:56:05 +00:00
return $order_count ;
2012-09-20 15:35:15 +00:00
}
/**
* Get capabilities for WooCommerce - these are assigned to admin / shop manager during installation or reset
2012-11-27 16:22:47 +00:00
*
2012-09-20 15:35:15 +00:00
* @ access public
* @ return void
*/
function woocommerce_get_core_capabilities () {
$capabilities = array ();
2012-11-27 16:22:47 +00:00
2012-09-20 15:35:15 +00:00
$capabilities [ 'core' ] = array (
2012-11-27 16:22:47 +00:00
" manage_woocommerce " ,
2012-09-20 15:35:15 +00:00
" view_woocommerce_reports "
);
2012-11-27 16:22:47 +00:00
2012-09-20 15:35:15 +00:00
$capability_types = array ( 'product' , 'shop_order' , 'shop_coupon' );
2012-11-27 16:22:47 +00:00
2012-09-20 15:35:15 +00:00
foreach ( $capability_types as $capability_type ) {
2012-11-27 16:22:47 +00:00
2012-09-20 15:35:15 +00:00
$capabilities [ $capability_type ] = array (
2012-11-27 16:22:47 +00:00
2012-09-20 15:35:15 +00:00
// Post type
" edit_ { $capability_type } " ,
" read_ { $capability_type } " ,
" delete_ { $capability_type } " ,
" edit_ { $capability_type } s " ,
" edit_others_ { $capability_type } s " ,
" publish_ { $capability_type } s " ,
" read_private_ { $capability_type } s " ,
" delete_ { $capability_type } s " ,
" delete_private_ { $capability_type } s " ,
" delete_published_ { $capability_type } s " ,
" delete_others_ { $capability_type } s " ,
" edit_private_ { $capability_type } s " ,
" edit_published_ { $capability_type } s " ,
2012-11-27 16:22:47 +00:00
2012-09-20 15:35:15 +00:00
// Terms
" manage_ { $capability_type } _terms " ,
" edit_ { $capability_type } _terms " ,
" delete_ { $capability_type } _terms " ,
" assign_ { $capability_type } _terms "
);
}
2012-11-27 16:22:47 +00:00
2012-09-20 15:35:15 +00:00
return $capabilities ;
}
/**
* woocommerce_init_roles function .
2012-11-27 16:22:47 +00:00
*
2012-09-20 15:35:15 +00:00
* @ access public
* @ return void
*/
function woocommerce_init_roles () {
global $wp_roles ;
2012-11-27 16:22:47 +00:00
if ( class_exists ( 'WP_Roles' ) )
if ( ! isset ( $wp_roles ) )
2012-09-20 15:35:15 +00:00
$wp_roles = new WP_Roles ();
if ( is_object ( $wp_roles ) ) {
// Customer role
2012-10-16 09:45:33 +00:00
add_role ( 'customer' , __ ( 'Customer' , 'woocommerce' ), array (
2012-09-20 15:35:15 +00:00
'read' => true ,
'edit_posts' => false ,
'delete_posts' => false
) );
// Shop manager role
2012-10-16 09:45:33 +00:00
add_role ( 'shop_manager' , __ ( 'Shop Manager' , 'woocommerce' ), array (
2012-09-20 15:35:15 +00:00
'read' => true ,
'read_private_pages' => true ,
'read_private_posts' => true ,
'edit_users' => true ,
'edit_posts' => true ,
'edit_pages' => true ,
'edit_published_posts' => true ,
'edit_published_pages' => true ,
'edit_private_pages' => true ,
'edit_private_posts' => true ,
'edit_others_posts' => true ,
'edit_others_pages' => true ,
'publish_posts' => true ,
'publish_pages' => true ,
'delete_posts' => true ,
'delete_pages' => true ,
'delete_private_pages' => true ,
'delete_private_posts' => true ,
'delete_published_pages' => true ,
'delete_published_posts' => true ,
'delete_others_posts' => true ,
'delete_others_pages' => true ,
'manage_categories' => true ,
'manage_links' => true ,
'moderate_comments' => true ,
'unfiltered_html' => true ,
'upload_files' => true ,
'export' => true ,
'import' => true
) );
2012-11-27 16:22:47 +00:00
2012-09-20 15:35:15 +00:00
$capabilities = woocommerce_get_core_capabilities ();
2012-11-27 16:22:47 +00:00
2012-09-20 15:35:15 +00:00
foreach ( $capabilities as $cap_group ) {
foreach ( $cap_group as $cap ) {
$wp_roles -> add_cap ( 'shop_manager' , $cap );
$wp_roles -> add_cap ( 'administrator' , $cap );
}
}
}
}
/**
* woocommerce_remove_roles function .
2012-11-27 16:22:47 +00:00
*
2012-09-20 15:35:15 +00:00
* @ access public
* @ return void
*/
function woocommerce_remove_roles () {
global $wp_roles ;
2012-11-27 16:22:47 +00:00
if ( class_exists ( 'WP_Roles' ) )
if ( ! isset ( $wp_roles ) )
2012-09-20 15:35:15 +00:00
$wp_roles = new WP_Roles ();
if ( is_object ( $wp_roles ) ) {
$capabilities = woocommerce_get_core_capabilities ();
2012-11-27 16:22:47 +00:00
2012-09-20 15:35:15 +00:00
foreach ( $capabilities as $cap_group ) {
foreach ( $cap_group as $cap ) {
$wp_roles -> remove_cap ( 'shop_manager' , $cap );
$wp_roles -> remove_cap ( 'administrator' , $cap );
}
}
2012-11-27 16:22:47 +00:00
2012-09-20 15:35:15 +00:00
remove_role ( 'customer' );
remove_role ( 'shop_manager' );
}
2012-10-09 13:16:32 +00:00
}
2012-10-19 17:59:17 +00:00
/**
2012-10-23 16:41:42 +00:00
* Add a item to an order ( for example a line item ) .
2012-11-27 16:22:47 +00:00
*
2012-10-19 17:59:17 +00:00
* @ access public
* @ param int $order_id
* @ param array $data
* @ return mixed
*/
function woocommerce_add_order_item ( $order_id , $item ) {
global $wpdb ;
2012-11-27 16:22:47 +00:00
2012-10-19 17:59:17 +00:00
$order_id = absint ( $order_id );
2012-11-27 16:22:47 +00:00
2012-10-19 17:59:17 +00:00
if ( ! $order_id )
return false ;
$defaults = array (
'order_item_name' => '' ,
2012-10-23 16:41:42 +00:00
'order_item_type' => 'line_item' ,
2012-10-19 17:59:17 +00:00
);
$item = wp_parse_args ( $item , $defaults );
2012-11-27 16:22:47 +00:00
$wpdb -> insert (
2012-10-19 17:59:17 +00:00
$wpdb -> prefix . " woocommerce_order_items " ,
2012-11-27 16:22:47 +00:00
array (
2012-10-19 17:59:17 +00:00
'order_item_name' => $item [ 'order_item_name' ],
2012-10-23 16:41:42 +00:00
'order_item_type' => $item [ 'order_item_type' ],
'order_id' => $order_id
2012-11-27 16:22:47 +00:00
),
2012-10-19 17:59:17 +00:00
array (
2012-10-23 16:41:42 +00:00
'%s' , '%s' , '%d'
2012-10-19 17:59:17 +00:00
)
);
2012-11-27 16:22:47 +00:00
2012-10-19 17:59:17 +00:00
do_action ( 'woocommerce_new_order_item' , absint ( $wpdb -> insert_id ) );
2012-11-27 16:22:47 +00:00
2012-10-19 17:59:17 +00:00
return absint ( $wpdb -> insert_id );
}
/**
* woocommerce_delete_order_item function .
2012-11-27 16:22:47 +00:00
*
2012-10-19 17:59:17 +00:00
* @ access public
* @ param int $item_id
* @ return bool
*/
function woocommerce_delete_order_item ( $item_id ) {
global $wpdb ;
2012-11-27 16:22:47 +00:00
2012-10-19 17:59:17 +00:00
$item_id = absint ( $item_id );
2012-11-27 16:22:47 +00:00
2012-10-19 17:59:17 +00:00
if ( ! $item_id )
return false ;
2012-11-27 16:22:47 +00:00
2012-10-19 17:59:17 +00:00
$wpdb -> query ( $wpdb -> prepare ( " DELETE FROM { $wpdb -> prefix } woocommerce_order_items WHERE order_item_id = %d " , $item_id ) );
$wpdb -> query ( $wpdb -> prepare ( " DELETE FROM { $wpdb -> prefix } woocommerce_order_itemmeta WHERE order_item_id = %d " , $item_id ) );
2012-11-27 16:22:47 +00:00
2012-10-19 17:59:17 +00:00
do_action ( 'woocommerce_delete_order_item' , $item_id );
2012-11-27 16:22:47 +00:00
return true ;
2012-10-19 17:59:17 +00:00
}
/**
* WooCommerce Order Item Meta API - Update term meta
*
* @ access public
* @ param mixed $item_id
* @ param mixed $meta_key
* @ param mixed $meta_value
* @ param string $prev_value ( default : '' )
* @ return bool
*/
function woocommerce_update_order_item_meta ( $item_id , $meta_key , $meta_value , $prev_value = '' ) {
return update_metadata ( 'order_item' , $item_id , $meta_key , $meta_value , $prev_value );
}
/**
* WooCommerce Order Item Meta API - Add term meta
*
* @ access public
* @ param mixed $item_id
* @ param mixed $meta_key
* @ param mixed $meta_value
* @ param bool $unique ( default : false )
* @ return bool
*/
function woocommerce_add_order_item_meta ( $item_id , $meta_key , $meta_value , $unique = false ){
return add_metadata ( 'order_item' , $item_id , $meta_key , $meta_value , $unique );
}
/**
* WooCommerce Order Item Meta API - Delete term meta
*
* @ access public
* @ param mixed $item_id
* @ param mixed $meta_key
* @ param string $meta_value ( default : '' )
* @ param bool $delete_all ( default : false )
* @ return bool
*/
function woocommerce_delete_order_item_meta ( $item_id , $meta_key , $meta_value = '' , $delete_all = false ) {
return delete_metadata ( 'order_item' , $item_id , $meta_key , $meta_value , $delete_all );
}
/**
* WooCommerce Order Item Meta API - Get term meta
*
* @ access public
* @ param mixed $item_id
* @ param mixed $key
* @ param bool $single ( default : true )
* @ return mixed
*/
function woocommerce_get_order_item_meta ( $item_id , $key , $single = true ) {
return get_metadata ( 'order_item' , $item_id , $key , $single );
}
2012-11-21 12:28:16 +00:00
/**
* WooCommerce Date Format - Allows to change date format for everything WooCommerce
*
* @ access public
* @ return string
*/
function woocommerce_date_format () {
return apply_filters ( 'woocommerce_date_format' , get_option ( 'date_format' ) );
}
2012-11-28 15:03:22 +00:00
/**
* Function for recounting product terms , ignoring hidden products .
*
* @ access public
* @ param mixed $term
* @ param mixed $taxonomy
* @ return void
*/
2012-11-28 15:40:08 +00:00
function _woocommerce_term_recount ( $terms , $taxonomy , $callback = true , $terms_are_term_taxonomy_ids = true ) {
2012-11-28 15:03:22 +00:00
global $wpdb ;
2012-11-29 12:50:02 +00:00
// Standard callback
if ( $callback )
_update_post_term_count ( $terms , $taxonomy );
2012-11-28 15:03:22 +00:00
// Stock query
if ( get_option ( 'woocommerce_hide_out_of_stock_items' ) == 'yes' ) {
$stock_join = " LEFT JOIN { $wpdb -> postmeta } AS meta_stock ON posts.ID = meta_stock.post_id " ;
$stock_query = "
AND (
meta_stock . meta_key = '_stock_status'
AND
meta_stock . meta_value = 'instock'
) " ;
} else {
$stock_query = $stock_join = '' ;
}
// Main query
$count_query = $wpdb -> prepare ( "
SELECT COUNT ( DISTINCT posts . ID ) FROM { $wpdb -> posts } as posts
LEFT JOIN { $wpdb -> postmeta } AS meta_visibilty ON posts . ID = meta_visibilty . post_id
LEFT JOIN { $wpdb -> term_relationships } AS rel ON posts . ID = rel . object_ID
LEFT JOIN { $wpdb -> term_taxonomy } AS tax USING ( term_taxonomy_id )
LEFT JOIN { $wpdb -> terms } AS term USING ( term_id )
$stock_join
WHERE posts . post_status = 'publish'
AND posts . post_type = 'product'
AND (
meta_visibilty . meta_key = '_visibility'
AND
meta_visibilty . meta_value IN ( 'visible' , 'catalog' )
)
AND tax . taxonomy = % s
$stock_query
" , $taxonomy->name );
// Store terms + counts here
$term_counts = array ();
$counted_terms = array ();
$maybe_count_parents = array ();
2012-11-28 15:23:13 +00:00
// Pre-process term taxonomy ids
if ( $terms_are_term_taxonomy_ids ) {
$term_ids = array ();
foreach ( ( array ) $terms as $term ) {
$the_term = $wpdb -> get_row ( " SELECT term_id, parent FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = $term AND taxonomy = ' $taxonomy->name ' " );
$term_ids [ $the_term -> term_id ] = $the_term -> parent ;
}
$terms = $term_ids ;
}
2012-11-28 15:03:22 +00:00
// Count those terms!
2012-11-28 15:23:13 +00:00
foreach ( ( array ) $terms as $term_id => $parent_id ) {
2012-11-28 15:03:22 +00:00
$term_ids = array ();
if ( is_taxonomy_hierarchical ( $taxonomy -> name ) ) {
// Grab the parents to count later
2012-11-28 15:23:13 +00:00
$parent = $parent_id ;
2012-11-28 15:03:22 +00:00
2012-11-28 15:23:13 +00:00
while ( ! empty ( $parent ) && $parent > 0 ) {
2012-11-28 15:03:22 +00:00
$maybe_count_parents [] = $parent ;
$parent_term = get_term_by ( 'id' , $parent , $taxonomy -> name );
2012-11-28 15:23:13 +00:00
if ( $parent_term )
$parent = $parent_term -> parent ;
else
$parent = 0 ;
2012-11-28 15:03:22 +00:00
}
// We need to get the $term's hierarchy so we can count its children too
2012-11-28 15:23:13 +00:00
$term_ids = get_term_children ( $term_id , $taxonomy -> name );
2012-11-28 15:03:22 +00:00
}
2012-11-28 15:23:13 +00:00
$term_ids [] = absint ( $term_id );
2012-11-28 15:03:22 +00:00
// Generate term query
$term_query = 'AND term.term_id IN ( ' . implode ( ',' , $term_ids ) . ' )' ;
// Get the count
$count = $wpdb -> get_var ( $count_query . $term_query );
2012-11-28 15:23:13 +00:00
update_woocommerce_term_meta ( $term_id , 'product_count_' . $taxonomy -> name , absint ( $count ) );
2012-11-28 15:03:22 +00:00
2012-11-28 15:23:13 +00:00
$counted_terms [] = $term_id ;
2012-11-28 15:03:22 +00:00
}
// Re-count parents
if ( is_taxonomy_hierarchical ( $taxonomy -> name ) ) {
$terms = array_diff ( $maybe_count_parents , $counted_terms );
foreach ( ( array ) $terms as $term ) {
$term_ids = get_term_children ( $term , $taxonomy -> name );
$term_ids [] = $term ;
// Generate term query
$term_query = 'AND term.term_id IN ( ' . implode ( ',' , $term_ids ) . ' )' ;
// Get the count
$count = $wpdb -> get_var ( $count_query . $term_query );
update_woocommerce_term_meta ( $term , 'product_count_' . $taxonomy -> name , absint ( $count ) );
}
}
}
2012-11-28 15:40:08 +00:00
/**
* woocommerce_recount_after_stock_change function .
*
* @ access public
* @ return void
*/
function woocommerce_recount_after_stock_change ( $product_id ) {
$product_terms = get_the_terms ( $product_id , 'product_cat' );
2012-12-17 20:30:54 +00:00
if ( $product_terms ) {
foreach ( $product_terms as $term )
$product_cats [ $term -> term_id ] = $term -> parent ;
_woocommerce_term_recount ( $product_cats , get_taxonomy ( 'product_cat' ), false , false );
}
2012-11-28 15:40:08 +00:00
$product_terms = get_the_terms ( $product_id , 'product_tag' );
2012-12-17 20:30:54 +00:00
if ( $product_terms ) {
foreach ( $product_terms as $term )
$product_tags [ $term -> term_id ] = $term -> parent ;
_woocommerce_term_recount ( $product_tags , get_taxonomy ( 'product_tag' ), false , false );
}
2012-11-28 15:40:08 +00:00
}
add_action ( 'woocommerce_product_set_stock_status' , 'woocommerce_recount_after_stock_change' );
2012-11-28 15:03:22 +00:00
/**
* woocommerce_change_term_counts function .
2012-12-27 09:29:53 +00:00
* Overrides the original term count for product categories and tags with the product count
* that takes catalog visibility into account .
2012-11-28 15:03:22 +00:00
*
* @ access public
* @ param mixed $terms
* @ param mixed $taxonomies
* @ param mixed $args
* @ return void
*/
function woocommerce_change_term_counts ( $terms , $taxonomies , $args ) {
if ( ! in_array ( $taxonomies [ 0 ], apply_filters ( 'woocommerce_change_term_counts' , array ( 'product_cat' , 'product_tag' ) ) ) )
return $terms ;
foreach ( $terms as & $term ) {
2012-12-27 09:29:53 +00:00
// If the original term count is zero, there's no way the product count could be higher.
2012-12-27 14:18:10 +00:00
if ( empty ( $term -> count ) ) continue ;
2012-12-27 09:29:53 +00:00
2012-11-28 15:03:22 +00:00
$count = get_woocommerce_term_meta ( $term -> term_id , 'product_count_' . $taxonomies [ 0 ] , true );
if ( $count != '' )
$term -> count = $count ;
}
return $terms ;
}
if ( ! is_admin () && ! is_ajax () )
2012-12-12 12:52:35 +00:00
add_filter ( 'get_terms' , 'woocommerce_change_term_counts' , 10 , 3 );
/**
* Function which handles the start and end of scheduled sales via cron .
*
* @ access public
* @ return void
*/
function woocommerce_scheduled_sales () {
2012-12-28 18:49:08 +00:00
global $woocommerce , $wpdb ;
2012-12-12 12:52:35 +00:00
// Sales which are due to start
$product_ids = $wpdb -> get_col ( $wpdb -> prepare ( "
SELECT postmeta . post_id FROM { $wpdb -> postmeta } as postmeta
LEFT JOIN { $wpdb -> postmeta } as postmeta_2 ON postmeta . post_id = postmeta_2 . post_id
LEFT JOIN { $wpdb -> postmeta } as postmeta_3 ON postmeta . post_id = postmeta_3 . post_id
WHERE postmeta . meta_key = '_sale_price_dates_from'
AND postmeta_2 . meta_key = '_price'
AND postmeta_3 . meta_key = '_sale_price'
AND postmeta . meta_value > 0
AND postmeta . meta_value < % s
AND postmeta_2 . meta_value != postmeta_3 . meta_value
" , current_time( 'timestamp' ) ) );
if ( $product_ids ) {
foreach ( $product_ids as $product_id ) {
$sale_price = get_post_meta ( $product_id , '_sale_price' , true );
if ( $sale_price ) {
update_post_meta ( $product_id , '_price' , $sale_price );
} else {
// No sale price!
update_post_meta ( $product_id , '_sale_price_dates_from' , '' );
update_post_meta ( $product_id , '_sale_price_dates_to' , '' );
}
2012-12-28 18:49:08 +00:00
$woocommerce -> clear_product_transients ( $product_id );
2012-12-12 12:52:35 +00:00
$parent = wp_get_post_parent_id ( $product_id );
// Sync parent
if ( $parent ) {
// We can force varaible product price to sync up by removing their min price meta
delete_post_meta ( $parent , 'min_variation_price' );
// Grouped products need syncing via a function
$this_product = get_product ( $product_id );
if ( $this_product -> is_type ( 'simple' ) )
$this_product -> grouped_product_sync ();
2012-12-28 18:49:08 +00:00
$woocommerce -> clear_product_transients ( $parent );
2012-12-12 12:52:35 +00:00
}
}
}
// Sales which are due to end
$product_ids = $wpdb -> get_col ( $wpdb -> prepare ( "
SELECT postmeta . post_id FROM { $wpdb -> postmeta } as postmeta
LEFT JOIN { $wpdb -> postmeta } as postmeta_2 ON postmeta . post_id = postmeta_2 . post_id
LEFT JOIN { $wpdb -> postmeta } as postmeta_3 ON postmeta . post_id = postmeta_3 . post_id
WHERE postmeta . meta_key = '_sale_price_dates_to'
AND postmeta_2 . meta_key = '_price'
AND postmeta_3 . meta_key = '_regular_price'
AND postmeta . meta_value > 0
AND postmeta . meta_value < % s
AND postmeta_2 . meta_value != postmeta_3 . meta_value
" , current_time( 'timestamp' ) ) );
if ( $product_ids ) {
foreach ( $product_ids as $product_id ) {
$regular_price = get_post_meta ( $product_id , '_regular_price' , true );
update_post_meta ( $product_id , '_price' , $regular_price );
update_post_meta ( $product_id , '_sale_price' , '' );
update_post_meta ( $product_id , '_sale_price_dates_from' , '' );
update_post_meta ( $product_id , '_sale_price_dates_to' , '' );
2012-12-28 18:49:08 +00:00
$woocommerce -> clear_product_transients ( $product_id );
2012-12-12 12:52:35 +00:00
$parent = wp_get_post_parent_id ( $product_id );
// Sync parent
if ( $parent ) {
// We can force varaible product price to sync up by removing their min price meta
delete_post_meta ( $parent , 'min_variation_price' );
// Grouped products need syncing via a function
$this_product = get_product ( $product_id );
if ( $this_product -> is_type ( 'simple' ) )
$this_product -> grouped_product_sync ();
2012-12-28 18:49:08 +00:00
$woocommerce -> clear_product_transients ( $parent );
2012-12-12 12:52:35 +00:00
}
}
}
}
2012-12-12 21:14:19 +00:00
add_action ( 'woocommerce_scheduled_sales' , 'woocommerce_scheduled_sales' );
/**
* woocommerce_cancel_unpaid_orders function .
*
* @ access public
* @ return void
*/
function woocommerce_cancel_unpaid_orders () {
global $wpdb ;
$held_duration = get_option ( 'woocommerce_hold_stock_minutes' );
if ( $held_duration == '' )
return ;
$date = date ( " Y-m-d H:i:s " , strtotime ( '+' . absint ( $held_duration ) . ' MINUTES' , current_time ( 'timestamp' ) ) );
$unpaid_orders = $wpdb -> get_col ( $wpdb -> prepare ( "
SELECT posts . ID
FROM { $wpdb -> posts } AS posts
LEFT JOIN { $wpdb -> term_relationships } AS rel ON posts . ID = rel . object_ID
LEFT JOIN { $wpdb -> term_taxonomy } AS tax USING ( term_taxonomy_id )
LEFT JOIN { $wpdb -> terms } AS term USING ( term_id )
WHERE posts . post_type = 'shop_order'
AND posts . post_status = 'publish'
AND tax . taxonomy = 'shop_order_status'
AND term . slug IN ( 'pending' )
AND posts . post_date < % s
" , $date ) );
if ( $unpaid_orders ) {
foreach ( $unpaid_orders as $unpaid_order ) {
$order = new WC_Order ( $unpaid_order );
$order -> update_status ( 'cancelled' , __ ( 'Unpaid order cancelled - time limit reached.' , 'woocommerce' ) );
}
}
wp_clear_scheduled_hook ( 'woocommerce_cancel_unpaid_orders' );
wp_schedule_single_event ( time () + ( absint ( $held_duration ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
}
2012-12-31 18:25:09 +00:00
add_action ( 'woocommerce_cancel_unpaid_orders' , 'woocommerce_cancel_unpaid_orders' );
/**
* Process the login .
*
* @ access public
* @ package WooCommerce / Widgets
* @ return void
*/
function woocommerce_sidebar_login_process () {
if ( isset ( $_POST [ 'woocommerce_login' ])) {
global $login_errors ;
// Get redirect URL
$redirect_to = esc_url ( apply_filters ( 'woocommerce_login_widget_redirect' , get_permalink ( woocommerce_get_page_id ( 'myaccount' ) ) ) );
// Check for Secure Cookie
$secure_cookie = '' ;
// If the user wants ssl but the session is not ssl, force a secure cookie.
if ( ! empty ( $_POST [ 'log' ]) && ! force_ssl_admin () ) {
$user_name = sanitize_user ( $_POST [ 'log' ]);
if ( $user = get_user_by ( 'login' , $user_name ) ) {
if ( get_user_option ( 'use_ssl' , $user -> ID ) ) {
$secure_cookie = true ;
force_ssl_admin ( true );
}
}
}
if ( force_ssl_admin () ) $secure_cookie = true ;
if ( $secure_cookie == '' && force_ssl_login () ) $secure_cookie = false ;
// Login
$user = wp_signon ( '' , $secure_cookie );
// Redirect filter
if ( $secure_cookie && strstr ( $redirect_to , 'wp-admin' ) ) $redirect_to = str_replace ( 'http:' , 'https:' , $redirect_to );
// Check the username
if ( ! $_POST [ 'log' ] ) :
$user = new WP_Error ();
$user -> add ( 'empty_username' , '<strong>' . __ ( 'ERROR' , 'woocommerce' ) . '</strong>: ' . __ ( 'Please enter a username.' , 'woocommerce' ));
elseif ( ! $_POST [ 'pwd' ] ) :
$user = new WP_Error ();
$user -> add ( 'empty_username' , '<strong>' . __ ( 'ERROR' , 'woocommerce' ) . '</strong>: ' . __ ( 'Please enter your password.' , 'woocommerce' ));
endif ;
// Redirect if successful
if ( ! is_wp_error ( $user ) ) :
wp_safe_redirect ( $redirect_to );
exit ;
endif ;
$login_errors = $user ;
}
}
add_action ( 'init' , 'woocommerce_sidebar_login_process' , 0 );