2011-12-09 17:01:56 +00:00
< ? php
/**
* WooCommerce Core Functions
*
2011-12-09 19:55:09 +00:00
* Functions available on both the front - end and admin .
2011-12-09 17:01:56 +00:00
*
* @ package WooCommerce
* @ category Core
* @ author WooThemes
*/
2011-12-11 14:40:25 +00:00
2012-04-10 12:45:07 +00:00
/**
* woocommerce_get_dimension function .
*
* Normalise dimensions , unify to cm then convert to wanted unit value
*
* Usage : woocommerce_get_dimension ( 55 , 'in' );
*
* @ access public
* @ param mixed $dim
* @ param mixed $unit 'in' , 'm' , 'cm' , 'm'
* @ return void
2012-05-17 22:42:12 +00:00
*
2012-04-10 12:45:07 +00:00
*/
function woocommerce_get_dimension ( $dim , $to_unit ) {
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 );
// 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
}
/**
* woocommerce_get_weight function .
*
* Normalise weights , unify to cm then convert to wanted unit value
*
* Usage : woocommerce_get_weight ( 55 , 'kg' );
*
* @ access public
* @ param mixed $weight
* @ param mixed $unit 'g' , 'kg' , 'lbs'
* @ return void
*/
function woocommerce_get_weight ( $weight , $to_unit ) {
$from_unit = strtolower ( get_option ( 'woocommerce_weight_unit' ) );
$to_unit = strtolower ( $to_unit );
//Unify all units to kg first
if ( $from_unit !== $to_unit ) {
switch ( $from_unit ) {
case 'g' :
$weight *= 0.001 ;
break ;
case 'lbs' :
$weight *= 0.4535 ;
break ;
}
// Output desired unit
switch ( $to_unit ) {
case 'g' :
$weight *= 1000 ;
break ;
case 'lbs' :
$weight *= 2.204 ;
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
/**
* Get the placeholder for products etc
**/
function woocommerce_placeholder_img_src () {
global $woocommerce ;
return apply_filters ( 'woocommerce_placeholder_img_src' , $woocommerce -> plugin_url () . '/assets/images/placeholder.png' );
}
2011-12-10 17:28:32 +00:00
/**
* HTML emails from WooCommerce
**/
function woocommerce_mail ( $to , $subject , $message , $headers = " Content-Type: text/html \r \n " , $attachments = " " ) {
global $woocommerce ;
$mailer = $woocommerce -> mailer ();
$mailer -> send ( $to , $subject , $message , $headers , $attachments );
}
2012-01-06 17:14:31 +00:00
/**
* WooCommerce page IDs
*
* retrieve page ids - used for myaccount , edit_address , change_password , shop , cart , checkout , pay , view_order , thanks , terms , order_tracking
2012-01-25 20:13:54 +00:00
*
* returns - 1 if no page is found
2012-01-06 17:14:31 +00:00
**/
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'woocommerce_get_page_id' ) ) {
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' ));
return ( $page ) ? $page : - 1 ;
2012-01-06 17:14:31 +00:00
}
}
2012-02-01 19:11:36 +00:00
/**
* WooCommerce clear cart
*
* Clears the cart session when called
**/
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'woocommerce_empty_cart' ) ) {
2012-02-01 19:11:36 +00:00
function woocommerce_empty_cart () {
global $woocommerce ;
if ( ! isset ( $woocommerce -> cart ) || $woocommerce -> cart == '' ) $woocommerce -> cart = new WC_Cart ();
2012-03-04 12:37:41 +00:00
$woocommerce -> cart -> empty_cart ( false );
}
}
2012-03-12 09:43:22 +00:00
/**
* WooCommerce disable admin bar
*
**/
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'woocommerce_disable_admin_bar' ) ) {
2012-03-12 09:43:22 +00:00
function woocommerce_disable_admin_bar () {
2012-03-16 11:18:53 +00:00
if ( get_option ( 'woocommerce_lock_down_admin' ) == 'yes' && ! current_user_can ( 'edit_posts' ) ) {
2012-03-16 15:09:12 +00:00
add_filter ( 'show_admin_bar' , '__return_false' );
wp_deregister_style ( 'admin-bar' );
remove_action ( 'wp_head' , '_admin_bar_bump_cb' );
2012-03-16 11:18:53 +00:00
}
2012-03-12 09:43:22 +00:00
}
}
2012-03-04 12:37:41 +00:00
/**
* Load the cart upon login
**/
function woocommerce_load_persistent_cart ( $user_login , $user ) {
global $woocommerce ;
$saved_cart = get_user_meta ( $user -> ID , '_woocommerce_persistent_cart' , true );
if ( $saved_cart ) {
if ( ! isset ( $_SESSION [ 'cart' ]) || ! is_array ( $_SESSION [ 'cart' ]) || sizeof ( $_SESSION [ 'cart' ]) == 0 ) {
$_SESSION [ 'cart' ] = $saved_cart [ 'cart' ];
}
2012-02-01 19:11:36 +00:00
}
}
2011-12-09 17:01:56 +00:00
/**
* WooCommerce conditionals
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 )
2011-12-09 17:01:56 +00:00
**/
function is_woocommerce () {
if ( is_shop () || is_product_category () || is_product_tag () || is_product ()) return true ; else return false ;
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'is_shop' ) ) {
2011-12-09 17:01:56 +00:00
function is_shop () {
2012-01-06 17:14:31 +00:00
if ( is_post_type_archive ( 'product' ) || is_page ( woocommerce_get_page_id ( 'shop' ))) return true ; else return false ;
2011-12-09 17:01:56 +00:00
}
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'is_product_category' ) ) {
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-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' ) ) {
2011-12-09 17:01:56 +00:00
function is_product () {
return is_singular ( array ( 'product' ) );
}
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'is_cart' ) ) {
2011-12-09 17:01:56 +00:00
function is_cart () {
2012-01-06 17:14:31 +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' ) ) {
2011-12-09 17:01:56 +00:00
function is_checkout () {
2012-01-06 17:14:31 +00:00
if ( is_page ( woocommerce_get_page_id ( 'checkout' )) || is_page ( woocommerce_get_page_id ( 'pay' ))) return true ; else return false ;
2011-12-09 17:01:56 +00:00
}
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'is_account_page' ) ) {
2011-12-09 17:01:56 +00:00
function is_account_page () {
2012-01-06 17:14:31 +00:00
if ( 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' )) ) return true ; else return false ;
2011-12-09 17:01:56 +00:00
}
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'is_ajax' ) ) {
2011-12-09 17:01:56 +00:00
function is_ajax () {
if ( defined ( 'DOING_AJAX' ) ) return true ;
if ( isset ( $_SERVER [ 'HTTP_X_REQUESTED_WITH' ]) && strtolower ( $_SERVER [ 'HTTP_X_REQUESTED_WITH' ]) == 'xmlhttprequest' ) return true ; else return false ;
}
}
/**
2011-12-09 19:55:09 +00:00
* Get template part ( for templates like the shop - loop )
*/
function woocommerce_get_template_part ( $slug , $name = '' ) {
global $woocommerce ;
2012-02-19 03:33:49 +00:00
$template = '' ;
2012-02-22 20:50:37 +00:00
// Look in yourtheme/slug-name.php and yourtheme/woocommerce/slug-name.php
if ( $name )
2012-02-19 03:33:49 +00:00
$template = locate_template ( array ( " { $slug } - { $name } .php " , " { $woocommerce -> template_url } { $slug } - { $name } .php " ) );
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
if ( ! $template )
2012-02-19 03:33:49 +00:00
$template = locate_template ( array ( " { $slug } .php " , " { $woocommerce -> template_url } { $slug } .php " ) );
2012-02-22 20:50:37 +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-02-03 16:17:35 +00:00
* Get other templates ( e . g . product attributes ) passing attributes and including the file
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-01-29 13:36:33 +00:00
2012-02-03 16:17:35 +00:00
if ( $args && is_array ( $args ) )
extract ( $args );
2012-05-14 14:36:47 +00:00
$located = woocommerce_locate_template ( $template_name , $template_path , $default_path );
2012-03-06 17:34:44 +00:00
do_action ( 'woocommerce_before_template_part' , $template_name , $template_path , $located );
2012-03-06 13:26:10 +00:00
include ( $located );
2012-03-06 17:34:44 +00:00
do_action ( 'woocommerce_after_template_part' , $template_name , $template_path , $located );
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-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-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-01-29 13:36:33 +00:00
2012-05-14 14:36:47 +00:00
// Look within passed path within the theme - this is priority
$template = locate_template (
array (
$template_path . $template_name ,
$template_name
)
);
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
/**
* Get Currency
**/
function get_woocommerce_currency () {
return apply_filters ( 'woocommerce_currency' , get_option ( 'woocommerce_currency' ) );
}
2011-12-09 17:01:56 +00:00
/**
* Currency
**/
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 ;
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 ;
case 'PLN' : $currency_symbol = 'łz' ; break ;
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 ;
2011-12-09 17:01:56 +00:00
default : $currency_symbol = '£' ; break ;
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
}
/**
2011-12-09 19:55:09 +00:00
* Price Formatting Helper
2011-12-09 17:01:56 +00:00
**/
function woocommerce_price ( $price , $args = array () ) {
global $woocommerce ;
extract ( shortcode_atts ( array (
'ex_tax_label' => '0'
), $args ));
$return = '' ;
$num_decimals = ( int ) get_option ( 'woocommerce_price_num_decimals' );
$currency_pos = get_option ( 'woocommerce_currency_pos' );
$currency_symbol = get_woocommerce_currency_symbol ();
2012-04-12 10:25:50 +00:00
$price = apply_filters ( 'raw_woocommerce_price' , ( double ) $price );
$price = number_format ( $price , $num_decimals , stripslashes ( get_option ( 'woocommerce_price_decimal_sep' )), stripslashes ( get_option ( 'woocommerce_price_thousand_sep' )) );
if ( get_option ( 'woocommerce_price_trim_zeros' ) == 'yes' && $num_decimals > 0 )
2012-01-27 15:00:12 +00:00
$price = woocommerce_trim_zeros ( $price );
2011-12-09 17:01:56 +00:00
switch ( $currency_pos ) :
case 'left' :
2012-01-11 15:17:34 +00:00
$return = '<span class="amount">' . $currency_symbol . $price . '</span>' ;
2011-12-09 17:01:56 +00:00
break ;
case 'right' :
2012-01-11 15:17:34 +00:00
$return = '<span class="amount">' . $price . $currency_symbol . '</span>' ;
2011-12-09 17:01:56 +00:00
break ;
case 'left_space' :
2012-02-09 08:39:07 +00:00
$return = '<span class="amount">' . $currency_symbol . ' ' . $price . '</span>' ;
2011-12-09 17:01:56 +00:00
break ;
case 'right_space' :
2012-02-09 08:39:07 +00:00
$return = '<span class="amount">' . $price . ' ' . $currency_symbol . '</span>' ;
2011-12-09 17:01:56 +00:00
break ;
endswitch ;
if ( $ex_tax_label && get_option ( 'woocommerce_calc_taxes' ) == 'yes' ) $return .= ' <small>' . $woocommerce -> countries -> ex_tax_or_vat () . '</small>' ;
return $return ;
}
2012-01-27 15:00:12 +00:00
/**
* Trim trailing zeros
**/
function woocommerce_trim_zeros ( $price ) {
2012-01-28 15:20:27 +00:00
return preg_replace ( '/' . preg_quote ( get_option ( 'woocommerce_price_decimal_sep' ), '/' ) . '0++$/' , '' , $price );
2012-01-27 15:00:12 +00:00
}
2011-12-09 17:01:56 +00:00
/**
* Clean variables
**/
function woocommerce_clean ( $var ) {
return trim ( strip_tags ( stripslashes ( $var )));
}
2011-12-16 15:11:41 +00:00
/**
* Merge two arrays
**/
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 ];
}
}
return $a1 ;
}
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
**/
function woocommerce_get_term_top_most_parent ( $term_id , $taxonomy ) {
// start from the current term
$parent = get_term_by ( 'id' , $term_id , $taxonomy );
// climb up the hierarchy until we reach a term with parent = '0'
while ( $parent -> parent != '0' ){
$term_id = $parent -> parent ;
$parent = get_term_by ( 'id' , $term_id , $taxonomy );
}
return $parent ;
}
2011-12-09 17:01:56 +00:00
/**
* Variation Formatting
*
* Gets a formatted version of variation data or item meta
**/
function woocommerce_get_formatted_variation ( $variation = '' , $flat = false ) {
global $woocommerce ;
if ( is_array ( $variation )) :
if ( ! $flat ) $return = '<dl class="variation">' ; else $return = '' ;
$variation_list = array ();
foreach ( $variation as $name => $value ) :
if ( ! $value ) continue ;
// If this is a term slug, get the term's nice name
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 ) :
$value = $term -> name ;
endif ;
else :
$value = ucfirst ( $value );
endif ;
if ( $flat ) :
$variation_list [] = $woocommerce -> attribute_label ( str_replace ( 'attribute_' , '' , $name )) . ': ' . $value ;
else :
$variation_list [] = '<dt>' . $woocommerce -> attribute_label ( str_replace ( 'attribute_' , '' , $name )) . ':</dt><dd>' . $value . '</dd>' ;
endif ;
endforeach ;
if ( $flat ) :
$return .= implode ( ', ' , $variation_list );
else :
$return .= implode ( '' , $variation_list );
endif ;
if ( ! $flat ) $return .= '</dl>' ;
return $return ;
endif ;
}
2011-12-10 17:28:32 +00:00
/**
* Hex darker / lighter / contrast functions for colours
**/
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'woocommerce_rgb_from_hex' ) ) {
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 );
$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' ) ) {
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 ;
$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 ;
return $color ;
}
}
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'woocommerce_hex_lighter' ) ) {
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 ) :
$amount = 255 - $v ;
$amount = $amount / 100 ;
$amount = round ( $amount * $factor );
$new_decimal = $v + $amount ;
$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 ;
return $color ;
}
}
2012-05-01 18:28:45 +00:00
/**
* Detect if we should use a light or dark colour on a background colour
**/
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'woocommerce_light_or_dark' ) ) {
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 ;
return $brightness > 155 ? $dark : $light ;
}
}
/**
* Format string as hex
**/
2012-06-14 11:15:18 +00:00
if ( ! function_exists ( 'woocommerce_format_hex' ) ) {
2012-05-01 18:28:45 +00:00
function woocommerce_format_hex ( $hex ) {
$hex = trim ( str_replace ( '#' , '' , $hex ) );
if ( strlen ( $hex ) == 3 ) {
$hex = $hex [ 0 ] . $hex [ 0 ] . $hex [ 1 ] . $hex [ 1 ] . $hex [ 2 ] . $hex [ 2 ];
}
if ( $hex ) return '#' . $hex ;
2011-12-10 17:28:32 +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' );
**/
add_filter ( 'comments_clauses' , 'woocommerce_exclude_order_comments' , 10 , 1 );
2011-12-21 22:46:29 +00:00
add_action ( 'comment_feed_join' , 'woocommerce_exclude_order_comments_from_feed_join' );
add_action ( 'comment_feed_where' , 'woocommerce_exclude_order_comments_from_feed_where' );
2011-12-09 19:55:09 +00:00
function woocommerce_exclude_order_comments ( $clauses ) {
global $wpdb , $typenow ;
if ( is_admin () && $typenow == 'shop_order' ) return $clauses ; // Don't hide when viewing orders in admin
$clauses [ 'join' ] = " LEFT JOIN $wpdb->posts ON $wpdb->comments .comment_post_ID = $wpdb->posts .ID " ;
if ( $clauses [ 'where' ]) $clauses [ 'where' ] .= ' AND ' ;
$clauses [ 'where' ] .= "
$wpdb -> posts . post_type NOT IN ( 'shop_order' )
" ;
return $clauses ;
}
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 ;
2011-12-21 22:46:29 +00:00
if ( ! $join ) $join = " JOIN $wpdb->posts ON ( $wpdb->comments .comment_post_ID = $wpdb->posts .ID ) " ;
return $join ;
}
function woocommerce_exclude_order_comments_from_feed_where ( $where ) {
global $wpdb ;
2011-12-09 19:55:09 +00:00
if ( $where ) $where .= ' AND ' ;
$where .= " $wpdb->posts .post_type NOT IN ('shop_order') " ;
return $where ;
}
2011-12-09 17:01:56 +00:00
/**
* Order Status completed - GIVE DOWNLOADABLE PRODUCT ACCESS TO CUSTOMER
**/
add_action ( 'woocommerce_order_status_completed' , 'woocommerce_downloadable_product_permissions' );
2012-02-24 20:04:05 +00:00
if ( get_option ( 'woocommerce_downloads_grant_access_after_payment' ) == 'yes' ) add_action ( 'woocommerce_order_status_processing' , 'woocommerce_downloadable_product_permissions' );
2011-12-09 17:01:56 +00:00
function woocommerce_downloadable_product_permissions ( $order_id ) {
global $wpdb ;
2012-02-25 21:11:06 +00:00
if ( get_post_meta ( $order_id , __ ( 'Download Permissions Granted' , 'woocommerce' ), true ) == 1 ) return ; // Only do this once
2012-02-24 20:04:05 +00:00
2012-01-27 16:38:39 +00:00
$order = new WC_Order ( $order_id );
2011-12-09 17:01:56 +00:00
2012-01-13 00:46:56 +00:00
if ( sizeof ( $order -> get_items ()) > 0 ) foreach ( $order -> get_items () as $item ) :
2011-12-09 17:01:56 +00:00
if ( $item [ 'id' ] > 0 ) :
$_product = $order -> get_product_from_item ( $item );
2012-04-20 10:17:40 +00:00
if ( $_product -> exists () && $_product -> is_downloadable () ) :
2011-12-09 17:01:56 +00:00
$download_id = ( $item [ 'variation_id' ] > 0 ) ? $item [ 'variation_id' ] : $item [ 'id' ];
$user_email = $order -> billing_email ;
2011-12-24 16:57:36 +00:00
$limit = trim ( get_post_meta ( $download_id , '_download_limit' , true ));
2012-02-25 21:11:06 +00:00
$expiry = trim ( get_post_meta ( $download_id , '_download_expiry' , true ));
2011-12-09 17:01:56 +00:00
2012-04-10 21:37:29 +00:00
$limit = ( empty ( $limit )) ? '' : ( int ) $limit ;
// Default value is NULL in the table schema
$expiry = ( empty ( $expiry )) ? null : ( int ) $expiry ;
2012-02-25 21:11:06 +00:00
if ( $expiry ) $expiry = date ( " Y-m-d " , strtotime ( 'NOW + ' . $expiry . ' DAY' ));
2012-04-10 21:37:29 +00:00
$data = array (
'product_id' => $download_id ,
2012-02-25 21:11:06 +00:00
'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
2012-04-10 21:37:29 +00:00
);
$format = array (
2011-12-09 17:01:56 +00:00
'%s' ,
'%s' ,
'%s' ,
'%s' ,
'%s' ,
2012-02-25 21:11:06 +00:00
'%s' ,
'%s' ,
'%d'
2012-04-10 21:37:29 +00:00
);
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
);
2011-12-09 17:01:56 +00:00
endif ;
endif ;
endforeach ;
2012-02-24 20:04:05 +00:00
2012-02-25 21:11:06 +00:00
update_post_meta ( $order_id , __ ( 'Download Permissions Granted' , 'woocommerce' ), 1 );
2011-12-09 17:01:56 +00:00
}
2011-12-19 17:29:24 +00:00
/**
* Order Status completed - This is a paying customer
**/
add_action ( 'woocommerce_order_status_completed' , 'woocommerce_paying_customer' );
function woocommerce_paying_customer ( $order_id ) {
2012-01-27 16:38:39 +00:00
$order = new WC_Order ( $order_id );
2011-12-19 17:29:24 +00:00
if ( $order -> user_id > 0 ) update_user_meta ( $order -> user_id , 'paying_customer' , 1 );
}
2011-12-09 17:01:56 +00:00
/**
* Filter to allow product_cat in the permalinks for products .
*
* @ param string $permalink The existing permalink URL .
*/
2011-12-09 19:55:09 +00:00
add_filter ( 'post_type_link' , 'woocommerce_product_cat_filter_post_link' , 10 , 4 );
2011-12-09 17:01:56 +00:00
function woocommerce_product_cat_filter_post_link ( $permalink , $post , $leavename , $sample ) {
// Abort if post is not a product
if ( $post -> post_type !== 'product' ) return $permalink ;
// Abort early if the placeholder rewrite tag isn't in the generated URL
if ( false === strpos ( $permalink , '%product_cat%' ) ) return $permalink ;
// Get the custom taxonomy terms in use by this post
$terms = get_the_terms ( $post -> ID , 'product_cat' );
if ( empty ( $terms ) ) :
// If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
2012-03-08 10:29:11 +00:00
$permalink = str_replace ( '%product_cat%' , _x ( 'product' , 'slug' , 'woocommerce' ), $permalink );
2011-12-09 17:01:56 +00:00
else :
// Replace the placeholder rewrite tag with the first term's slug
$first_term = array_shift ( $terms );
$permalink = str_replace ( '%product_cat%' , $first_term -> slug , $permalink );
endif ;
return $permalink ;
}
/**
* Add term ordering to get_terms
*
* It enables the support a 'menu_order' parameter to get_terms for the product_cat taxonomy .
* By default it is 'ASC' . It accepts 'DESC' too
*
* To disable it , set it ot false ( or 0 )
*
*/
add_filter ( 'terms_clauses' , 'woocommerce_terms_clauses' , 10 , 3 );
function woocommerce_terms_clauses ( $clauses , $taxonomies , $args ) {
global $wpdb , $woocommerce ;
// No sorting when menu_order is false
if ( isset ( $args [ 'menu_order' ]) && $args [ 'menu_order' ] == false ) return $clauses ;
// No sorting when orderby is non default
if ( isset ( $args [ 'orderby' ]) && $args [ 'orderby' ] != 'name' ) return $clauses ;
// No sorting in admin when sorting by a column
if ( isset ( $_GET [ 'orderby' ]) ) return $clauses ;
// wordpress should give us the taxonomies asked when calling the get_terms function. Only apply to categories and pa_ attributes
$found = false ;
foreach (( array ) $taxonomies as $taxonomy ) :
if ( $taxonomy == 'product_cat' || strstr ( $taxonomy , 'pa_' )) :
$found = true ;
break ;
endif ;
endforeach ;
if ( ! $found ) return $clauses ;
// 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
if ( strpos ( 'COUNT(*)' , $clauses [ 'fields' ]) === false ) $clauses [ 'fields' ] .= ', tm.* ' ;
//query join
$clauses [ 'join' ] .= " LEFT JOIN { $wpdb -> woocommerce_termmeta } AS tm ON (t.term_id = tm.woocommerce_term_id AND tm.meta_key = ' " . $meta_name . " ') " ;
// default to ASC
if ( ! isset ( $args [ 'menu_order' ]) || ! in_array ( strtoupper ( $args [ 'menu_order' ]), array ( 'ASC' , 'DESC' )) ) $args [ 'menu_order' ] = 'ASC' ;
$order = " ORDER BY CAST(tm.meta_value AS SIGNED) " . $args [ 'menu_order' ];
if ( $clauses [ 'orderby' ] ) :
$clauses [ 'orderby' ] = str_replace ( 'ORDER BY' , $order . ',' , $clauses [ 'orderby' ] );
else :
$clauses [ 'orderby' ] = $order ;
endif ;
return $clauses ;
}
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 .
*
* @ 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' ) {
$terms = array ();
$object_terms = wp_get_object_terms ( $object_id , $taxonomy );
$all_terms = array_flip ( get_terms ( $taxonomy , array ( 'menu_order' => 'ASC' , 'fields' => 'ids' ) ) );
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 ;
}
ksort ( $terms );
return $terms ;
}
2011-12-09 17:01:56 +00:00
/**
* WooCommerce Dropdown categories
*
* 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
2011-12-09 17:01:56 +00:00
*/
function woocommerce_product_dropdown_categories ( $show_counts = 1 , $hierarchal = 1 ) {
2012-04-05 16:51:28 +00:00
global $wp_query , $woocommerce ;
include_once ( $woocommerce -> plugin_path () . '/classes/walkers/class-product-cat-dropdown-walker.php' );
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 ;
$r [ 'show_count' ] = 1 ;
$r [ 'selected' ] = ( isset ( $wp_query -> query [ 'product_cat' ] ) ) ? $wp_query -> query [ 'product_cat' ] : '' ;
2011-12-09 17:01:56 +00:00
$terms = get_terms ( 'product_cat' , $r );
2012-04-05 16:51:28 +00:00
2011-12-09 17:01:56 +00:00
if ( ! $terms ) return ;
$output = " <select name='product_cat' id='dropdown_product_cat'> " ;
2012-06-10 18:29:04 +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-06-10 18:29:04 +00:00
$output .= '<option value="0" ' . selected ( isset ( $_GET [ 'product_cat' ] ) ? $_GET [ 'product_cat' ] : '' , '0' , false ) . '>' . __ ( 'Uncategorized' , 'woocommerce' ) . '</option>' ;
2011-12-09 17:01:56 +00:00
$output .= " </select> " ;
echo $output ;
}
/**
* Walk the Product Categories .
*/
function woocommerce_walk_category_dropdown_tree () {
$args = func_get_args ();
2012-04-05 16:51:28 +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 );
}
2011-12-09 19:55:09 +00:00
/**
* WooCommerce Term Meta API
*
* API for working with term meta data . Adapted from 'Term meta API' by Nikolay Karev
*
*/
add_action ( 'init' , 'woocommerce_taxonomy_metadata_wpdbfix' , 0 );
add_action ( 'switch_blog' , 'woocommerce_taxonomy_metadata_wpdbfix' , 0 );
function woocommerce_taxonomy_metadata_wpdbfix () {
global $wpdb ;
$variable_name = 'woocommerce_termmeta' ;
$wpdb -> $variable_name = $wpdb -> prefix . $variable_name ;
$wpdb -> tables [] = $variable_name ;
}
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 );
}
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 );
}
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 );
}
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
}
/**
* Move a term before the a given element of its hierarchy level
*
* @ param object $the_term
* @ param int $next_id the id of the next slibling element in save hierachy level
* @ param int $index
* @ param int $terms
*/
function woocommerce_order_terms ( $the_term , $next_id , $taxonomy , $index = 0 , $terms = null ) {
if ( ! $terms ) $terms = get_terms ( $taxonomy , 'menu_order=ASC&hide_empty=0&parent=0' );
if ( empty ( $terms ) ) return $index ;
$id = $the_term -> term_id ;
$term_in_level = false ; // flag: is our term to order in this level of terms
foreach ( $terms as $term ) {
if ( $term -> term_id == $id ) { // our term to order, we skip
$term_in_level = true ;
continue ; // our term to order, we skip
}
// the nextid of our term to order, lets move our term here
if ( null !== $next_id && $term -> term_id == $next_id ) {
$index ++ ;
$index = woocommerce_set_term_order ( $id , $index , $taxonomy , true );
}
// set order
$index ++ ;
$index = woocommerce_set_term_order ( $term -> term_id , $index , $taxonomy );
// 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 ) ) {
$index = woocommerce_order_terms ( $the_term , $next_id , $taxonomy , $index , $children );
}
}
// 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
wp_cache_flush ();
2011-12-11 13:11:00 +00:00
return $index ;
}
/**
* Set the sort order of a term
*
* @ param int $term_id
* @ param int $index
* @ param bool $recursive
*/
function woocommerce_set_term_order ( $term_id , $index , $taxonomy , $recursive = false ) {
global $wpdb ;
$term_id = ( int ) $term_id ;
$index = ( int ) $index ;
// Meta name
if ( strstr ( $taxonomy , 'pa_' )) :
$meta_name = 'order_' . esc_attr ( $taxonomy );
else :
$meta_name = 'order' ;
endif ;
update_woocommerce_term_meta ( $term_id , $meta_name , $index );
if ( ! $recursive ) return $index ;
$children = get_terms ( $taxonomy , " parent= $term_id &menu_order=ASC&hide_empty=0 " );
foreach ( $children as $term ) {
$index ++ ;
$index = woocommerce_set_term_order ( $term -> term_id , $index , $taxonomy , true );
}
return $index ;
2012-03-23 16:28:27 +00:00
}
/**
* let_to_num function .
*
* This function transforms the php . ini notation for numbers ( like '2M' ) to an integer
*
* @ 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
}