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
*/
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-01-12 00:54:45 +00:00
function woocommerce_redirects () {
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-01-25 20:13:54 +00:00
if ( isset ( $_GET [ 'page_id' ]) && $_GET [ 'page_id' ] > 0 && 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 ;
endif ;
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
if ( is_page ( woocommerce_get_page_id ( 'checkout' )) && sizeof ( $woocommerce -> cart -> get_cart ()) == 0 ) :
wp_redirect ( get_permalink ( woocommerce_get_page_id ( 'cart' )));
exit ;
endif ;
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
if ( is_page ( woocommerce_get_page_id ( 'pay' )) && ! isset ( $_GET [ 'order' ])) :
wp_redirect ( get_permalink ( woocommerce_get_page_id ( 'checkout' )));
exit ;
endif ;
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// My account page redirects (logged out)
if ( ! 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' )));
exit ;
endif ;
2012-02-09 15:02:55 +00:00
// Redirect to the product page if we have a single product
if ( is_search () && is_post_type_archive ( 'product' ) && get_option ( 'woocommerce_redirect_on_single_search_result' ) == 'yes' ) {
if ( $wp_query -> post_count == 1 ) {
$product = new WC_Product ( $wp_query -> post -> ID );
if ( $product -> is_visible ()) wp_safe_redirect ( get_permalink ( $product -> id ), 302 );
exit ;
}
}
2011-12-09 19:55:09 +00:00
}
2011-11-26 20:33:15 +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
* 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 ;
}
2012-08-14 15:30:23 +00:00
2011-12-09 19:55:09 +00:00
/**
2012-08-14 15:30:23 +00:00
* Add logout link to my account menu .
*
* @ access public
* @ param string $items
* @ param array $args
* @ return string
*/
2011-12-09 19:55:09 +00:00
function woocommerce_nav_menu_items ( $items , $args ) {
2012-08-14 15:30:23 +00:00
if ( get_option ( 'woocommerce_menu_logout_link' ) == 'yes' && strstr ( $items , get_permalink ( woocommerce_get_page_id ( 'myaccount' ))) && is_user_logged_in () )
2012-05-01 09:39:19 +00:00
$items .= '<li class="logout"><a href="' . wp_logout_url ( home_url ()) . '">' . __ ( 'Logout' , 'woocommerce' ) . '</a></li>' ;
2012-08-10 11:15:32 +00:00
2011-12-09 19:55:09 +00:00
return $items ;
}
2011-09-20 16:43: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
* Update catalog ordering if posted .
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
2011-09-20 16:43:09 +00:00
function woocommerce_update_catalog_ordering () {
2012-09-07 17:26:13 +00:00
global $woocommerce ;
2012-08-14 15:30:23 +00:00
if ( isset ( $_REQUEST [ 'sort' ] ) && $_REQUEST [ 'sort' ] != '' )
2012-09-07 17:26:13 +00:00
$woocommerce -> session -> orderby = esc_attr ( $_REQUEST [ 'sort' ] );
2011-09-20 16:43:09 +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
* 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-01-05 11:31:22 +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 ;
// Clean the quantity input
$quantity = absint ( $cart_totals [ $cart_item_key ][ 'qty' ] );
2012-02-27 18:22:54 +00:00
// Update cart validation
$passed_validation = apply_filters ( 'woocommerce_update_cart_validation' , true , $cart_item_key , $values , $quantity );
2012-08-10 11:15:32 +00:00
2012-02-27 18:22:54 +00:00
// Check downloadable items
2012-08-21 16:23:26 +00:00
if ( get_option ( 'woocommerce_limit_downloadable_product_qty' ) == 'yes' ) {
if ( $_product -> is_downloadable () && $_product -> is_virtual () && $quantity > 1 ) {
2012-02-27 18:22:54 +00:00
$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 {
$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 ) {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2012-04-25 10:32:45 +00:00
if ( empty ( $_REQUEST [ 'add-to-cart' ] ) ) return ;
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
$added_to_cart = false ;
2012-08-10 11:15:32 +00:00
2012-01-28 22:57:30 +00:00
switch ( $_REQUEST [ 'add-to-cart' ]) {
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
// Variable Products
case 'variation' :
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
// Only allow integer variation ID - if its not set, redirect to the product page
2012-03-12 10:49:54 +00:00
if ( empty ( $_REQUEST [ 'variation_id' ]) || ! is_numeric ( $_REQUEST [ 'variation_id' ]) || $_REQUEST [ 'variation_id' ] < 1 ) {
2012-01-27 18:31:30 +00:00
$woocommerce -> add_error ( __ ( 'Please choose product options…' , 'woocommerce' ) );
2012-01-28 22:57:30 +00:00
wp_redirect ( apply_filters ( 'woocommerce_add_to_cart_product_id' , get_permalink ( $_REQUEST [ 'product_id' ])));
2012-01-27 18:31:30 +00:00
exit ;
}
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
// Get product ID to add and quantity
2012-01-28 22:57:30 +00:00
$product_id = ( int ) apply_filters ( 'woocommerce_add_to_cart_product_id' , $_REQUEST [ 'product_id' ]);
2012-03-12 10:49:54 +00:00
$variation_id = ( int ) $_REQUEST [ 'variation_id' ];
$quantity = ( isset ( $_REQUEST [ 'quantity' ])) ? ( int ) $_REQUEST [ 'quantity' ] : 1 ;
2012-01-27 18:31:30 +00:00
$attributes = ( array ) maybe_unserialize ( get_post_meta ( $product_id , '_product_attributes' , true ));
$variations = array ();
$all_variations_set = true ;
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
// Verify all attributes for the variable product were set
foreach ( $attributes as $attribute ) {
2011-09-10 20:21:44 +00:00
if ( ! $attribute [ 'is_variation' ] ) continue ;
2011-08-10 17:11:11 +00:00
2011-09-11 13:28:15 +00:00
$taxonomy = 'attribute_' . sanitize_title ( $attribute [ 'name' ]);
2012-03-12 10:49:54 +00:00
if ( ! empty ( $_REQUEST [ $taxonomy ])) {
2011-10-22 18:38:22 +00:00
// Get value from post data
2012-03-12 10:49:54 +00:00
$value = esc_attr ( stripslashes ( $_REQUEST [ $taxonomy ]));
2011-10-22 18:38:22 +00:00
2011-10-16 20:56:18 +00:00
// Use name so it looks nicer in the cart widget/order page etc - instead of a sanitized string
2011-10-22 18:38:22 +00:00
$variations [ esc_attr ( $attribute [ 'name' ])] = $value ;
2012-01-27 18:31:30 +00:00
} else {
2011-08-22 14:10:22 +00:00
$all_variations_set = false ;
2012-01-27 18:31:30 +00:00
}
}
2012-02-06 18:14:46 +00:00
2012-01-27 18:31:30 +00:00
if ( $all_variations_set ) {
// 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-01-27 18:31:30 +00:00
if ( $passed_validation ) {
if ( $woocommerce -> cart -> add_to_cart ( $product_id , $quantity , $variation_id , $variations )) {
woocommerce_add_to_cart_message ();
$added_to_cart = true ;
}
}
} else {
2012-01-05 11:31:22 +00:00
$woocommerce -> add_error ( __ ( 'Please choose product options…' , 'woocommerce' ) );
2012-01-28 22:57:30 +00:00
wp_redirect ( apply_filters ( 'woocommerce_add_to_cart_product_id' , get_permalink ( $_REQUEST [ 'product_id' ])));
2011-08-22 14:10:22 +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
break ;
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
// Grouped Products
case 'group' :
2012-08-10 11:15:32 +00:00
2012-03-12 10:49:54 +00:00
if ( isset ( $_REQUEST [ 'quantity' ]) && is_array ( $_REQUEST [ 'quantity' ])) {
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
$quantity_set = false ;
2012-08-10 11:15:32 +00:00
2012-03-12 10:49:54 +00:00
foreach ( $_REQUEST [ 'quantity' ] as $item => $quantity ) {
2012-01-27 18:31:30 +00:00
if ( $quantity < 1 ) continue ;
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
$quantity_set = true ;
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
// Add to cart validation
$passed_validation = apply_filters ( 'woocommerce_add_to_cart_validation' , true , $item , $quantity );
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
if ( $passed_validation ) {
if ( $woocommerce -> cart -> add_to_cart ( $item , $quantity )) {
woocommerce_add_to_cart_message ();
$added_to_cart = true ;
}
}
}
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
if ( ! $added_to_cart && ! $quantity_set ) {
$woocommerce -> add_error ( __ ( 'Please choose a quantity…' , 'woocommerce' ) );
2012-01-28 22:57:30 +00:00
wp_redirect ( apply_filters ( 'woocommerce_add_to_cart_product_id' , get_permalink ( $_REQUEST [ 'product_id' ])));
2012-01-27 18:31:30 +00:00
exit ;
}
2012-08-10 11:15:32 +00:00
2012-01-28 22:57:30 +00:00
} elseif ( $_REQUEST [ 'product_id' ]) {
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
/* Link on product archives */
$woocommerce -> add_error ( __ ( 'Please choose a product…' , 'woocommerce' ) );
2012-01-28 22:57:30 +00:00
wp_redirect ( get_permalink ( $_REQUEST [ 'product_id' ] ) );
2012-01-27 18:31:30 +00:00
exit ;
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
}
break ;
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
// Simple Products - add-to-cart contains product ID
default :
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
// Only allow integers
2012-01-28 22:57:30 +00:00
if ( ! is_numeric ( $_REQUEST [ 'add-to-cart' ])) break ;
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
// Get product ID to add and quantity
2012-01-28 22:57:30 +00:00
$product_id = ( int ) $_REQUEST [ 'add-to-cart' ];
2012-03-12 10:49:54 +00:00
$quantity = ( isset ( $_REQUEST [ 'quantity' ])) ? ( int ) $_REQUEST [ 'quantity' ] : 1 ;
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +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-01-27 18:31:30 +00:00
if ( $passed_validation ) {
// Add the product to the cart
2012-01-28 22:57:30 +00:00
if ( $woocommerce -> cart -> add_to_cart ( $_REQUEST [ 'add-to-cart' ], $quantity )) {
2012-01-27 18:31:30 +00:00
woocommerce_add_to_cart_message ();
$added_to_cart = true ;
}
}
2012-08-10 11:15:32 +00:00
2012-01-27 18:31:30 +00:00
break ;
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
if ( $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-08-31 09:05:28 +00:00
// Redirect to page without querystring args
elseif ( wp_get_referer () ) {
wp_safe_redirect ( remove_query_arg ( array ( 'add-to-cart' , 'quantity' , 'product_id' ), wp_get_referer () ) );
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
*/
2011-11-27 01:24:57 +00:00
function woocommerce_add_to_cart_message () {
global $woocommerce ;
2012-08-10 11:15:32 +00:00
2011-11-27 01:24:57 +00:00
// Output success messages
if ( get_option ( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :
2012-08-10 11:15:32 +00:00
2012-01-31 09:48:28 +00:00
$return_to = ( wp_get_referer ()) ? wp_get_referer () : home_url ();
2012-08-10 11:15:32 +00:00
2012-01-31 09:48:28 +00:00
$message = sprintf ( '<a href="%s" class="button">%s</a> %s' , $return_to , __ ( 'Continue Shopping →' , 'woocommerce' ), __ ( 'Product successfully added to your cart.' , 'woocommerce' ) );
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-01-31 09:48:28 +00:00
$message = sprintf ( '<a href="%s" class="button">%s</a> %s' , get_permalink ( woocommerce_get_page_id ( 'cart' )), __ ( 'View Cart →' , 'woocommerce' ), __ ( 'Product successfully added to your cart.' , 'woocommerce' ) );
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-01-12 00:54:45 +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
$order_key = urldecode ( $_GET [ 'order' ] );
$order_id = ( int ) $_GET [ 'order_id' ];
2012-01-27 16:38:39 +00:00
$order = new WC_Order ( $order_id );
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +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
if ( $order -> billing_country ) $woocommerce -> customer -> set_country ( $order -> billing_country );
if ( $order -> billing_state ) $woocommerce -> customer -> set_state ( $order -> billing_state );
if ( $order -> billing_postcode ) $woocommerce -> customer -> set_postcode ( $order -> billing_postcode );
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Update payment method
2012-08-10 11:15:32 +00:00
if ( $order -> order_total > 0 ) :
2012-01-12 00:54:45 +00:00
$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
update_post_meta ( $order_id , '_payment_method' , $payment_method );
if ( isset ( $available_gateways ) && isset ( $available_gateways [ $payment_method ])) :
2012-06-29 18:44:33 +00:00
$payment_method_title = $available_gateways [ $payment_method ] -> get_title ();
2012-01-12 00:54:45 +00:00
endif ;
update_post_meta ( $order_id , '_payment_method_title' , $payment_method_title );
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
$result = $available_gateways [ $payment_method ] -> process_payment ( $order_id );
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
// Redirect to success/confirmation/payment page
if ( $result [ 'result' ] == 'success' ) :
wp_redirect ( $result [ 'redirect' ] );
exit ;
endif ;
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 ();
wp_safe_redirect ( get_permalink ( woocommerce_get_page_id ( 'thanks' )) );
exit ;
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
endif ;
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +00:00
endif ;
2012-08-10 11:15:32 +00:00
2012-01-12 00:54:45 +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
* Process the login form .
*
* @ access public
* @ return void
*/
2011-08-10 17:11:11 +00:00
function woocommerce_process_login () {
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 ( $_POST [ 'login' ]) && $_POST [ 'login' ]) :
2012-08-10 11:15:32 +00:00
2011-09-06 11:11:22 +00:00
$woocommerce -> verify_nonce ( 'login' );
2011-08-10 17:11:11 +00:00
2012-01-05 11:31:22 +00:00
if ( ! isset ( $_POST [ 'username' ]) || empty ( $_POST [ 'username' ]) ) $woocommerce -> add_error ( __ ( 'Username is required.' , 'woocommerce' ) );
if ( ! isset ( $_POST [ 'password' ]) || empty ( $_POST [ 'password' ]) ) $woocommerce -> add_error ( __ ( 'Password is required.' , 'woocommerce' ) );
2012-08-10 11:15:32 +00:00
2011-09-06 11:11:22 +00:00
if ( $woocommerce -> error_count () == 0 ) :
2012-08-10 11:15:32 +00:00
2011-08-10 17:11:11 +00:00
$creds = array ();
2012-01-18 17:19:04 +00:00
$creds [ 'user_login' ] = esc_attr ( $_POST [ 'username' ]);
$creds [ 'user_password' ] = esc_attr ( $_POST [ 'password' ]);
2011-08-10 17:11:11 +00:00
$creds [ 'remember' ] = true ;
$secure_cookie = is_ssl () ? true : false ;
$user = wp_signon ( $creds , $secure_cookie );
if ( is_wp_error ( $user ) ) :
2011-09-06 11:11:22 +00:00
$woocommerce -> add_error ( $user -> get_error_message () );
2011-08-10 17:11:11 +00:00
else :
2012-08-10 11:15:32 +00:00
2012-01-05 14:55:09 +00:00
if ( isset ( $_POST [ 'redirect' ]) && $_POST [ 'redirect' ]) :
wp_safe_redirect ( esc_attr ( $_POST [ 'redirect' ]) );
exit ;
endif ;
2012-08-10 11:15:32 +00:00
2011-11-27 01:24:57 +00:00
if ( wp_get_referer () ) :
wp_safe_redirect ( wp_get_referer () );
2011-08-10 17:11:11 +00:00
exit ;
2011-10-24 11:54:06 +00:00
endif ;
2012-08-10 11:15:32 +00:00
2012-01-06 17:14:31 +00:00
wp_redirect ( get_permalink ( woocommerce_get_page_id ( 'myaccount' )));
2011-08-10 17:11:11 +00:00
exit ;
endif ;
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
endif ;
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 () {
2012-08-10 11:15:32 +00:00
2011-11-14 16:46:11 +00:00
global $woocommerce ;
2012-08-10 11:15:32 +00:00
2011-11-14 16:46:11 +00:00
if ( isset ( $_POST [ 'register' ]) && $_POST [ 'register' ]) :
2012-08-10 11:15:32 +00:00
2011-11-14 16:46:11 +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-08-10 11:15:32 +00:00
$user_email = isset ( $_POST [ 'email' ] ) ? esc_attr ( trim ( $_POST [ 'email' ] ) ) : '' ;
$password = isset ( $_POST [ 'password' ] ) ? esc_attr ( trim ( $_POST [ 'password' ] ) ) : '' ;
$password2 = isset ( $_POST [ 'password2' ] ) ? esc_attr ( trim ( $_POST [ 'password2' ] ) ) : '' ;
$user_email = apply_filters ( 'user_registration_email' , $user_email );
if ( get_option ( 'woocommerce_registration_email_for_username' ) == 'no' ) {
$username = isset ( $_POST [ 'username' ] ) ? esc_attr ( trim ( $_POST [ 'username' ] ) ) : '' ;
$sanitized_user_login = sanitize_user ( $username );
// Check the username
if ( $sanitized_user_login == '' ) {
$woocommerce -> add_error ( '<strong>' . __ ( 'ERROR' , 'woocommerce' ) . '</strong>: ' . __ ( 'Please enter a username.' , 'woocommerce' ) );
} elseif ( ! validate_username ( $username ) ) {
$woocommerce -> add_error ( '<strong>' . __ ( 'ERROR' , 'woocommerce' ) . '</strong>: ' . __ ( 'This username is invalid because it uses illegal characters. Please enter a valid username.' , 'woocommerce' ) );
$sanitized_user_login = '' ;
} elseif ( username_exists ( $sanitized_user_login ) ) {
$woocommerce -> add_error ( '<strong>' . __ ( 'ERROR' , 'woocommerce' ) . '</strong>: ' . __ ( 'This username is already registered, please choose another one.' , 'woocommerce' ) );
}
} 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-01-05 12:29:52 +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-01-05 12:29:52 +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-01-05 12:29:52 +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-01-05 11:31:22 +00:00
if ( ! $password ) $woocommerce -> add_error ( __ ( 'Password is required.' , 'woocommerce' ) );
if ( ! $password2 ) $woocommerce -> add_error ( __ ( 'Re-enter your password.' , 'woocommerce' ) );
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-01-05 11:31:22 +00:00
if ( isset ( $_POST [ 'email_2' ]) && $_POST [ 'email_2' ]) $woocommerce -> add_error ( __ ( 'Anti-spam field was filled in.' , 'woocommerce' ) );
2012-08-10 11:15:32 +00:00
2011-11-14 16:46:11 +00:00
if ( $woocommerce -> error_count () == 0 ) :
2012-08-10 11:15:32 +00:00
2011-11-14 16:46:11 +00:00
$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 );
2012-08-10 11:15:32 +00:00
2011-11-14 16:46:11 +00:00
// if there are no errors, let's create the user account
if ( ! $reg_errors -> get_error_code () ) :
$user_id = wp_create_user ( $sanitized_user_login , $password , $user_email );
2012-08-10 11:15:32 +00:00
2011-11-14 16:46:11 +00:00
if ( ! $user_id ) {
2012-06-06 20:35:40 +00:00
$woocommerce -> add_error ( '<strong>' . __ ( 'ERROR' , 'woocommerce' ) . '</strong>: ' . __ ( 'Couldn’t register you… please contact us if you continue to have problems.' , 'woocommerce' ) );
2011-11-14 16:46:11 +00:00
return ;
}
// Change role
wp_update_user ( array ( 'ID' => $user_id , 'role' => 'customer' ) ) ;
2012-08-10 11:15:32 +00:00
2011-12-10 17:28: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
// 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
2011-11-14 16:46:11 +00:00
// Redirect
2011-11-27 01:24:57 +00:00
if ( wp_get_referer () ) :
wp_safe_redirect ( wp_get_referer () );
2011-11-14 16:46:11 +00:00
exit ;
endif ;
2012-01-06 17:14:31 +00:00
wp_redirect ( get_permalink ( woocommerce_get_page_id ( 'myaccount' )));
2011-11-14 16:46:11 +00:00
exit ;
2012-08-10 11:15:32 +00:00
2011-11-14 16:46:11 +00:00
else :
$woocommerce -> add_error ( $reg_errors -> get_error_message () );
2012-08-10 11:15:32 +00:00
return ;
2011-11-14 16:46:11 +00:00
endif ;
2012-08-10 11:15:32 +00:00
2011-11-14 16:46:11 +00:00
endif ;
2012-08-10 11:15:32 +00:00
endif ;
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
$product_id = ( int ) apply_filters ( 'woocommerce_add_to_cart_product_id' , $item [ 'id' ] );
$quantity = ( int ) $item [ 'qty' ];
$variation_id = ( int ) $item [ 'variation_id' ];
2012-03-14 14:45:42 +00:00
$variations = array ();
2012-03-14 14:28:58 +00:00
foreach ( $item [ 'item_meta' ] as $meta ) {
if ( ! substr ( $meta [ 'meta_name' ], 0 , 3 ) === 'pa_' ) continue ;
$variations [ $meta [ 'meta_name' ]] = $meta [ 'meta_value' ];
}
// Add to cart validation
if ( ! apply_filters ( 'woocommerce_add_to_cart_validation' , true , $product_id , $quantity ) ) continue ;
$woocommerce -> cart -> add_to_cart ( $product_id , $quantity , $variation_id , $variations );
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-03-15 00:25:51 +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
2011-09-22 19:50:58 +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-01-05 11:31:22 +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-01-05 11:31:22 +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
2011-08-10 17:11:11 +00:00
elseif ( $order -> status != 'pending' ) :
2012-08-10 11:15:32 +00:00
2012-01-05 11:31:22 +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-01-05 11:31:22 +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
2011-08-10 17:11:11 +00:00
global $wpdb ;
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
$product_id = ( int ) urldecode ( $_GET [ 'download_file' ]);
$order_key = urldecode ( $_GET [ 'order' ] );
$email = str_replace ( ' ' , '+' , urldecode ( $_GET [ 'email' ] ) );
$download_id = isset ( $_GET [ 'key' ] ) ? urldecode ( $_GET [ 'key' ] ) : '' ; // backwards compatibility for existing download URLs
$_product = new WC_Product ( $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
);
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-01-05 12:29:52 +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' ) {
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>' );
elseif ( $user_id != get_current_user_id () )
wp_die ( __ ( 'This is not your download link.' , 'woocommerce' ) );
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 );
if ( ! $order -> is_download_permitted () && $order -> status != 'publish' )
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-01-05 12:29:52 +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 ,
'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
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
2012-08-28 15:21:54 +00:00
if ( ! $file_path ) exit ;
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
$file_download_method = apply_filters ( 'woocommerce_file_download_method' , get_option ( 'woocommerce_file_download_method' ), $product_id );
// Redirect to download location
if ( $file_download_method == 'redirect' ) {
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
2012-02-25 21:11:06 +00:00
// Get URLS with https
$site_url = site_url ();
$network_url = network_admin_url ();
2012-09-06 15:16:16 +00:00
if ( is_ssl () ) {
$site_url = str_replace ( 'https:' , 'http:' , $site_url );
$network_url = str_replace ( 'https:' , 'http:' , $network_url );
}
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
if ( ! is_multisite () ) {
$file_path = str_replace ( trailingslashit ( $site_url ), ABSPATH , $file_path );
} else {
2012-02-25 21:11:06 +00:00
$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
2012-09-06 15:16:16 +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
2012-09-06 15:16:16 +00:00
$file_path = str_replace ( $upload_dir [ 'baseurl' ], $upload_dir [ 'basedir' ], $file_path );
}
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 ;
2012-09-06 15:16:16 +00:00
$file_path = realpath ( $file_path );
}
2012-08-10 11:15:32 +00:00
2012-02-25 21:11:06 +00:00
// Download the file
2012-09-06 15:16:16 +00:00
$file_extension = strtolower ( substr ( strrchr ( $file_path , " . " ), 1 ) );
2012-08-10 11:15:32 +00:00
2012-02-25 21:11:06 +00:00
$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
2012-09-06 15:16:16 +00:00
if ( $file_download_method == 'xsendfile' ) {
// Path fix - kudos to Jason Judge
if ( getcwd () )
$file_path = trim ( preg_replace ( '`^' . getcwd () . '`' , '' , $file_path ), '/' );
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
header ( " Content-Disposition: attachment; filename= \" " . basename ( $file_path ) . " \" ; " );
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
2012-09-06 15:16:16 +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
2012-08-14 15:30:23 +00:00
if ( ! function_exists ( 'readfile_chunked' )) {
/**
* 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
*/
2012-09-06 15:16:16 +00:00
function readfile_chunked ( $file , $retbytes = true ) {
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
$chunksize = 1 * ( 1024 * 1024 );
2012-02-25 21:11:06 +00:00
$buffer = '' ;
$cnt = 0 ;
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
$handle = fopen ( $file , 'r' );
if ( $handle === FALSE )
return FALSE ;
while ( ! feof ( $handle ) ) {
$buffer = fread ( $handle , $chunksize );
echo $buffer ;
ob_flush ();
flush ();
if ( $retbytes )
$cnt += strlen ( $buffer );
}
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
$status = fclose ( $handle );
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
if ( $retbytes && $status )
return $cnt ;
2012-08-10 11:15:32 +00:00
2012-02-25 21:11:06 +00:00
return $status ;
}
}
@ session_write_close ();
2012-09-06 15:16:16 +00:00
if ( function_exists ( 'apache_setenv' ) )
@ apache_setenv ( 'no-gzip' , 1 );
@ ini_set ( 'zlib.output_compression' , 'Off' );
2012-02-25 21:11:06 +00:00
@ set_time_limit ( 0 );
@ set_magic_quotes_runtime ( 0 );
@ ob_end_clean ();
2012-09-06 15:16:16 +00:00
if ( ob_get_level () )
@ ob_end_clean (); // Zip corruption fix
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
header ( " Pragma: no-cache " );
header ( " Expires: 0 " );
header ( " Cache-Control: must-revalidate, post-check=0, pre-check=0 " );
header ( " Robots: none " );
header ( " Content-Type: " . $ctype );
header ( " Content-Description: File Transfer " );
header ( " Content-Disposition: attachment; filename= \" " . basename ( $file_path ) . " \" ; " );
header ( " Content-Transfer-Encoding: binary " );
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
if ( $size = @ filesize ( $file_path ) )
header ( " Content-Length: " . $size );
2012-02-25 21:11:06 +00:00
// Serve it
2012-09-06 15:16:16 +00:00
if ( $remote_file ) {
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
@ readfile_chunked ( " $file_path " ) or header ( 'Location: ' . $file_path );
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
} else {
2012-08-10 11:15:32 +00:00
2012-09-06 15:16:16 +00:00
@ readfile_chunked ( " $file_path " ) or wp_die ( __ ( 'File not found' , '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
}
2012-08-10 11:15:32 +00:00
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
}
2011-08-10 17:11:11 +00:00
}
2012-08-14 15:30:23 +00:00
2011-12-11 13:59:36 +00:00
/**
2012-08-14 15:30:23 +00:00
* ecommerce tracking with piwik .
*
* @ access public
* @ param int $order_id
* @ return void
2011-12-11 13:59:36 +00:00
*/
function woocommerce_ecommerce_tracking_piwik ( $order_id ) {
global $woocommerce ;
2012-08-10 11:15:32 +00:00
2011-12-19 00:13:41 +00:00
if ( is_admin ()) return ; // Don't track admin
2012-08-10 11:15:32 +00:00
2011-12-19 00:13:41 +00:00
// Call the Piwik ecommerce function if WP-Piwik is configured to add tracking codes to the page
$wp_piwik_global_settings = get_option ( 'wp-piwik_global-settings' );
2012-08-10 11:15:32 +00:00
2012-05-23 11:15:25 +00:00
// Return if Piwik settings are not here, or if global is not set
if ( ! isset ( $wp_piwik_global_settings [ 'add_tracking_code' ] ) || ! $wp_piwik_global_settings [ 'add_tracking_code' ] ) return ;
if ( ! isset ( $GLOBALS [ 'wp_piwik' ] ) ) return ;
2012-08-10 11:15:32 +00:00
// Remove WP-Piwik from wp_footer and run it here instead, to get Piwik
2011-12-11 13:59:36 +00:00
// loaded *before* we do our ecommerce tracking calls
remove_action ( 'wp_footer' , array ( $GLOBALS [ 'wp_piwik' ], 'footer' ));
$GLOBALS [ 'wp_piwik' ] -> footer ();
2012-08-10 11:15:32 +00:00
2011-12-11 13:59:36 +00:00
// Get the order and output tracking code
2012-01-27 16:38:39 +00:00
$order = new WC_Order ( $order_id );
2011-12-11 13:59:36 +00:00
?>
< script type = " text/javascript " >
try {
// Add order items
2012-01-13 00:46:56 +00:00
< ? php if ( $order -> get_items ()) foreach ( $order -> get_items () as $item ) : $_product = $order -> get_product_from_item ( $item ); ?>
2011-12-11 13:59:36 +00:00
piwikTracker . addEcommerceItem (
" <?php echo $_product->sku ; ?> " , // (required) SKU: Product unique identifier
" <?php echo $item['name'] ; ?> " , // (optional) Product name
" <?php if (isset( $_product->variation_data )) echo woocommerce_get_formatted_variation( $_product->variation_data , true ); ?> " , // (optional) Product category. You can also specify an array of up to 5 categories eg. ["Books", "New releases", "Biography"]
2012-01-04 16:24:26 +00:00
< ? php echo ( $item [ 'line_cost' ] / $item [ 'qty' ]); ?> , // (recommended) Product price
2011-12-11 13:59:36 +00:00
< ? php echo $item [ 'qty' ]; ?> // (optional, default to 1) Product quantity
);
< ? php endforeach ; ?>
// Track order
piwikTracker . trackEcommerceOrder (
" <?php echo $order_id ; ?> " , // (required) Unique Order ID
< ? php echo $order -> order_total ; ?> , // (required) Order Revenue grand total (includes tax, shipping, and subtracted discount)
false , // (optional) Order sub total (excludes shipping)
2012-01-04 16:24:26 +00:00
< ? php echo $order -> get_total_tax (); ?> , // (optional) Tax amount
< ? php echo $order -> get_shipping (); ?> , // (optional) Shipping amount
2011-12-11 13:59:36 +00:00
false // (optional) Discount offered (set to false for unspecified parameter)
);
} catch ( err ) {}
</ script >
< ? php
2012-08-10 11:15:32 +00:00
}
2011-12-11 13:59:36 +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
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-01-05 11:31:22 +00:00
echo '<link rel="alternate" type="application/rss+xml" title="' . __ ( 'New products' , 'woocommerce' ) . '" href="' . $feed . '" />' ;
2012-08-10 11:15:32 +00:00
2011-10-31 01:03:51 +00:00
elseif ( is_tax ( 'product_cat' ) ) :
2012-08-10 11:15:32 +00:00
2011-10-31 01:03:51 +00:00
$term = get_term_by ( 'slug' , 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-01-05 11:31:22 +00:00
echo '<link rel="alternate" type="application/rss+xml" title="' . sprintf ( __ ( 'New products added to %s' , 'woocommerce' ), urlencode ( $term -> name )) . '" href="' . $feed . '" />' ;
2012-08-10 11:15:32 +00:00
2011-10-31 01:03:51 +00:00
elseif ( is_tax ( 'product_tag' ) ) :
2012-08-10 11:15:32 +00:00
2011-10-31 01:03:51 +00:00
$term = get_term_by ( 'slug' , 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-01-05 11:31:22 +00:00
echo '<link rel="alternate" type="application/rss+xml" title="' . sprintf ( __ ( 'New products tagged %s' , 'woocommerce' ), urlencode ( $term -> name )) . '" href="' . $feed . '" />' ;
2012-08-10 11:15:32 +00:00
2011-10-31 01:03:51 +00:00
endif ;
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
*/
2011-12-09 19:55:09 +00:00
function woocommerce_add_comment_rating ( $comment_id ) {
if ( isset ( $_POST [ 'rating' ]) ) :
global $post ;
2012-03-27 19:35:35 +00:00
if ( ! $_POST [ 'rating' ] || $_POST [ 'rating' ] > 5 || $_POST [ 'rating' ] < 0 ) return ;
add_comment_meta ( $comment_id , 'rating' , ( int ) esc_attr ( $_POST [ 'rating' ]), true );
2012-03-16 16:39:16 +00:00
delete_transient ( 'wc_average_rating_' . esc_attr ( $post -> ID ) );
2011-12-09 19:55:09 +00:00
endif ;
}
2012-08-14 15:30:23 +00:00
/**
* Validate the comment ratings .
*
* @ access public
* @ param array $comment_data
* @ return array
*/
2011-12-09 19:55:09 +00:00
function woocommerce_check_comment_rating ( $comment_data ) {
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-01-05 11:31:22 +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-01-05 11:31:22 +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
/**
* Finds an Order ID based on an order key .
*
* @ access public
* @ param string $order_key An order key has generated by
* @ 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 ;
}