2011-08-10 17:11:11 +00:00
< ? php
/**
2011-12-11 00:33:28 +00:00
* WooCommerce Functions
2012-08-10 11:15:32 +00:00
*
2011-12-11 00:33:28 +00:00
* Hooked - in functions for WooCommerce related events on the front - end .
2011-08-10 17:11:11 +00:00
*
2012-08-14 15:30:23 +00:00
* @ author WooThemes
* @ category Core
* @ package WooCommerce / Functions
* @ version 1.6 . 4
2011-08-10 17:11:11 +00:00
*/
2013-03-11 10:23:14 +00:00
/**
* Handle IPN requests for the legacy paypal gateway by calling gateways manually if needed .
*
* @ access public
* @ return void
*/
function woocommerce_legacy_paypal_ipn () {
if ( ! empty ( $_GET [ 'paypalListener' ] ) && $_GET [ 'paypalListener' ] == 'paypal_standard_IPN' ) {
global $woocommerce ;
$woocommerce -> payment_gateways ();
do_action ( 'woocommerce_api_wc_gateway_paypal' );
}
}
add_action ( 'init' , 'woocommerce_legacy_paypal_ipn' );
2011-11-26 20:33:15 +00:00
/**
2012-08-14 15:30:23 +00:00
* Handle redirects before content is output - hooked into template_redirect so is_page works .
*
* @ access public
* @ return void
*/
2012-12-31 13:03:56 +00:00
function woocommerce_template_redirect () {
2012-02-09 15:53:29 +00:00
global $woocommerce , $wp_query ;
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// When default permalinks are enabled, redirect shop page to post type archive url
2012-12-31 13:03:56 +00:00
if ( ! empty ( $_GET [ 'page_id' ] ) && get_option ( 'permalink_structure' ) == " " && $_GET [ 'page_id' ] == woocommerce_get_page_id ( 'shop' ) ) {
2011-12-09 19:55:09 +00:00
wp_safe_redirect ( get_post_type_archive_link ( 'product' ) );
exit ;
2012-12-31 13:03:56 +00:00
}
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// When on the checkout with an empty cart, redirect to cart page
2012-12-31 13:03:56 +00:00
elseif ( is_page ( woocommerce_get_page_id ( 'checkout' ) ) && sizeof ( $woocommerce -> cart -> get_cart () ) == 0 ) {
wp_redirect ( get_permalink ( woocommerce_get_page_id ( 'cart' ) ) );
2012-01-12 00:54:45 +00:00
exit ;
2012-12-31 13:03:56 +00:00
}
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// When on pay page with no query string, redirect to checkout
2012-12-31 13:03:56 +00:00
elseif ( is_page ( woocommerce_get_page_id ( 'pay' ) ) && ! isset ( $_GET [ 'order' ] ) ) {
wp_redirect ( get_permalink ( woocommerce_get_page_id ( 'checkout' ) ) );
2012-01-12 00:54:45 +00:00
exit ;
2012-12-31 13:03:56 +00:00
}
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// My account page redirects (logged out)
2012-12-31 13:03:56 +00:00
elseif ( ! is_user_logged_in () && ( is_page ( woocommerce_get_page_id ( 'edit_address' ) ) || is_page ( woocommerce_get_page_id ( 'view_order' ) ) || is_page ( woocommerce_get_page_id ( 'change_password' ) ) ) ) {
wp_redirect ( get_permalink ( woocommerce_get_page_id ( 'myaccount' ) ) );
2012-01-12 00:54:45 +00:00
exit ;
2012-12-31 13:03:56 +00:00
}
2012-01-12 00:54:45 +00:00
2013-02-10 23:02:18 +00:00
// Logout
elseif ( is_page ( woocommerce_get_page_id ( 'logout' ) ) ) {
wp_redirect ( str_replace ( '&' , '&' , wp_logout_url ( get_permalink ( woocommerce_get_page_id ( 'myaccount' ) ) ) ) );
exit ;
}
2012-02-09 15:02:55 +00:00
// Redirect to the product page if we have a single product
2013-03-22 18:06:07 +00:00
elseif ( is_search () && is_post_type_archive ( 'product' ) && apply_filters ( 'woocommerce_redirect_single_search_result' , true ) && $wp_query -> post_count == 1 ) {
$product = get_product ( $wp_query -> post );
if ( $product -> is_visible () ) {
wp_safe_redirect ( get_permalink ( $product -> id ), 302 );
2012-12-31 13:03:56 +00:00
exit ;
}
}
// Force SSL
elseif ( get_option ( 'woocommerce_force_ssl_checkout' ) == 'yes' && ! is_ssl () ) {
2013-02-14 17:00:26 +00:00
if ( is_checkout () || is_account_page () || apply_filters ( 'woocommerce_force_ssl_checkout' , false ) ) {
2012-12-31 13:03:56 +00:00
if ( 0 === strpos ( $_SERVER [ 'REQUEST_URI' ], 'http' ) ) {
wp_safe_redirect ( preg_replace ( '|^http://|' , 'https://' , $_SERVER [ 'REQUEST_URI' ] ) );
exit ;
} else {
wp_safe_redirect ( 'https://' . $_SERVER [ 'HTTP_HOST' ] . $_SERVER [ 'REQUEST_URI' ] );
exit ;
}
2012-02-09 15:02:55 +00:00
}
2012-12-31 13:03:56 +00:00
}
// Break out of SSL if we leave the checkout/my accounts (anywhere but thanks)
elseif ( get_option ( 'woocommerce_force_ssl_checkout' ) == 'yes' && get_option ( 'woocommerce_unforce_ssl_checkout' ) == 'yes' && is_ssl () && $_SERVER [ 'REQUEST_URI' ] && ! is_checkout () && ! is_page ( woocommerce_get_page_id ( 'thanks' ) ) && ! is_ajax () && ! is_account_page () && apply_filters ( 'woocommerce_unforce_ssl_checkout' , true ) ) {
if ( 0 === strpos ( $_SERVER [ 'REQUEST_URI' ], 'http' ) ) {
wp_safe_redirect ( preg_replace ( '|^https://|' , 'http://' , $_SERVER [ 'REQUEST_URI' ] ) );
exit ;
} else {
wp_safe_redirect ( 'http://' . $_SERVER [ 'HTTP_HOST' ] . $_SERVER [ 'REQUEST_URI' ] );
exit ;
}
}
// Buffer the checkout page
elseif ( is_checkout () ) {
ob_start ();
2012-02-09 15:02:55 +00:00
}
2013-02-10 23:02:18 +00:00
2011-12-09 19:55:09 +00:00
}
2011-11-26 20:33:15 +00:00
2013-02-11 13:30:18 +00:00
/**
* woocommerce_nav_menu_items function .
*
* @ access public
* @ param mixed $items
* @ param mixed $args
* @ return void
*/
function woocommerce_nav_menu_items ( $items , $args ) {
if ( ! is_user_logged_in () ) {
$hide_pages = array ();
$hide_pages [] = ( int ) woocommerce_get_page_id ( 'change_password' );
$hide_pages [] = ( int ) woocommerce_get_page_id ( 'logout' );
$hide_pages [] = ( int ) woocommerce_get_page_id ( 'edit_address' );
$hide_pages [] = ( int ) woocommerce_get_page_id ( 'view_order' );
$hide_pages = apply_filters ( 'woocommerce_logged_out_hidden_page_ids' , $hide_pages );
foreach ( $items as $key => $item ) {
2013-03-27 11:04:51 +00:00
if ( ! empty ( $item -> object_id ) && ! empty ( $item -> object ) && in_array ( $item -> object_id , $hide_pages ) && $item -> object == 'page' ) {
2013-02-11 13:30:18 +00:00
unset ( $items [ $key ] );
}
}
}
return $items ;
}
2012-08-14 15:30:23 +00:00
2011-12-09 19:55:09 +00:00
/**
2012-08-14 15:30:23 +00:00
* Fix active class in nav for shop page .
*
* @ access public
* @ param array $menu_items
* @ param array $args
* @ return array
*/
2011-12-09 19:55:09 +00:00
function woocommerce_nav_menu_item_classes ( $menu_items , $args ) {
2012-08-10 11:15:32 +00:00
2012-03-23 11:05:00 +00:00
if ( ! is_woocommerce () ) return $menu_items ;
2012-08-10 11:15:32 +00:00
2012-01-06 17:14:31 +00:00
$shop_page = ( int ) woocommerce_get_page_id ( 'shop' );
2011-12-09 19:55:09 +00:00
$page_for_posts = ( int ) get_option ( 'page_for_posts' );
2012-03-23 11:05:00 +00:00
foreach ( ( array ) $menu_items as $key => $menu_item ) {
2011-12-09 19:55:09 +00:00
$classes = ( array ) $menu_item -> classes ;
// Unset active class for blog page
2012-03-23 11:05:00 +00:00
if ( $page_for_posts == $menu_item -> object_id ) {
2011-12-09 19:55:09 +00:00
$menu_items [ $key ] -> current = false ;
unset ( $classes [ array_search ( 'current_page_parent' , $classes ) ] );
unset ( $classes [ array_search ( 'current-menu-item' , $classes ) ] );
// Set active state if this is the shop page link
2012-03-23 11:05:00 +00:00
} elseif ( is_shop () && $shop_page == $menu_item -> object_id ) {
2011-12-09 19:55:09 +00:00
$menu_items [ $key ] -> current = true ;
$classes [] = 'current-menu-item' ;
$classes [] = 'current_page_item' ;
2012-08-10 11:15:32 +00:00
2012-03-23 11:05:00 +00:00
// Set parent state if this is a product page
} elseif ( is_singular ( 'product' ) && $shop_page == $menu_item -> object_id ) {
$classes [] = 'current_page_parent' ;
}
2011-12-09 19:55:09 +00:00
$menu_items [ $key ] -> classes = array_unique ( $classes );
2012-08-10 11:15:32 +00:00
2012-03-23 11:05:00 +00:00
}
2011-12-09 19:55:09 +00:00
return $menu_items ;
}
2012-08-14 15:30:23 +00:00
2011-12-09 19:55:09 +00:00
/**
2012-08-14 15:30:23 +00:00
* Fix active class in wp_list_pages for shop page .
2011-12-09 19:55:09 +00:00
*
2012-08-14 15:30:23 +00:00
* https :// github . com / woothemes / woocommerce / issues / 177
*
* @ author Jessor , Peter Sterling
* @ access public
* @ param string $pages
* @ return string
*/
function woocommerce_list_pages ( $pages ){
2011-12-09 19:55:09 +00:00
global $post ;
2011-12-19 19:18:28 +00:00
if ( is_woocommerce ()) {
2011-12-09 19:55:09 +00:00
$pages = str_replace ( 'current_page_parent' , '' , $pages ); // remove current_page_parent class from any item
2012-01-06 17:14:31 +00:00
$shop_page = 'page-item-' . woocommerce_get_page_id ( 'shop' ); // find shop_page_id through woocommerce options
2012-08-10 11:15:32 +00:00
2011-12-09 19:55:09 +00:00
if ( is_shop ()) :
$pages = str_replace ( $shop_page , $shop_page . ' current_page_item' , $pages ); // add current_page_item class to shop page
else :
$pages = str_replace ( $shop_page , $shop_page . ' current_page_parent' , $pages ); // add current_page_parent class to shop page
endif ;
}
return $pages ;
}
2011-08-10 17:11:11 +00:00
/**
2012-08-14 15:30:23 +00:00
* Remove from cart / update .
*
* @ access public
* @ return void
*/
2011-08-10 17:11:11 +00:00
function woocommerce_update_cart_action () {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2012-08-10 11:15:32 +00:00
2011-08-10 17:11:11 +00:00
// Remove from cart
2012-08-21 16:23:26 +00:00
if ( isset ( $_GET [ 'remove_item' ]) && $_GET [ 'remove_item' ] && $woocommerce -> verify_nonce ( 'cart' , '_GET' )) {
2012-08-10 11:15:32 +00:00
2011-09-06 11:11:22 +00:00
$woocommerce -> cart -> set_quantity ( $_GET [ 'remove_item' ], 0 );
2012-08-10 11:15:32 +00:00
2012-10-16 09:45:33 +00:00
$woocommerce -> add_message ( __ ( 'Cart updated.' , 'woocommerce' ) );
2012-08-10 11:15:32 +00:00
2012-02-15 20:19:57 +00:00
$referer = ( wp_get_referer () ) ? wp_get_referer () : $woocommerce -> cart -> get_cart_url ();
wp_safe_redirect ( $referer );
exit ;
2012-08-10 11:15:32 +00:00
2011-08-10 17:11:11 +00:00
// Update Cart
2012-08-21 16:23:26 +00:00
} elseif ( ( ! empty ( $_POST [ 'update_cart' ] ) || ! empty ( $_POST [ 'proceed' ] ) ) && $woocommerce -> verify_nonce ( 'cart' )) {
2012-08-10 11:15:32 +00:00
2012-05-11 21:44:22 +00:00
$cart_totals = isset ( $_POST [ 'cart' ] ) ? $_POST [ 'cart' ] : '' ;
2012-08-10 11:15:32 +00:00
2012-08-21 16:23:26 +00:00
if ( sizeof ( $woocommerce -> cart -> get_cart () ) > 0 ) {
foreach ( $woocommerce -> cart -> get_cart () as $cart_item_key => $values ) {
2012-08-10 11:15:32 +00:00
2012-02-27 18:22:54 +00:00
$_product = $values [ 'data' ];
2012-03-01 08:14:33 +00:00
// Skip product if no updated quantity was posted
if ( ! isset ( $cart_totals [ $cart_item_key ][ 'qty' ] ) )
continue ;
2012-11-19 14:05:03 +00:00
// Sanitize
2013-03-22 20:01:08 +00:00
$quantity = apply_filters ( 'woocommerce_stock_amount_cart_item' , apply_filters ( 'woocommerce_stock_amount' , preg_replace ( " /[^0-9 \ .]/ " , " " , $cart_totals [ $cart_item_key ][ 'qty' ] ) ), $cart_item_key );
2012-11-27 16:22:47 +00:00
2013-03-18 05:38:22 +00:00
if ( " " === $quantity )
2012-11-27 16:22:47 +00:00
continue ;
2012-03-01 08:14:33 +00:00
2012-02-27 18:22:54 +00:00
// Update cart validation
2012-11-19 14:05:03 +00:00
$passed_validation = apply_filters ( 'woocommerce_update_cart_validation' , true , $cart_item_key , $values , $quantity );
2012-08-10 11:15:32 +00:00
2013-02-10 23:02:18 +00:00
// is_sold_individually
if ( $_product -> is_sold_individually () && $quantity > 1 ) {
$woocommerce -> add_error ( sprintf ( __ ( 'You can only have 1 %s in your cart.' , 'woocommerce' ), $_product -> get_title () ) );
$passed_validation = false ;
2012-08-21 16:23:26 +00:00
}
2012-08-10 11:15:32 +00:00
2012-08-21 16:23:26 +00:00
if ( $passed_validation )
2012-02-27 18:22:54 +00:00
$woocommerce -> cart -> set_quantity ( $cart_item_key , $quantity );
2012-08-10 11:15:32 +00:00
2012-08-21 16:23:26 +00:00
}
}
2012-08-10 11:15:32 +00:00
2012-08-21 16:23:26 +00:00
if ( ! empty ( $_POST [ 'proceed' ] ) ) {
wp_safe_redirect ( $woocommerce -> cart -> get_checkout_url () );
exit ;
} else {
2012-10-16 09:45:33 +00:00
$woocommerce -> add_message ( __ ( 'Cart updated.' , 'woocommerce' ) );
2012-08-10 11:15:32 +00:00
2012-08-21 16:23:26 +00:00
$referer = ( wp_get_referer () ) ? wp_get_referer () : $woocommerce -> cart -> get_cart_url ();
$referer = remove_query_arg ( 'remove_discounts' , $referer );
wp_safe_redirect ( $referer );
exit ;
}
2012-02-15 15:54:18 +00:00
2012-08-21 16:23:26 +00:00
}
2011-08-10 17:11:11 +00:00
}
2012-08-14 15:30:23 +00:00
2011-08-10 17:11:11 +00:00
/**
2012-01-27 18:31:30 +00:00
* Add to cart action
*
2012-08-14 15:30:23 +00:00
* Checks for a valid request , does validation ( via hooks ) and then redirects if valid .
*
* @ access public
* @ param bool $url ( default : false )
* @ return void
*/
2011-08-10 17:11:11 +00:00
function woocommerce_add_to_cart_action ( $url = false ) {
2012-11-27 16:22:47 +00:00
if ( empty ( $_REQUEST [ 'add-to-cart' ] ) || ! is_numeric ( $_REQUEST [ 'add-to-cart' ] ) )
2012-09-21 20:11:57 +00:00
return ;
2012-08-10 11:15:32 +00:00
2012-09-21 20:11:57 +00:00
global $woocommerce ;
2012-11-27 16:22:47 +00:00
2013-02-27 07:57:13 +00:00
$product_id = apply_filters ( 'woocommerce_add_to_cart_product_id' , absint ( $_REQUEST [ 'add-to-cart' ] ) );
$was_added_to_cart = false ;
2013-03-22 20:34:16 +00:00
$added_to_cart = array ();
2013-02-27 07:57:13 +00:00
$adding_to_cart = get_product ( $product_id );
2013-02-27 07:55:39 +00:00
$add_to_cart_handler = apply_filters ( 'woocommerce_add_to_cart_handler' , $adding_to_cart -> product_type , $adding_to_cart );
2012-11-27 16:22:47 +00:00
2012-09-21 20:11:57 +00:00
// Variable product handling
2013-02-27 07:55:39 +00:00
if ( 'variable' === $add_to_cart_handler ) {
2012-11-27 16:22:47 +00:00
2012-12-06 14:38:02 +00:00
$variation_id = empty ( $_REQUEST [ 'variation_id' ] ) ? '' : absint ( $_REQUEST [ 'variation_id' ] );
$quantity = empty ( $_REQUEST [ 'quantity' ] ) ? 1 : apply_filters ( 'woocommerce_stock_amount' , $_REQUEST [ 'quantity' ] );
2012-09-21 20:11:57 +00:00
$all_variations_set = true ;
2012-12-06 14:38:02 +00:00
$variations = array ();
2012-09-21 20:11:57 +00:00
// Only allow integer variation ID - if its not set, redirect to the product page
if ( empty ( $variation_id ) ) {
2012-10-16 09:45:33 +00:00
$woocommerce -> add_error ( __ ( 'Please choose product options…' , 'woocommerce' ) );
2013-03-22 20:34:16 +00:00
return ;
2012-09-21 20:11:57 +00:00
}
2012-11-27 16:22:47 +00:00
2012-12-06 14:38:02 +00:00
$attributes = $adding_to_cart -> get_attributes ();
$variation = get_product ( $variation_id );
2012-09-21 20:11:57 +00:00
2012-12-06 14:38:02 +00:00
// Verify all attributes
2012-09-21 20:11:57 +00:00
foreach ( $attributes as $attribute ) {
2012-11-27 16:22:47 +00:00
if ( ! $attribute [ 'is_variation' ] )
2012-09-21 20:11:57 +00:00
continue ;
$taxonomy = 'attribute_' . sanitize_title ( $attribute [ 'name' ] );
2012-12-06 14:38:02 +00:00
if ( ! empty ( $_REQUEST [ $taxonomy ] ) ) {
2012-09-21 20:11:57 +00:00
// Get value from post data
2013-03-10 16:55:53 +00:00
// Don't use woocommerce_clean as it destroys sanitized characters
$value = sanitize_title ( trim ( stripslashes ( $_REQUEST [ $taxonomy ] ) ) );
2012-12-06 14:38:02 +00:00
// Get valid value from variation
$valid_value = $variation -> variation_data [ $taxonomy ];
// Allow if valid
if ( $valid_value == '' || $valid_value == $value ) {
2013-03-07 19:34:29 +00:00
if ( $attribute [ 'is_taxonomy' ] )
$variations [ esc_html ( $attribute [ 'name' ] ) ] = $value ;
else {
// For custom attributes, get the name from the slug
$options = array_map ( 'trim' , explode ( '|' , $attribute [ 'value' ] ) );
foreach ( $options as $option ) {
if ( sanitize_title ( $option ) == $value ) {
$value = $option ;
break ;
}
}
$variations [ esc_html ( $attribute [ 'name' ] ) ] = $value ;
}
2012-12-06 14:38:02 +00:00
continue ;
}
}
2012-09-21 20:11:57 +00:00
2012-12-06 14:38:02 +00:00
$all_variations_set = false ;
2012-09-21 20:11:57 +00:00
}
2012-02-06 18:14:46 +00:00
2012-09-21 20:11:57 +00:00
if ( $all_variations_set ) {
// Add to cart validation
2012-12-19 16:49:20 +00:00
$passed_validation = apply_filters ( 'woocommerce_add_to_cart_validation' , true , $product_id , $quantity , $variation_id , $variations );
2012-08-10 11:15:32 +00:00
2012-09-21 20:11:57 +00:00
if ( $passed_validation ) {
if ( $woocommerce -> cart -> add_to_cart ( $product_id , $quantity , $variation_id , $variations ) ) {
woocommerce_add_to_cart_message ( $product_id );
$was_added_to_cart = true ;
2013-03-22 20:34:16 +00:00
$added_to_cart [] = $product_id ;
2012-01-27 18:31:30 +00:00
}
2012-09-21 20:11:57 +00:00
}
} else {
$woocommerce -> add_error ( __ ( 'Please choose product options…' , 'woocommerce' ) );
2013-03-22 20:34:16 +00:00
return ;
2012-09-21 20:11:57 +00:00
}
2012-08-10 11:15:32 +00:00
2012-09-21 20:11:57 +00:00
// Grouped Products
2013-02-27 07:55:39 +00:00
} elseif ( 'grouped' === $add_to_cart_handler ) {
2012-08-10 11:15:32 +00:00
2012-09-21 20:11:57 +00:00
if ( ! empty ( $_REQUEST [ 'quantity' ] ) && is_array ( $_REQUEST [ 'quantity' ] ) ) {
2012-11-27 16:22:47 +00:00
2012-09-21 20:11:57 +00:00
$quantity_set = false ;
2012-11-27 16:22:47 +00:00
2012-09-21 20:11:57 +00:00
foreach ( $_REQUEST [ 'quantity' ] as $item => $quantity ) {
2012-11-27 16:22:47 +00:00
if ( $quantity <= 0 )
2012-09-21 20:11:57 +00:00
continue ;
2012-11-27 16:22:47 +00:00
2012-09-21 20:11:57 +00:00
$quantity_set = true ;
2012-11-27 16:22:47 +00:00
2012-09-21 20:11:57 +00:00
// Add to cart validation
$passed_validation = apply_filters ( 'woocommerce_add_to_cart_validation' , true , $item , $quantity );
2012-11-27 16:22:47 +00:00
2012-09-21 20:11:57 +00:00
if ( $passed_validation ) {
if ( $woocommerce -> cart -> add_to_cart ( $item , $quantity ) ) {
$was_added_to_cart = true ;
2012-11-08 11:15:52 +00:00
$added_to_cart [] = $item ;
2012-01-27 18:31:30 +00:00
}
}
2012-09-21 20:11:57 +00:00
}
2012-11-27 16:22:47 +00:00
2012-11-08 11:15:52 +00:00
if ( $was_added_to_cart ) {
woocommerce_add_to_cart_message ( $added_to_cart );
}
2012-11-27 16:22:47 +00:00
2012-09-21 20:11:57 +00:00
if ( ! $was_added_to_cart && ! $quantity_set ) {
$woocommerce -> add_error ( __ ( 'Please choose the quantity of items you wish to add to your cart…' , 'woocommerce' ) );
2013-03-22 20:34:16 +00:00
return ;
2012-01-27 18:31:30 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-21 20:11:57 +00:00
} elseif ( $product_id ) {
2012-11-27 16:22:47 +00:00
2012-09-21 20:11:57 +00:00
/* Link on product archives */
$woocommerce -> add_error ( __ ( 'Please choose a product to add to your cart…' , 'woocommerce' ) );
2013-03-22 20:34:16 +00:00
return ;
2012-11-27 16:22:47 +00:00
2012-09-21 20:11:57 +00:00
}
2012-01-27 18:31:30 +00:00
2012-09-21 20:11:57 +00:00
// Simple Products
} else {
2012-08-10 11:15:32 +00:00
2012-11-19 14:05:03 +00:00
$quantity = empty ( $_REQUEST [ 'quantity' ] ) ? 1 : apply_filters ( 'woocommerce_stock_amount' , $_REQUEST [ 'quantity' ] );
2012-08-10 11:15:32 +00:00
2012-09-21 20:11:57 +00:00
// Add to cart validation
$passed_validation = apply_filters ( 'woocommerce_add_to_cart_validation' , true , $product_id , $quantity );
2012-08-10 11:15:32 +00:00
2012-09-21 20:11:57 +00:00
if ( $passed_validation ) {
// Add the product to the cart
if ( $woocommerce -> cart -> add_to_cart ( $product_id , $quantity ) ) {
woocommerce_add_to_cart_message ( $product_id );
$was_added_to_cart = true ;
2013-03-22 20:34:16 +00:00
$added_to_cart [] = $product_id ;
2012-01-27 18:31:30 +00:00
}
2012-09-21 20:11:57 +00:00
}
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
}
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
// If we added the product to the cart we can now do a redirect, otherwise just continue loading the page to show errors
2012-09-21 20:11:57 +00:00
if ( $was_added_to_cart ) {
2012-08-10 11:15:32 +00:00
2012-06-10 12:53:26 +00:00
$url = apply_filters ( 'add_to_cart_redirect' , $url );
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
// If has custom URL redirect there
if ( $url ) {
wp_safe_redirect ( $url );
2011-08-18 23:14:35 +00:00
exit ;
2012-01-27 18:31:30 +00:00
}
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
// Redirect to cart option
2012-08-31 09:05:28 +00:00
elseif ( get_option ( 'woocommerce_cart_redirect_after_add' ) == 'yes' && $woocommerce -> error_count () == 0 ) {
2012-01-27 18:31:30 +00:00
wp_safe_redirect ( $woocommerce -> cart -> get_cart_url () );
exit ;
}
2012-11-27 16:22:47 +00:00
2012-08-31 09:05:28 +00:00
// Redirect to page without querystring args
elseif ( wp_get_referer () ) {
2013-03-22 20:34:16 +00:00
wp_safe_redirect ( add_query_arg ( 'added-to-cart' , implode ( ',' , $added_to_cart ), remove_query_arg ( array ( 'add-to-cart' , 'quantity' , 'product_id' ), wp_get_referer () ) ) );
2012-08-31 09:05:28 +00:00
exit ;
}
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
}
2011-08-10 17:11:11 +00:00
}
2012-08-14 15:30:23 +00:00
2011-11-27 01:24:57 +00:00
/**
2012-08-14 15:30:23 +00:00
* Add to cart messages .
*
* @ access public
* @ return void
*/
2012-09-21 20:11:57 +00:00
function woocommerce_add_to_cart_message ( $product_id ) {
2011-11-27 01:24:57 +00:00
global $woocommerce ;
2012-11-27 16:22:47 +00:00
2012-11-08 11:15:52 +00:00
if ( is_array ( $product_id ) ) {
2012-11-27 16:22:47 +00:00
2012-11-08 11:15:52 +00:00
$titles = array ();
2012-11-27 16:22:47 +00:00
2012-11-08 11:15:52 +00:00
foreach ( $product_id as $id ) {
$titles [] = get_the_title ( $id );
}
2013-04-17 15:03:23 +00:00
$added_text = sprintf ( __ ( 'Added "%s" to your cart.' , 'woocommerce' ), join ( __ ( '" and "' , 'woocommerce' ), array_filter ( array_merge ( array ( join ( '", "' , array_slice ( $titles , 0 , - 1 ) ) ), array_slice ( $titles , - 1 ) ) ) ) );
2012-11-27 16:22:47 +00:00
2012-11-08 11:15:52 +00:00
} else {
$added_text = sprintf ( __ ( '"%s" was successfully added to your cart.' , 'woocommerce' ), get_the_title ( $product_id ) );
}
2012-08-10 11:15:32 +00:00
2011-11-27 01:24:57 +00:00
// Output success messages
2012-09-21 20:11:57 +00:00
if ( get_option ( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :
2012-08-10 11:15:32 +00:00
2012-12-05 18:02:56 +00:00
$return_to = apply_filters ( 'woocommerce_continue_shopping_redirect' , wp_get_referer () ? wp_get_referer () : home_url () );
2012-08-10 11:15:32 +00:00
2012-10-16 09:45:33 +00:00
$message = sprintf ( '<a href="%s" class="button">%s</a> %s' , $return_to , __ ( 'Continue Shopping →' , 'woocommerce' ), $added_text );
2012-08-10 11:15:32 +00:00
2011-11-27 01:24:57 +00:00
else :
2012-08-10 11:15:32 +00:00
2012-12-05 18:02:56 +00:00
$message = sprintf ( '<a href="%s" class="button">%s</a> %s' , get_permalink ( woocommerce_get_page_id ( 'cart' ) ), __ ( 'View Cart →' , 'woocommerce' ), $added_text );
2012-08-10 11:15:32 +00:00
2011-11-27 01:24:57 +00:00
endif ;
2012-08-10 11:15:32 +00:00
2012-01-31 09:48:28 +00:00
$woocommerce -> add_message ( apply_filters ( 'woocommerce_add_to_cart_message' , $message ) );
2011-11-27 01:24:57 +00:00
}
2012-08-14 15:30:23 +00:00
2011-08-10 17:11:11 +00:00
/**
2012-08-14 15:30:23 +00:00
* Clear cart after payment .
*
* @ access public
* @ return void
*/
2011-12-09 19:55:09 +00:00
function woocommerce_clear_cart_after_payment () {
2011-09-09 10:54:46 +00:00
global $woocommerce ;
2012-08-10 11:15:32 +00:00
2012-08-14 15:30:23 +00:00
if ( is_page ( woocommerce_get_page_id ( 'thanks' ) ) ) {
2012-08-10 11:15:32 +00:00
2012-08-14 15:30:23 +00:00
if ( isset ( $_GET [ 'order' ] ) )
$order_id = $_GET [ 'order' ];
else
$order_id = 0 ;
2012-08-10 11:15:32 +00:00
2012-08-14 15:30:23 +00:00
if ( isset ( $_GET [ 'key' ] ) )
$order_key = $_GET [ 'key' ];
else
$order_key = '' ;
2012-08-10 11:15:32 +00:00
2012-08-14 15:30:23 +00:00
if ( $order_id > 0 ) {
$order = new WC_Order ( $order_id );
2012-08-10 11:15:32 +00:00
2012-08-14 15:30:23 +00:00
if ( $order -> order_key == $order_key ) {
$woocommerce -> cart -> empty_cart ();
}
}
2012-08-10 11:15:32 +00:00
2012-08-14 15:30:23 +00:00
}
2012-08-10 11:15:32 +00:00
2012-09-07 17:26:13 +00:00
if ( $woocommerce -> session -> order_awaiting_payment > 0 ) {
2012-08-10 11:15:32 +00:00
2012-09-07 17:26:13 +00:00
$order = new WC_Order ( $woocommerce -> session -> order_awaiting_payment );
2012-08-10 11:15:32 +00:00
2012-08-14 15:30:23 +00:00
if ( $order -> id > 0 && $order -> status !== 'pending' ) {
2011-09-06 11:11:22 +00:00
$woocommerce -> cart -> empty_cart ();
2012-08-14 15:30:23 +00:00
}
}
2011-08-10 17:11:11 +00:00
}
2012-08-14 15:30:23 +00:00
2012-01-12 00:54:45 +00:00
/**
2012-08-14 15:30:23 +00:00
* Process the checkout form .
*
* @ access public
* @ return void
*/
2012-01-12 00:54:45 +00:00
function woocommerce_checkout_action () {
global $woocommerce ;
2012-08-10 11:15:32 +00:00
2012-08-14 15:30:23 +00:00
if ( isset ( $_POST [ 'woocommerce_checkout_place_order' ] ) || isset ( $_POST [ 'woocommerce_checkout_update_totals' ] ) ) {
2012-08-10 11:15:32 +00:00
2012-08-14 15:30:23 +00:00
if ( sizeof ( $woocommerce -> cart -> get_cart () ) == 0 ) {
wp_redirect ( get_permalink ( woocommerce_get_page_id ( 'cart' ) ) );
2012-01-12 00:54:45 +00:00
exit ;
2012-08-14 15:30:23 +00:00
}
2012-08-10 11:15:32 +00:00
2012-08-14 15:30:23 +00:00
if ( ! defined ( 'WOOCOMMERCE_CHECKOUT' ) )
define ( 'WOOCOMMERCE_CHECKOUT' , true );
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
$woocommerce_checkout = $woocommerce -> checkout ();
$woocommerce_checkout -> process_checkout ();
2012-08-14 15:30:23 +00:00
}
2012-01-12 00:54:45 +00:00
}
2012-08-14 15:30:23 +00:00
2012-01-12 00:54:45 +00:00
/**
2012-08-14 15:30:23 +00:00
* Process the pay form .
*
* @ access public
* @ return void
*/
2012-01-12 00:54:45 +00:00
function woocommerce_pay_action () {
global $woocommerce ;
2012-08-10 11:15:32 +00:00
2012-11-13 23:50:20 +00:00
if ( isset ( $_POST [ 'woocommerce_pay' ] ) && $woocommerce -> verify_nonce ( 'pay' ) ) {
2012-08-10 11:15:32 +00:00
2012-02-09 13:58:34 +00:00
ob_start ();
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Pay for existing order
2012-11-13 23:50:20 +00:00
$order_key = urldecode ( $_GET [ 'order' ] );
$order_id = absint ( $_GET [ 'order_id' ] );
$order = new WC_Order ( $order_id );
2012-08-10 11:15:32 +00:00
2012-11-13 23:50:20 +00:00
if ( $order -> id == $order_id && $order -> order_key == $order_key && in_array ( $order -> status , array ( 'pending' , 'failed' ) ) ) {
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Set customer location to order location
2012-11-27 16:22:47 +00:00
if ( $order -> billing_country )
2012-11-13 23:50:20 +00:00
$woocommerce -> customer -> set_country ( $order -> billing_country );
2012-11-27 16:22:47 +00:00
if ( $order -> billing_state )
2012-11-13 23:50:20 +00:00
$woocommerce -> customer -> set_state ( $order -> billing_state );
2012-11-27 16:22:47 +00:00
if ( $order -> billing_postcode )
2012-11-13 23:50:20 +00:00
$woocommerce -> customer -> set_postcode ( $order -> billing_postcode );
2012-11-27 16:22:47 +00:00
if ( $order -> billing_city )
2012-11-13 23:50:20 +00:00
$woocommerce -> customer -> set_city ( $order -> billing_city );
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Update payment method
2012-11-13 23:50:20 +00:00
if ( $order -> order_total > 0 ) {
$payment_method = woocommerce_clean ( $_POST [ 'payment_method' ] );
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
$available_gateways = $woocommerce -> payment_gateways -> get_available_payment_gateways ();
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Update meta
2012-11-13 23:50:20 +00:00
update_post_meta ( $order_id , '_payment_method' , $payment_method );
2012-11-27 16:22:47 +00:00
2012-11-13 23:50:20 +00:00
if ( isset ( $available_gateways [ $payment_method ] ) )
$payment_method_title = $available_gateways [ $payment_method ] -> get_title ();
2012-11-27 16:22:47 +00:00
2012-01-12 00:54:45 +00:00
update_post_meta ( $order_id , '_payment_method_title' , $payment_method_title );
2012-11-27 16:22:47 +00:00
2012-11-13 23:50:20 +00:00
// Validate
$available_gateways [ $payment_method ] -> validate_fields ();
2012-11-27 16:22:47 +00:00
2012-11-13 23:50:20 +00:00
// Process
if ( $woocommerce -> error_count () == 0 ) {
2012-11-27 16:22:47 +00:00
2012-11-13 23:50:20 +00:00
$result = $available_gateways [ $payment_method ] -> process_payment ( $order_id );
// Redirect to success/confirmation/payment page
if ( $result [ 'result' ] == 'success' ) {
wp_redirect ( $result [ 'redirect' ] );
exit ;
}
2012-11-27 16:22:47 +00:00
2012-11-13 23:50:20 +00:00
}
2012-11-27 16:22:47 +00:00
2012-11-13 23:50:20 +00:00
} else {
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// No payment was required for order
$order -> payment_complete ();
2012-11-13 23:50:20 +00:00
wp_safe_redirect ( get_permalink ( woocommerce_get_page_id ( 'thanks' ) ) );
2012-01-12 00:54:45 +00:00
exit ;
2012-08-10 11:15:32 +00:00
2012-11-13 23:50:20 +00:00
}
2012-08-10 11:15:32 +00:00
2012-11-13 23:50:20 +00:00
}
2012-08-10 11:15:32 +00:00
2012-11-13 23:50:20 +00:00
}
2012-01-12 00:54:45 +00:00
}
2012-08-14 15:30:23 +00:00
2011-08-10 17:11:11 +00:00
/**
2012-08-14 15:30:23 +00:00
* Process the login form .
*
* @ access public
* @ return void
*/
2011-08-10 17:11:11 +00:00
function woocommerce_process_login () {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2012-08-10 11:15:32 +00:00
2013-01-31 11:42:27 +00:00
if ( ! empty ( $_POST [ 'login' ] ) ) {
2012-08-10 11:15:32 +00:00
2013-01-31 11:42:27 +00:00
$woocommerce -> verify_nonce ( 'login' );
2011-08-10 17:11:11 +00:00
2013-05-01 14:24:28 +00:00
try {
2011-08-10 17:11:11 +00:00
$creds = array ();
2012-11-28 20:09:58 +00:00
2013-05-01 14:24:28 +00:00
if ( empty ( $_POST [ 'username' ] ) )
throw new Exception ( '<strong>' . __ ( 'Error' , 'woocommerce' ) . ':</strong> ' . __ ( 'Username is required.' , 'woocommerce' ) );
if ( empty ( $_POST [ 'password' ] ) )
throw new Exception ( '<strong>' . __ ( 'Error' , 'woocommerce' ) . ':</strong> ' . __ ( 'Password is required.' , 'woocommerce' ) );
2012-11-28 20:09:58 +00:00
if ( is_email ( $_POST [ 'username' ] ) ) {
$user = get_user_by ( 'email' , $_POST [ 'username' ] );
if ( isset ( $user -> user_login ) )
$creds [ 'user_login' ] = $user -> user_login ;
else
2013-05-01 14:24:28 +00:00
throw new Exception ( '<strong>' . __ ( 'Error' , 'woocommerce' ) . ':</strong> ' . __ ( 'A user could not be found with this email address.' , 'woocommerce' ) );
2012-11-28 20:09:58 +00:00
} else {
$creds [ 'user_login' ] = $_POST [ 'username' ];
}
2012-10-15 12:08:33 +00:00
$creds [ 'user_password' ] = $_POST [ 'password' ];
2013-01-31 11:42:27 +00:00
$creds [ 'remember' ] = true ;
$secure_cookie = is_ssl () ? true : false ;
$user = wp_signon ( $creds , $secure_cookie );
if ( is_wp_error ( $user ) ) {
2013-05-01 14:24:28 +00:00
throw new Exception ( $user -> get_error_message () );
2013-01-31 11:42:27 +00:00
} else {
2012-08-10 11:15:32 +00:00
2013-01-31 11:42:27 +00:00
if ( ! empty ( $_POST [ 'redirect' ] ) ) {
$redirect = esc_url ( $_POST [ 'redirect' ] );
} elseif ( wp_get_referer () ) {
$redirect = esc_url ( wp_get_referer () );
2012-12-04 21:37:52 +00:00
} else {
2013-01-31 11:42:27 +00:00
$redirect = esc_url ( get_permalink ( woocommerce_get_page_id ( 'myaccount' ) ) );
2012-12-04 21:37:52 +00:00
}
2012-08-10 11:15:32 +00:00
2013-01-31 11:42:27 +00:00
wp_redirect ( apply_filters ( 'woocommerce_login_redirect' , $redirect , $user ) );
2011-08-10 17:11:11 +00:00
exit ;
2013-01-31 11:42:27 +00:00
}
2013-05-01 14:24:28 +00:00
} catch ( Exception $e ) {
$woocommerce -> add_error ( $e -> getMessage () );
2013-01-31 11:42:27 +00:00
}
}
2011-12-19 17:10:53 +00:00
}
2012-08-14 15:30:23 +00:00
2011-11-14 16:46:11 +00:00
/**
2012-08-14 15:30:23 +00:00
* Process the registration form .
*
* @ access public
* @ return void
*/
2011-11-14 16:46:11 +00:00
function woocommerce_process_registration () {
2013-01-23 11:41:54 +00:00
global $woocommerce , $current_user ;
2012-08-10 11:15:32 +00:00
2012-12-28 18:45:06 +00:00
if ( ! empty ( $_POST [ 'register' ] ) ) {
2012-08-10 11:15:32 +00:00
2013-01-31 11:42:27 +00:00
$woocommerce -> verify_nonce ( 'register' );
2012-08-10 11:15:32 +00:00
2011-11-14 16:46:11 +00:00
// Get fields
2012-12-28 18:45:06 +00:00
$user_email = isset ( $_POST [ 'email' ] ) ? trim ( $_POST [ 'email' ] ) : '' ;
$password = isset ( $_POST [ 'password' ] ) ? trim ( $_POST [ 'password' ] ) : '' ;
$password2 = isset ( $_POST [ 'password2' ] ) ? trim ( $_POST [ 'password2' ] ) : '' ;
$user_email = apply_filters ( 'user_registration_email' , $user_email );
2012-08-10 11:15:32 +00:00
if ( get_option ( 'woocommerce_registration_email_for_username' ) == 'no' ) {
2012-10-15 12:08:33 +00:00
$username = isset ( $_POST [ 'username' ] ) ? trim ( $_POST [ 'username' ] ) : '' ;
2012-08-10 11:15:32 +00:00
$sanitized_user_login = sanitize_user ( $username );
// Check the username
if ( $sanitized_user_login == '' ) {
2012-10-16 09:45:33 +00:00
$woocommerce -> add_error ( '<strong>' . __ ( 'ERROR' , 'woocommerce' ) . '</strong>: ' . __ ( 'Please enter a username.' , 'woocommerce' ) );
2012-08-10 11:15:32 +00:00
} elseif ( ! validate_username ( $username ) ) {
2012-10-16 09:45:33 +00:00
$woocommerce -> add_error ( '<strong>' . __ ( 'ERROR' , 'woocommerce' ) . '</strong>: ' . __ ( 'This username is invalid because it uses illegal characters. Please enter a valid username.' , 'woocommerce' ) );
2012-08-10 11:15:32 +00:00
$sanitized_user_login = '' ;
} elseif ( username_exists ( $sanitized_user_login ) ) {
2012-10-16 09:45:33 +00:00
$woocommerce -> add_error ( '<strong>' . __ ( 'ERROR' , 'woocommerce' ) . '</strong>: ' . __ ( 'This username is already registered, please choose another one.' , 'woocommerce' ) );
2012-08-10 11:15:32 +00:00
}
} else {
$username = $user_email ;
$sanitized_user_login = sanitize_user ( $username );
2011-11-14 16:46:11 +00:00
}
2012-08-10 11:15:32 +00:00
2011-11-14 16:46:11 +00:00
// Check the e-mail address
if ( $user_email == '' ) {
2012-10-16 09:45:33 +00:00
$woocommerce -> add_error ( '<strong>' . __ ( 'ERROR' , 'woocommerce' ) . '</strong>: ' . __ ( 'Please type your e-mail address.' , 'woocommerce' ) );
2011-11-14 16:46:11 +00:00
} elseif ( ! is_email ( $user_email ) ) {
2012-10-16 09:45:33 +00:00
$woocommerce -> add_error ( '<strong>' . __ ( 'ERROR' , 'woocommerce' ) . '</strong>: ' . __ ( 'The email address isn’t correct.' , 'woocommerce' ) );
2011-11-14 16:46:11 +00:00
$user_email = '' ;
} elseif ( email_exists ( $user_email ) ) {
2012-10-16 09:45:33 +00:00
$woocommerce -> add_error ( '<strong>' . __ ( 'ERROR' , 'woocommerce' ) . '</strong>: ' . __ ( 'This email is already registered, please choose another one.' , 'woocommerce' ) );
2011-11-14 16:46:11 +00:00
}
2012-08-10 11:15:32 +00:00
2011-11-14 16:46:11 +00:00
// Password
2012-12-28 18:45:06 +00:00
if ( ! $password ) $woocommerce -> add_error ( __ ( 'Password is required.' , 'woocommerce' ) );
if ( ! $password2 ) $woocommerce -> add_error ( __ ( 'Re-enter your password.' , 'woocommerce' ) );
2012-10-16 09:45:33 +00:00
if ( $password != $password2 ) $woocommerce -> add_error ( __ ( 'Passwords do not match.' , 'woocommerce' ) );
2012-08-10 11:15:32 +00:00
2011-11-14 16:46:11 +00:00
// Spam trap
2012-12-28 18:45:06 +00:00
if ( ! empty ( $_POST [ 'email_2' ] ) )
$woocommerce -> add_error ( __ ( 'Anti-spam field was filled in.' , 'woocommerce' ) );
2012-08-10 11:15:32 +00:00
2013-01-21 10:41:32 +00:00
// More error checking
$reg_errors = new WP_Error ();
do_action ( 'register_post' , $sanitized_user_login , $user_email , $reg_errors );
$reg_errors = apply_filters ( 'registration_errors' , $reg_errors , $sanitized_user_login , $user_email );
if ( $reg_errors -> get_error_code () ) {
$woocommerce -> add_error ( $reg_errors -> get_error_message () );
return ;
}
2012-08-10 11:15:32 +00:00
2013-01-21 10:41:32 +00:00
if ( $woocommerce -> error_count () == 0 ) {
2012-12-28 18:45:06 +00:00
2013-01-21 10:41:32 +00:00
$new_customer_data = array (
'user_login' => $sanitized_user_login ,
'user_pass' => $password ,
'user_email' => $user_email ,
'role' => 'customer'
);
2011-11-14 16:46:11 +00:00
2013-01-21 10:41:32 +00:00
$user_id = wp_insert_user ( apply_filters ( 'woocommerce_new_customer_data' , $new_customer_data ) );
2012-08-10 11:15:32 +00:00
2013-02-03 18:41:47 +00:00
if ( is_wp_error ( $user_id ) ) {
2013-01-21 10:41:32 +00:00
$woocommerce -> add_error ( '<strong>' . __ ( 'ERROR' , 'woocommerce' ) . '</strong>: ' . __ ( 'Couldn’t register you… please contact us if you continue to have problems.' , 'woocommerce' ) );
return ;
}
2011-11-14 16:46:11 +00:00
2013-01-23 11:41:54 +00:00
// Get user
$current_user = get_user_by ( 'id' , $user_id );
2013-01-21 10:41:32 +00:00
// Action
do_action ( 'woocommerce_created_customer' , $user_id );
2012-08-10 11:15:32 +00:00
2013-01-21 10:41:32 +00:00
// send the user a confirmation and their login details
$mailer = $woocommerce -> mailer ();
$mailer -> customer_new_account ( $user_id , $password );
2011-11-14 16:46:11 +00:00
2013-01-21 10:41:32 +00:00
// set the WP login cookie
$secure_cookie = is_ssl () ? true : false ;
wp_set_auth_cookie ( $user_id , true , $secure_cookie );
2012-08-10 11:15:32 +00:00
2013-01-21 10:41:32 +00:00
// Redirect
2013-04-08 18:55:49 +00:00
if ( wp_get_referer () ) {
$redirect = esc_url ( wp_get_referer () );
2012-12-28 18:45:06 +00:00
} else {
2013-04-08 18:55:49 +00:00
$redirect = esc_url ( get_permalink ( woocommerce_get_page_id ( 'myaccount' ) ) );
2012-12-28 18:45:06 +00:00
}
2012-08-10 11:15:32 +00:00
2013-04-08 18:55:49 +00:00
wp_redirect ( apply_filters ( 'woocommerce_registration_redirect' , $redirect ) );
exit ;
2012-12-28 18:45:06 +00:00
}
2012-08-10 11:15:32 +00:00
2012-12-28 18:45:06 +00:00
}
2011-11-14 16:46:11 +00:00
}
2012-08-14 15:30:23 +00:00
2012-03-14 12:48:49 +00:00
/**
2012-08-14 15:30:23 +00:00
* Place a previous order again .
*
* @ access public
* @ return void
*/
2012-03-14 12:48:49 +00:00
function woocommerce_order_again () {
global $woocommerce ;
// Nothing to do
2012-03-15 00:25:51 +00:00
if ( ! isset ( $_GET [ 'order_again' ] ) || ! is_user_logged_in () || get_option ( 'woocommerce_allow_customers_to_reorder' ) == 'no' ) return ;
2012-03-14 12:48:49 +00:00
2012-03-14 14:00:09 +00:00
// Nonce security check
if ( ! $woocommerce -> verify_nonce ( 'order_again' , '_GET' ) ) return ;
2012-03-14 12:48:49 +00:00
2012-06-29 19:49:27 +00:00
// Clear current cart
$woocommerce -> cart -> empty_cart ();
2012-08-10 11:15:32 +00:00
2012-03-15 00:25:51 +00:00
// Load the previous order - Stop if the order does not exist
2012-03-14 12:48:49 +00:00
$order = new WC_Order ( ( int ) $_GET [ 'order_again' ] );
2012-08-10 11:15:32 +00:00
2012-03-14 12:48:49 +00:00
if ( empty ( $order -> id ) ) return ;
2012-08-10 11:15:32 +00:00
2012-03-15 12:15:22 +00:00
if ( $order -> status != 'completed' ) return ;
2012-03-14 12:48:49 +00:00
// Make sure the previous order belongs to the current customer
2012-03-15 00:25:51 +00:00
if ( $order -> user_id != get_current_user_id () ) return ;
2012-03-14 12:48:49 +00:00
2012-03-14 13:34:06 +00:00
// Copy products from the order to the cart
foreach ( $order -> get_items () as $item ) {
2012-03-14 14:28:58 +00:00
// Load all product info including variation data
2012-10-23 16:41:42 +00:00
$product_id = ( int ) apply_filters ( 'woocommerce_add_to_cart_product_id' , $item [ 'product_id' ] );
2012-03-14 14:28:58 +00:00
$quantity = ( int ) $item [ 'qty' ];
$variation_id = ( int ) $item [ 'variation_id' ];
2012-03-14 14:45:42 +00:00
$variations = array ();
2013-03-18 05:36:59 +00:00
$cart_item_data = apply_filters ( 'woocommerce_order_again_cart_item_data' , array (), $item , $order );
foreach ( $item [ 'item_meta' ] as $meta_name => $meta_value ) {
if ( 'pa_' === substr ( $meta_name , 0 , 3 ) )
$variations [ $meta_name ] = $meta_value [ 0 ];
2012-03-14 14:28:58 +00:00
}
// Add to cart validation
2013-03-18 05:36:59 +00:00
if ( ! apply_filters ( 'woocommerce_add_to_cart_validation' , true , $product_id , $quantity , $variation_id , $variations , $cart_item_data ) ) continue ;
2012-03-14 14:28:58 +00:00
2013-03-18 05:36:59 +00:00
$woocommerce -> cart -> add_to_cart ( $product_id , $quantity , $variation_id , $variations , $cart_item_data );
2012-03-14 12:48:49 +00:00
}
2012-08-10 11:15:32 +00:00
2012-08-05 16:42:25 +00:00
do_action ( 'woocommerce_ordered_again' , $order -> id );
2012-03-14 13:34:06 +00:00
// Redirect to cart
2012-10-16 09:45:33 +00:00
$woocommerce -> add_message ( __ ( 'The cart has been filled with the items from your previous order.' , 'woocommerce' ) );
2012-03-14 13:34:06 +00:00
wp_safe_redirect ( $woocommerce -> cart -> get_cart_url () );
exit ;
2012-03-14 12:48:49 +00:00
}
2012-08-14 15:30:23 +00:00
2011-08-10 17:11:11 +00:00
/**
2012-08-14 15:30:23 +00:00
* Cancel a pending order .
*
* @ access public
* @ return void
*/
2011-08-10 17:11:11 +00:00
function woocommerce_cancel_order () {
2012-08-10 11:15:32 +00:00
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2012-08-10 11:15:32 +00:00
2011-08-10 17:11:11 +00:00
if ( isset ( $_GET [ 'cancel_order' ]) && isset ( $_GET [ 'order' ]) && isset ( $_GET [ 'order_id' ]) ) :
2012-08-10 11:15:32 +00:00
2011-08-10 17:11:11 +00:00
$order_key = urldecode ( $_GET [ 'order' ] );
$order_id = ( int ) $_GET [ 'order_id' ];
2012-08-10 11:15:32 +00:00
2012-01-27 16:38:39 +00:00
$order = new WC_Order ( $order_id );
2011-08-10 17:11:11 +00:00
2012-12-06 15:39:59 +00:00
if ( $order -> id == $order_id && $order -> order_key == $order_key && in_array ( $order -> status , array ( 'pending' , 'failed' ) ) && $woocommerce -> verify_nonce ( 'cancel_order' , '_GET' ) ) :
2012-08-10 11:15:32 +00:00
2011-08-10 17:11:11 +00:00
// Cancel the order + restore stock
2012-10-16 09:45:33 +00:00
$order -> cancel_order ( __ ( 'Order cancelled by customer.' , 'woocommerce' ) );
2012-08-10 11:15:32 +00:00
2011-08-10 17:11:11 +00:00
// Message
2012-10-16 09:45:33 +00:00
$woocommerce -> add_message ( __ ( 'Your order was cancelled.' , 'woocommerce' ) );
2012-08-10 11:15:32 +00:00
2012-08-05 16:42:25 +00:00
do_action ( 'woocommerce_cancelled_order' , $order -> id );
2012-08-10 11:15:32 +00:00
2012-12-06 15:39:59 +00:00
elseif ( $order -> status != 'pending' ) :
2012-08-10 11:15:32 +00:00
2012-10-16 09:45:33 +00:00
$woocommerce -> add_error ( __ ( 'Your order is no longer pending and could not be cancelled. Please contact us if you need assistance.' , 'woocommerce' ) );
2012-08-10 11:15:32 +00:00
2011-08-10 17:11:11 +00:00
else :
2012-08-10 11:15:32 +00:00
2012-10-16 09:45:33 +00:00
$woocommerce -> add_error ( __ ( 'Invalid order.' , 'woocommerce' ) );
2012-08-10 11:15:32 +00:00
2011-08-10 17:11:11 +00:00
endif ;
2012-08-10 11:15:32 +00:00
2011-09-06 11:11:22 +00:00
wp_safe_redirect ( $woocommerce -> cart -> get_cart_url ());
2011-08-10 17:11:11 +00:00
exit ;
2012-08-10 11:15:32 +00:00
2011-08-10 17:11:11 +00:00
endif ;
}
2012-08-14 15:30:23 +00:00
2011-08-10 17:11:11 +00:00
/**
2012-08-14 15:30:23 +00:00
* Download a file - hook into init function .
*
* @ access public
* @ return void
*/
2011-08-10 17:11:11 +00:00
function woocommerce_download_product () {
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
if ( isset ( $_GET [ 'download_file' ] ) && isset ( $_GET [ 'order' ] ) && isset ( $_GET [ 'email' ] ) ) {
2012-08-10 11:15:32 +00:00
2013-04-03 20:17:50 +00:00
global $wpdb , $is_IE ;
2012-08-10 11:15:32 +00:00
2013-02-02 16:48:38 +00:00
$product_id = ( int ) urldecode ( $_GET [ 'download_file' ]);
$order_key = urldecode ( $_GET [ 'order' ] );
$email = sanitize_email ( str_replace ( ' ' , '+' , urldecode ( $_GET [ 'email' ] ) ) );
2013-04-29 15:36:02 +00:00
$download_id = isset ( $_GET [ 'key' ] ) ? preg_replace ( '/\s+/' , ' ' , urldecode ( $_GET [ 'key' ] ) ) : '' ;
2013-02-02 16:48:38 +00:00
$_product = get_product ( $product_id );
$file_download_method = apply_filters ( 'woocommerce_file_download_method' , get_option ( 'woocommerce_file_download_method' ), $product_id );
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
if ( ! is_email ( $email ) )
wp_die ( __ ( 'Invalid email address.' , 'woocommerce' ) . ' <a href="' . home_url () . '">' . __ ( 'Go to homepage →' , 'woocommerce' ) . '</a>' );
2012-08-10 11:15:32 +00:00
2012-08-28 15:21:54 +00:00
$query = "
SELECT order_id , downloads_remaining , user_id , download_count , access_expires , download_id
FROM " . $wpdb->prefix . " woocommerce_downloadable_product_permissions
2011-10-05 16:22:38 +00:00
WHERE user_email = % s
AND order_key = % s
2012-08-28 15:21:54 +00:00
AND product_id = % s " ;
$args = array (
$email ,
$order_key ,
$product_id
);
2013-04-29 15:36:02 +00:00
2012-08-28 15:21:54 +00:00
if ( $download_id ) {
// backwards compatibility for existing download URLs
$query .= " AND download_id = %s " ;
$args [] = $download_id ;
}
$download_result = $wpdb -> get_row ( $wpdb -> prepare ( $query , $args ) );
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
if ( ! $download_result )
2012-10-16 09:45:33 +00:00
wp_die ( __ ( 'Invalid download.' , 'woocommerce' ) . ' <a href="' . home_url () . '">' . __ ( 'Go to homepage →' , 'woocommerce' ) . '</a>' );
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
$download_id = $download_result -> download_id ;
$order_id = $download_result -> order_id ;
$downloads_remaining = $download_result -> downloads_remaining ;
$download_count = $download_result -> download_count ;
$user_id = $download_result -> user_id ;
$access_expires = $download_result -> access_expires ;
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
if ( $user_id && get_option ( 'woocommerce_downloads_require_login' ) == 'yes' ) {
2012-11-27 16:22:47 +00:00
2012-09-06 15:16:16 +00:00
if ( ! is_user_logged_in () )
wp_die ( __ ( 'You must be logged in to download files.' , 'woocommerce' ) . ' <a href="' . wp_login_url ( get_permalink ( woocommerce_get_page_id ( 'myaccount' ) ) ) . '">' . __ ( 'Login →' , 'woocommerce' ) . '</a>' );
2012-11-27 16:22:47 +00:00
2012-09-06 15:16:16 +00:00
elseif ( $user_id != get_current_user_id () )
wp_die ( __ ( 'This is not your download link.' , 'woocommerce' ) );
2012-11-27 16:22:47 +00:00
2012-08-28 15:21:54 +00:00
}
2012-09-06 15:16:16 +00:00
if ( ! get_post ( $product_id ) )
wp_die ( __ ( 'Product no longer exists.' , 'woocommerce' ) . ' <a href="' . home_url () . '">' . __ ( 'Go to homepage →' , 'woocommerce' ) . '</a>' );
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
if ( $order_id ) {
$order = new WC_Order ( $order_id );
2012-11-27 16:22:47 +00:00
2012-12-06 15:39:59 +00:00
if ( ! $order -> is_download_permitted () || $order -> post_status != 'publish' )
2012-09-06 15:16:16 +00:00
wp_die ( __ ( 'Invalid order.' , 'woocommerce' ) . ' <a href="' . home_url () . '">' . __ ( 'Go to homepage →' , 'woocommerce' ) . '</a>' );
}
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
if ( $downloads_remaining == '0' )
2012-10-16 09:45:33 +00:00
wp_die ( __ ( 'Sorry, you have reached your download limit for this file' , 'woocommerce' ) . ' <a href="' . home_url () . '">' . __ ( 'Go to homepage →' , 'woocommerce' ) . '</a>' );
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
if ( $access_expires > 0 && strtotime ( $access_expires ) < current_time ( 'timestamp' ) )
wp_die ( __ ( 'Sorry, this download has expired' , 'woocommerce' ) . ' <a href="' . home_url () . '">' . __ ( 'Go to homepage →' , 'woocommerce' ) . '</a>' );
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
if ( $downloads_remaining > 0 ) {
2012-08-10 11:15:32 +00:00
$wpdb -> update ( $wpdb -> prefix . " woocommerce_downloadable_product_permissions " , array (
'downloads_remaining' => $downloads_remaining - 1 ,
), array (
2012-09-06 15:16:16 +00:00
'user_email' => $email ,
'order_key' => $order_key ,
'product_id' => $product_id ,
2012-11-27 16:22:47 +00:00
'download_id' => $download_id
2012-08-28 15:21:54 +00:00
), array ( '%d' ), array ( '%s' , '%s' , '%d' , '%s' ) );
2012-09-06 15:16:16 +00:00
}
2012-08-10 11:15:32 +00:00
2012-02-25 21:11:06 +00:00
// Count the download
2012-08-10 11:15:32 +00:00
$wpdb -> update ( $wpdb -> prefix . " woocommerce_downloadable_product_permissions " , array (
'download_count' => $download_count + 1 ,
), array (
2012-09-06 15:16:16 +00:00
'user_email' => $email ,
'order_key' => $order_key ,
'product_id' => $product_id ,
'download_id' => $download_id
2012-08-28 15:21:54 +00:00
), array ( '%d' ), array ( '%s' , '%s' , '%d' , '%s' ) );
2012-08-10 11:15:32 +00:00
2013-01-21 10:45:41 +00:00
// Trigger action
2013-03-13 03:13:09 +00:00
do_action ( 'woocommerce_download_product' , $email , $order_key , $product_id , $user_id , $download_id , $order_id );
2013-01-21 10:45:41 +00:00
2012-08-28 15:21:54 +00:00
// Get the download URL and try to replace the url with a path
$file_path = $_product -> get_file_download_path ( $download_id );
2012-08-10 11:15:32 +00:00
2013-01-21 10:45:41 +00:00
if ( ! $file_path )
2013-02-02 16:48:38 +00:00
wp_die ( __ ( 'No file defined' , 'woocommerce' ) . ' <a href="' . home_url () . '">' . __ ( 'Go to homepage →' , 'woocommerce' ) . '</a>' );
2012-11-27 16:22:47 +00:00
2013-02-02 16:48:38 +00:00
// Redirect to the file...
if ( $file_download_method == " redirect " ) {
2012-09-06 15:16:16 +00:00
header ( 'Location: ' . $file_path );
2012-02-25 21:11:06 +00:00
exit ;
2012-09-06 15:16:16 +00:00
}
2012-08-10 11:15:32 +00:00
2013-02-02 16:48:38 +00:00
// ...or serve it
2012-09-06 15:16:16 +00:00
if ( ! is_multisite () ) {
2013-02-02 16:48:38 +00:00
2013-03-18 10:42:09 +00:00
/*
2013-04-03 20:17:50 +00:00
* Download file may be either http or https .
* site_url () depends on whether the page containing the download ( ie ; My Account ) is served via SSL because WC
* modifies site_url () via a filter to force_ssl .
* So blindly doing a str_replace is incorrect because it will fail when schemes are mismatched . This code
* handles the various permutations .
2013-03-18 01:29:32 +00:00
*/
$scheme = parse_url ( $file_path , PHP_URL_SCHEME );
2013-04-08 17:43:07 +00:00
2013-03-18 01:29:32 +00:00
if ( $scheme ) {
2013-04-03 20:17:50 +00:00
$site_url = set_url_scheme ( site_url ( '' ), $scheme );
2013-03-18 01:29:32 +00:00
} else {
$site_url = is_ssl () ? str_replace ( 'https:' , 'http:' , site_url () ) : site_url ();
}
2013-03-18 10:42:09 +00:00
2013-02-02 16:48:38 +00:00
$file_path = str_replace ( trailingslashit ( $site_url ), ABSPATH , $file_path );
2012-09-06 15:16:16 +00:00
} else {
2013-02-02 16:48:38 +00:00
$network_url = is_ssl () ? str_replace ( 'https:' , 'http:' , network_admin_url () ) : network_admin_url ();
$upload_dir = wp_upload_dir ();
2012-08-10 11:15:32 +00:00
2012-02-25 21:11:06 +00:00
// Try to replace network url
2013-02-02 16:48:38 +00:00
$file_path = str_replace ( trailingslashit ( $network_url ), ABSPATH , $file_path );
2012-08-10 11:15:32 +00:00
2012-02-25 21:11:06 +00:00
// Now try to replace upload URL
2013-02-02 16:48:38 +00:00
$file_path = str_replace ( $upload_dir [ 'baseurl' ], $upload_dir [ 'basedir' ], $file_path );
2012-09-06 15:16:16 +00:00
}
2012-08-10 11:15:32 +00:00
2012-02-25 21:11:06 +00:00
// See if its local or remote
2012-09-06 15:16:16 +00:00
if ( strstr ( $file_path , 'http:' ) || strstr ( $file_path , 'https:' ) || strstr ( $file_path , 'ftp:' ) ) {
2012-02-25 21:11:06 +00:00
$remote_file = true ;
2012-09-06 15:16:16 +00:00
} else {
2012-02-25 21:11:06 +00:00
$remote_file = false ;
2013-03-18 14:41:44 +00:00
// Remove Query String
if ( strstr ( $file_path , '?' ) )
$file_path = current ( explode ( '?' , $file_path ) );
2013-02-02 16:48:38 +00:00
$file_path = realpath ( $file_path );
2012-09-06 15:16:16 +00:00
}
2012-08-10 11:15:32 +00:00
2013-02-02 16:48:38 +00:00
$file_extension = strtolower ( substr ( strrchr ( $file_path , " . " ), 1 ) );
$ctype = " application/force-download " ;
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
foreach ( get_allowed_mime_types () as $mime => $type ) {
$mimes = explode ( '|' , $mime );
if ( in_array ( $file_extension , $mimes ) ) {
2012-02-25 21:11:06 +00:00
$ctype = $type ;
break ;
2012-09-06 15:16:16 +00:00
}
}
2012-08-10 11:15:32 +00:00
2013-02-02 16:48:38 +00:00
// Start setting headers
if ( ! ini_get ( 'safe_mode' ) )
@ set_time_limit ( 0 );
if ( function_exists ( 'get_magic_quotes_runtime' ) && get_magic_quotes_runtime () )
@ set_magic_quotes_runtime ( 0 );
if ( function_exists ( 'apache_setenv' ) )
@ apache_setenv ( 'no-gzip' , 1 );
@ session_write_close ();
@ ini_set ( 'zlib.output_compression' , 'Off' );
@ ob_end_clean ();
if ( ob_get_level () )
@ ob_end_clean (); // Zip corruption fix
2013-04-04 02:58:38 +00:00
if ( $is_IE && is_ssl () ) {
// IE bug prevents download via SSL when Cache Control and Pragma no-cache headers set.
header ( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
header ( 'Cache-Control: private' );
2013-04-08 17:43:07 +00:00
} else {
2013-04-04 02:58:38 +00:00
nocache_headers ();
2013-04-03 20:17:50 +00:00
}
2013-02-02 16:48:38 +00:00
2013-03-18 14:41:44 +00:00
$file_name = basename ( $file_path );
if ( strstr ( $file_name , '?' ) )
$file_name = current ( explode ( '?' , $file_name ) );
2013-02-02 16:48:38 +00:00
header ( " Robots: none " );
header ( " Content-Type: " . $ctype );
header ( " Content-Description: File Transfer " );
2013-03-18 14:41:44 +00:00
header ( " Content-Disposition: attachment; filename= \" " . $file_name . " \" ; " );
2013-02-02 16:48:38 +00:00
header ( " Content-Transfer-Encoding: binary " );
if ( $size = @ filesize ( $file_path ) )
header ( " Content-Length: " . $size );
2012-09-06 15:16:16 +00:00
if ( $file_download_method == 'xsendfile' ) {
2012-11-27 16:22:47 +00:00
2012-09-06 15:16:16 +00:00
// Path fix - kudos to Jason Judge
if ( getcwd () )
$file_path = trim ( preg_replace ( '`^' . getcwd () . '`' , '' , $file_path ), '/' );
2012-08-10 11:15:32 +00:00
2013-03-18 14:41:44 +00:00
header ( " Content-Disposition: attachment; filename= \" " . $file_name . " \" ; " );
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
if ( function_exists ( 'apache_get_modules' ) && in_array ( 'mod_xsendfile' , apache_get_modules () ) ) {
2012-08-10 11:15:32 +00:00
2012-02-25 21:11:06 +00:00
header ( " X-Sendfile: $file_path " );
exit ;
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
} elseif ( stristr ( getenv ( 'SERVER_SOFTWARE' ), 'lighttpd' ) ) {
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
header ( " X-Lighttpd-Sendfile: $file_path " );
2012-02-25 21:11:06 +00:00
exit ;
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
} elseif ( stristr ( getenv ( 'SERVER_SOFTWARE' ), 'nginx' ) || stristr ( getenv ( 'SERVER_SOFTWARE' ), 'cherokee' ) ) {
2012-08-10 11:15:32 +00:00
2013-02-20 09:29:32 +00:00
header ( " X-Accel-Redirect: / $file_path " );
2012-02-25 21:11:06 +00:00
exit ;
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
}
}
2012-02-25 21:11:06 +00:00
2013-02-02 16:48:38 +00:00
if ( $remote_file )
@ woocommerce_readfile_chunked ( $file_path ) or header ( 'Location: ' . $file_path );
else
@ woocommerce_readfile_chunked ( $file_path ) or wp_die ( __ ( 'File not found' , 'woocommerce' ) . ' <a href="' . home_url () . '">' . __ ( 'Go to homepage →' , 'woocommerce' ) . '</a>' );
2012-02-25 21:11:06 +00:00
2013-02-02 16:48:38 +00:00
exit ;
}
}
2012-08-10 11:15:32 +00:00
2013-02-02 16:48:38 +00:00
/**
* readfile_chunked
*
* Reads file in chunks so big downloads are possible without changing PHP . INI - http :// codeigniter . com / wiki / Download_helper_for_large_files /
*
* @ access public
* @ param string file
* @ param boolean return bytes of file
* @ return void
*/
function woocommerce_readfile_chunked ( $file , $retbytes = true ) {
2012-02-25 21:11:06 +00:00
2013-02-02 16:48:38 +00:00
$chunksize = 1 * ( 1024 * 1024 );
$buffer = '' ;
$cnt = 0 ;
2012-08-10 11:15:32 +00:00
2013-02-02 16:48:38 +00:00
$handle = fopen ( $file , 'r' );
if ( $handle === FALSE )
return FALSE ;
2012-08-10 11:15:32 +00:00
2013-02-02 16:48:38 +00:00
while ( ! feof ( $handle ) ) {
$buffer = fread ( $handle , $chunksize );
echo $buffer ;
ob_flush ();
flush ();
2012-08-10 11:15:32 +00:00
2013-02-02 16:48:38 +00:00
if ( $retbytes )
$cnt += strlen ( $buffer );
}
2012-08-10 11:15:32 +00:00
2013-02-02 16:48:38 +00:00
$status = fclose ( $handle );
2012-08-10 11:15:32 +00:00
2013-02-02 16:48:38 +00:00
if ( $retbytes && $status )
return $cnt ;
2012-08-10 11:15:32 +00:00
2013-02-02 16:48:38 +00:00
return $status ;
2011-08-10 17:11:11 +00:00
}
2012-08-14 15:30:23 +00:00
/**
* Products RSS Feed .
*
* @ access public
* @ return void
*/
2011-10-31 01:03:51 +00:00
function woocommerce_products_rss_feed () {
// Product RSS
2012-10-16 13:35:17 +00:00
if ( is_post_type_archive ( 'product' ) || is_singular ( 'product' ) ) {
2012-08-10 11:15:32 +00:00
2011-10-31 01:03:51 +00:00
$feed = get_post_type_archive_feed_link ( 'product' );
2012-10-16 13:35:17 +00:00
echo '<link rel="alternate" type="application/rss+xml" title="' . __ ( 'New products' , 'woocommerce' ) . '" href="' . esc_attr ( $feed ) . '" />' ;
2012-08-10 11:15:32 +00:00
2012-10-16 13:35:17 +00:00
} elseif ( is_tax ( 'product_cat' ) ) {
2012-08-10 11:15:32 +00:00
2012-10-16 13:35:17 +00:00
$term = get_term_by ( 'slug' , esc_attr ( get_query_var ( 'product_cat' ) ), 'product_cat' );
2012-08-10 11:15:32 +00:00
2011-10-31 01:03:51 +00:00
$feed = add_query_arg ( 'product_cat' , $term -> slug , get_post_type_archive_feed_link ( 'product' ));
2012-08-10 11:15:32 +00:00
2012-10-16 13:35:17 +00:00
echo '<link rel="alternate" type="application/rss+xml" title="' . sprintf ( __ ( 'New products added to %s' , 'woocommerce' ), urlencode ( $term -> name )) . '" href="' . esc_attr ( $feed ) . '" />' ;
2012-08-10 11:15:32 +00:00
2012-10-16 13:35:17 +00:00
} elseif ( is_tax ( 'product_tag' ) ) {
2012-08-10 11:15:32 +00:00
2012-10-16 13:35:17 +00:00
$term = get_term_by ( 'slug' , esc_attr ( get_query_var ( 'product_tag' ) ), 'product_tag' );
2012-08-10 11:15:32 +00:00
2011-10-31 01:03:51 +00:00
$feed = add_query_arg ( 'product_tag' , $term -> slug , get_post_type_archive_feed_link ( 'product' ));
2012-08-10 11:15:32 +00:00
2012-10-16 13:35:17 +00:00
echo '<link rel="alternate" type="application/rss+xml" title="' . sprintf ( __ ( 'New products tagged %s' , 'woocommerce' ), urlencode ( $term -> name )) . '" href="' . esc_attr ( $feed ) . '" />' ;
2012-08-10 11:15:32 +00:00
2012-10-16 13:35:17 +00:00
}
2011-12-09 19:55:09 +00:00
}
2012-08-14 15:30:23 +00:00
2011-12-09 19:55:09 +00:00
/**
2012-08-14 15:30:23 +00:00
* Rating field for comments .
*
* @ access public
* @ param mixed $comment_id
* @ return void
*/
2013-04-23 11:53:42 +00:00
function woocommerce_add_comment_rating ( $comment_id ) {
2013-01-12 10:53:24 +00:00
if ( isset ( $_POST [ 'rating' ] ) ) {
if ( ! $_POST [ 'rating' ] || $_POST [ 'rating' ] > 5 || $_POST [ 'rating' ] < 0 )
return ;
add_comment_meta ( $comment_id , 'rating' , ( int ) esc_attr ( $_POST [ 'rating' ] ), true );
2013-04-23 11:53:42 +00:00
woocommerce_clear_comment_rating_transients ( $comment_id );
2013-01-12 10:53:24 +00:00
}
2011-12-09 19:55:09 +00:00
}
2012-08-14 15:30:23 +00:00
/**
* Validate the comment ratings .
*
* @ access public
* @ param array $comment_data
* @ return array
*/
2013-04-23 11:53:42 +00:00
function woocommerce_check_comment_rating ( $comment_data ) {
2011-12-09 19:55:09 +00:00
global $woocommerce ;
2012-08-10 11:15:32 +00:00
2011-12-09 19:55:09 +00:00
// If posting a comment (not trackback etc) and not logged in
2012-03-27 19:35:35 +00:00
if ( isset ( $_POST [ 'rating' ] ) && ! $woocommerce -> verify_nonce ( 'comment_rating' ) )
2012-10-16 09:45:33 +00:00
wp_die ( __ ( 'You have taken too long. Please go back and refresh the page.' , 'woocommerce' ) );
2012-08-10 11:15:32 +00:00
2012-03-27 19:35:35 +00:00
elseif ( isset ( $_POST [ 'rating' ] ) && empty ( $_POST [ 'rating' ] ) && $comment_data [ 'comment_type' ] == '' && get_option ( 'woocommerce_review_rating_required' ) == 'yes' ) {
2012-10-16 09:45:33 +00:00
wp_die ( __ ( 'Please rate the product.' , 'woocommerce' ) );
2011-12-09 19:55:09 +00:00
exit ;
}
return $comment_data ;
2011-12-11 13:59:36 +00:00
}
2012-08-27 04:04:07 +00:00
/**
2012-11-27 16:22:47 +00:00
* Finds an Order ID based on an order key .
2012-08-27 04:04:07 +00:00
*
* @ access public
2012-11-27 16:22:47 +00:00
* @ param string $order_key An order key has generated by
2012-08-27 04:04:07 +00:00
* @ return int The ID of an order , or 0 if the order could not be found
*/
function woocommerce_get_order_id_by_order_key ( $order_key ) {
global $wpdb ;
// Faster than get_posts()
$order_id = $wpdb -> get_var ( " SELECT post_id FROM { $wpdb -> prefix } postmeta WHERE meta_key = '_order_key' AND meta_value = ' { $order_key } ' " );
return $order_id ;
2012-12-31 18:25:09 +00:00
}
/**
* Track product views
*
* @ access public
* @ package WooCommerce / Widgets
* @ return void
*/
function woocommerce_track_product_view () {
2013-01-14 17:17:57 +00:00
if ( ! is_singular ( 'product' ) )
return ;
2013-01-12 13:03:38 +00:00
global $post , $product ;
2012-12-31 18:25:09 +00:00
2013-01-12 13:03:38 +00:00
if ( empty ( $_COOKIE [ 'woocommerce_recently_viewed' ] ) )
2012-12-31 18:25:09 +00:00
$viewed_products = array ();
2013-01-12 13:03:38 +00:00
else
$viewed_products = ( array ) explode ( '|' , $_COOKIE [ 'woocommerce_recently_viewed' ] );
2012-12-31 18:25:09 +00:00
if ( ! in_array ( $post -> ID , $viewed_products ) )
$viewed_products [] = $post -> ID ;
if ( sizeof ( $viewed_products ) > 15 )
array_shift ( $viewed_products );
2013-01-12 13:03:38 +00:00
// Store for session only
setcookie ( " woocommerce_recently_viewed " , implode ( '|' , $viewed_products ), 0 , COOKIEPATH , COOKIE_DOMAIN , false , true );
2012-12-31 18:25:09 +00:00
}
2013-03-18 10:42:09 +00:00
add_action ( 'template_redirect' , 'woocommerce_track_product_view' , 20 );
2012-12-31 18:25:09 +00:00
/**
* Layered Nav Init
*
* @ package WooCommerce / Widgets
* @ access public
* @ return void
*/
function woocommerce_layered_nav_init ( ) {
if ( is_active_widget ( false , false , 'woocommerce_layered_nav' , true ) && ! is_admin () ) {
global $_chosen_attributes , $woocommerce , $_attributes_array ;
$_chosen_attributes = $_attributes_array = array ();
$attribute_taxonomies = $woocommerce -> get_attribute_taxonomies ();
if ( $attribute_taxonomies ) {
foreach ( $attribute_taxonomies as $tax ) {
$attribute = sanitize_title ( $tax -> attribute_name );
$taxonomy = $woocommerce -> attribute_taxonomy_name ( $attribute );
// create an array of product attribute taxonomies
$_attributes_array [] = $taxonomy ;
$name = 'filter_' . $attribute ;
$query_type_name = 'query_type_' . $attribute ;
if ( ! empty ( $_GET [ $name ] ) && taxonomy_exists ( $taxonomy ) ) {
$_chosen_attributes [ $taxonomy ][ 'terms' ] = explode ( ',' , $_GET [ $name ] );
2013-04-22 23:35:00 +00:00
if ( empty ( $_GET [ $query_type_name ] ) || ! in_array ( strtolower ( $_GET [ $query_type_name ] ), array ( 'and' , 'or' ) ) )
2013-04-24 12:42:40 +00:00
$_chosen_attributes [ $taxonomy ][ 'query_type' ] = apply_filters ( 'woocommerce_layered_nav_default_query_type' , 'and' );
else
$_chosen_attributes [ $taxonomy ][ 'query_type' ] = strtolower ( $_GET [ $query_type_name ] );
2012-12-31 18:25:09 +00:00
}
}
}
add_filter ( 'loop_shop_post_in' , 'woocommerce_layered_nav_query' );
}
}
add_action ( 'init' , 'woocommerce_layered_nav_init' , 1 );
/**
* Layered Nav post filter
*
* @ package WooCommerce / Widgets
* @ access public
* @ param array $filtered_posts
* @ return array
*/
function woocommerce_layered_nav_query ( $filtered_posts ) {
global $_chosen_attributes , $woocommerce , $wp_query ;
if ( sizeof ( $_chosen_attributes ) > 0 ) {
$matched_products = array ();
$filtered_attribute = false ;
foreach ( $_chosen_attributes as $attribute => $data ) {
$matched_products_from_attribute = array ();
$filtered = false ;
if ( sizeof ( $data [ 'terms' ] ) > 0 ) {
foreach ( $data [ 'terms' ] as $value ) {
$posts = get_posts (
array (
'post_type' => 'product' ,
'numberposts' => - 1 ,
'post_status' => 'publish' ,
'fields' => 'ids' ,
'no_found_rows' => true ,
'tax_query' => array (
array (
'taxonomy' => $attribute ,
'terms' => $value ,
'field' => 'id'
)
)
)
);
// AND or OR
if ( $data [ 'query_type' ] == 'or' ) {
if ( ! is_wp_error ( $posts ) && ( sizeof ( $matched_products_from_attribute ) > 0 || $filtered ) )
$matched_products_from_attribute = array_merge ( $posts , $matched_products_from_attribute );
elseif ( ! is_wp_error ( $posts ) )
$matched_products_from_attribute = $posts ;
} else {
if ( ! is_wp_error ( $posts ) && ( sizeof ( $matched_products_from_attribute ) > 0 || $filtered ) )
$matched_products_from_attribute = array_intersect ( $posts , $matched_products_from_attribute );
elseif ( ! is_wp_error ( $posts ) )
$matched_products_from_attribute = $posts ;
}
$filtered = true ;
}
}
if ( sizeof ( $matched_products ) > 0 || $filtered_attribute )
$matched_products = array_intersect ( $matched_products_from_attribute , $matched_products );
else
$matched_products = $matched_products_from_attribute ;
$filtered_attribute = true ;
}
if ( $filtered ) {
$woocommerce -> query -> layered_nav_post__in = $matched_products ;
$woocommerce -> query -> layered_nav_post__in [] = 0 ;
if ( sizeof ( $filtered_posts ) == 0 ) {
$filtered_posts = $matched_products ;
$filtered_posts [] = 0 ;
} else {
$filtered_posts = array_intersect ( $filtered_posts , $matched_products );
$filtered_posts [] = 0 ;
}
}
}
return ( array ) $filtered_posts ;
}
/**
* Price filter Init
*
* @ package WooCommerce / Widgets
* @ access public
* @ return void
*/
function woocommerce_price_filter_init () {
global $woocommerce ;
2013-05-24 15:51:58 +00:00
if ( is_active_widget ( false , false , 'woocommerce_price_filter' , true ) && ! is_admin () ) {
2012-12-31 18:25:09 +00:00
$suffix = defined ( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min' ;
wp_register_script ( 'wc-price-slider' , $woocommerce -> plugin_url () . '/assets/js/frontend/price-slider' . $suffix . '.js' , array ( 'jquery-ui-slider' ), '1.6' , true );
add_filter ( 'loop_shop_post_in' , 'woocommerce_price_filter' );
}
}
add_action ( 'init' , 'woocommerce_price_filter_init' );
/**
* Price Filter post filter
*
* @ package WooCommerce / Widgets
* @ access public
* @ param array $filtered_posts
* @ return array
*/
function woocommerce_price_filter ( $filtered_posts ) {
global $wpdb ;
if ( isset ( $_GET [ 'max_price' ] ) && isset ( $_GET [ 'min_price' ] ) ) {
$matched_products = array ();
$min = floatval ( $_GET [ 'min_price' ] );
$max = floatval ( $_GET [ 'max_price' ] );
$matched_products_query = $wpdb -> get_results ( $wpdb -> prepare ( "
SELECT DISTINCT ID , post_parent , post_type FROM $wpdb -> posts
INNER JOIN $wpdb -> postmeta ON ID = post_id
WHERE post_type IN ( 'product' , 'product_variation' ) AND post_status = 'publish' AND meta_key = % s AND meta_value BETWEEN % d AND % d
" , '_price', $min , $max ), OBJECT_K );
if ( $matched_products_query ) {
foreach ( $matched_products_query as $product ) {
if ( $product -> post_type == 'product' )
$matched_products [] = $product -> ID ;
if ( $product -> post_parent > 0 && ! in_array ( $product -> post_parent , $matched_products ) )
$matched_products [] = $product -> post_parent ;
}
}
// Filter the id's
if ( sizeof ( $filtered_posts ) == 0 ) {
$filtered_posts = $matched_products ;
$filtered_posts [] = 0 ;
} else {
$filtered_posts = array_intersect ( $filtered_posts , $matched_products );
$filtered_posts [] = 0 ;
}
}
return ( array ) $filtered_posts ;
}
/**
* Save the password and redirect back to the my account page .
*
* @ access public
*/
function woocommerce_save_password () {
global $woocommerce ;
if ( 'POST' !== strtoupper ( $_SERVER [ 'REQUEST_METHOD' ] ) )
return ;
if ( empty ( $_POST [ 'action' ] ) || ( 'change_password' !== $_POST [ 'action' ] ) )
return ;
$woocommerce -> verify_nonce ( 'change_password' );
2013-02-14 09:42:31 +00:00
$update = true ;
$errors = new WP_Error ();
$user = new stdClass ();
2012-12-31 18:25:09 +00:00
2013-02-14 09:42:31 +00:00
$user -> ID = ( int ) get_current_user_id ();
if ( $user -> ID <= 0 )
2012-12-31 18:25:09 +00:00
return ;
$_POST = array_map ( 'woocommerce_clean' , $_POST );
2013-02-14 09:42:31 +00:00
$pass1 = ! empty ( $_POST [ 'password_1' ] ) ? $_POST [ 'password_1' ] : '' ;
2013-02-25 11:21:37 +00:00
$pass2 = ! empty ( $_POST [ 'password_2' ] ) ? $_POST [ 'password_2' ] : '' ;
2013-02-14 09:42:31 +00:00
$user -> user_pass = $pass1 ;
if ( empty ( $pass1 ) || empty ( $pass2 ) )
2012-12-31 18:25:09 +00:00
$woocommerce -> add_error ( __ ( 'Please enter your password.' , 'woocommerce' ) );
2013-02-14 09:42:31 +00:00
if ( $pass1 !== $pass2 )
2012-12-31 18:25:09 +00:00
$woocommerce -> add_error ( __ ( 'Passwords do not match.' , 'woocommerce' ) );
2013-02-14 09:42:31 +00:00
// Allow plugins to return their own errors.
do_action_ref_array ( 'user_profile_update_errors' , array ( & $errors , $update , & $user ) );
if ( $errors -> get_error_messages () )
foreach ( $errors -> get_error_messages () as $error )
$woocommerce -> add_error ( $error );
2012-12-31 18:25:09 +00:00
if ( $woocommerce -> error_count () == 0 ) {
2013-02-14 09:44:17 +00:00
wp_update_user ( $user ) ;
2012-12-31 18:25:09 +00:00
2013-01-10 08:22:46 +00:00
$woocommerce -> add_message ( __ ( 'Password changed successfully.' , 'woocommerce' ) );
2013-02-14 09:44:17 +00:00
do_action ( 'woocommerce_customer_change_password' , $user -> ID );
2012-12-31 18:25:09 +00:00
wp_safe_redirect ( get_permalink ( woocommerce_get_page_id ( 'myaccount' ) ) );
exit ;
}
}
add_action ( 'template_redirect' , 'woocommerce_save_password' );
/**
* Save and and update a billing or shipping address if the
* form was submitted through the user account page .
*
* @ access public
*/
function woocommerce_save_address () {
global $woocommerce ;
if ( 'POST' !== strtoupper ( $_SERVER [ 'REQUEST_METHOD' ] ) )
return ;
if ( empty ( $_POST [ 'action' ] ) || ( 'edit_address' !== $_POST [ 'action' ] ) )
return ;
$woocommerce -> verify_nonce ( 'edit_address' );
$validation = $woocommerce -> validation ();
$user_id = get_current_user_id ();
if ( $user_id <= 0 ) return ;
2013-01-07 13:23:40 +00:00
$load_address = ( isset ( $_GET [ 'address' ] ) ) ? esc_attr ( $_GET [ 'address' ] ) : '' ;
$load_address = ( $load_address == 'billing' || $load_address == 'shipping' ) ? $load_address : '' ;
2012-12-31 18:25:09 +00:00
$address = $woocommerce -> countries -> get_address_fields ( esc_attr ( $_POST [ $load_address . '_country' ]), $load_address . '_' );
foreach ( $address as $key => $field ) :
if ( ! isset ( $field [ 'type' ])) $field [ 'type' ] = 'text' ;
// Get Value
switch ( $field [ 'type' ]) :
case " checkbox " :
$_POST [ $key ] = isset ( $_POST [ $key ]) ? 1 : 0 ;
break ;
default :
$_POST [ $key ] = isset ( $_POST [ $key ]) ? woocommerce_clean ( $_POST [ $key ]) : '' ;
break ;
endswitch ;
// Hook to allow modification of value
$_POST [ $key ] = apply_filters ( 'woocommerce_process_myaccount_field_' . $key , $_POST [ $key ]);
// Validation: Required fields
if ( isset ( $field [ 'required' ]) && $field [ 'required' ] && empty ( $_POST [ $key ]) ) $woocommerce -> add_error ( $field [ 'label' ] . ' ' . __ ( 'is a required field.' , 'woocommerce' ) );
// Postcode
if ( $key == 'billing_postcode' || $key == 'shipping_postcode' ) :
if ( ! $validation -> is_postcode ( $_POST [ $key ], $_POST [ $load_address . '_country' ] ) ) :
$woocommerce -> add_error ( __ ( 'Please enter a valid postcode/ZIP.' , 'woocommerce' ) );
else :
$_POST [ $key ] = $validation -> format_postcode ( $_POST [ $key ], $_POST [ $load_address . '_country' ] );
endif ;
endif ;
endforeach ;
if ( $woocommerce -> error_count () == 0 ) {
foreach ( $address as $key => $field ) :
update_user_meta ( $user_id , $key , $_POST [ $key ] );
endforeach ;
2013-01-10 08:36:29 +00:00
$woocommerce -> add_message ( __ ( 'Address changed successfully.' , 'woocommerce' ) );
2012-12-31 18:25:09 +00:00
do_action ( 'woocommerce_customer_save_address' , $user_id );
wp_safe_redirect ( get_permalink ( woocommerce_get_page_id ( 'myaccount' ) ) );
exit ;
}
}
2013-04-15 11:09:20 +00:00
add_action ( 'template_redirect' , 'woocommerce_save_address' );