2011-08-10 17:11:11 +00:00
< ? php
/**
2011-12-09 17:01:56 +00:00
* My Account Shortcodes
2011-08-10 17:11:11 +00:00
*
* Shows the 'my account' section where the customer can view past orders and update their information .
*
* @ package WooCommerce
* @ category Shortcode
* @ author WooThemes
*/
2011-12-09 17:01:56 +00:00
/**
* Shortcode wrappers
*/
function get_woocommerce_my_account ( $atts ) {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
return $woocommerce -> shortcode_wrapper ( 'woocommerce_my_account' , $atts );
2011-12-09 17:01:56 +00:00
}
function get_woocommerce_edit_address () {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2011-12-09 17:01:56 +00:00
return $woocommerce -> shortcode_wrapper ( 'woocommerce_edit_address' );
}
function get_woocommerce_change_password () {
global $woocommerce ;
return $woocommerce -> shortcode_wrapper ( 'woocommerce_change_password' );
}
function get_woocommerce_view_order () {
global $woocommerce ;
return $woocommerce -> shortcode_wrapper ( 'woocommerce_view_order' );
}
2011-08-10 17:11:11 +00:00
2011-12-09 17:01:56 +00:00
/**
* My Account Shortcode .
*
* @ package WooCommerce
* @ since 1.4
*/
function woocommerce_my_account ( $atts ) {
2012-02-03 16:17:35 +00:00
global $woocommerce , $current_user ;
2011-08-10 17:11:11 +00:00
2012-02-24 21:05:15 +00:00
$woocommerce -> nocache ();
2012-02-07 12:09:23 +00:00
2011-12-09 17:01:56 +00:00
if ( ! is_user_logged_in () ) :
2011-08-10 17:11:11 +00:00
2012-01-29 13:36:33 +00:00
woocommerce_get_template ( 'myaccount/form-login.php' );
2011-12-09 17:01:56 +00:00
2011-08-10 17:11:11 +00:00
else :
2011-12-09 17:01:56 +00:00
extract ( shortcode_atts ( array (
'recent_orders' => 5
), $atts ));
2011-08-10 17:11:11 +00:00
2011-12-09 17:01:56 +00:00
$recent_orders = ( 'all' == $recent_orders ) ? - 1 : $recent_orders ;
2011-08-10 17:11:11 +00:00
2011-12-09 17:01:56 +00:00
get_currentuserinfo ();
2012-02-03 16:17:35 +00:00
woocommerce_get_template ( 'myaccount/my-account.php' , array (
'current_user' => $current_user ,
'recent_orders' => $recent_orders
) );
2011-12-09 17:01:56 +00:00
2011-08-10 17:11:11 +00:00
endif ;
}
2011-12-09 17:01:56 +00:00
/**
* Edit Address Shortcode .
*
* @ todo Address fields should be loaded using the array defined in
* the checkout class , and the fields should be built off of that .
*
* Adapted from spencerfinnell ' s pull request
*
* @ package WooCommerce
* @ since 1.4
*/
2011-08-10 17:11:11 +00:00
function woocommerce_edit_address () {
2012-02-03 16:17:35 +00:00
global $woocommerce ;
2011-08-10 17:11:11 +00:00
2012-02-24 21:05:15 +00:00
$woocommerce -> nocache ();
2012-02-07 12:09:23 +00:00
2012-01-12 00:54:45 +00:00
if ( ! is_user_logged_in () ) return ;
2011-12-09 17:01:56 +00:00
$load_address = woocommerce_get_address_to_edit ();
2011-12-21 16:03:45 +00:00
$address = $woocommerce -> countries -> get_address_fields ( get_user_meta ( get_current_user_id (), $load_address . '_country' , true ), $load_address . '_' );
2011-12-09 17:01:56 +00:00
2012-02-03 16:17:35 +00:00
woocommerce_get_template ( 'myaccount/form-edit-address.php' , array (
'load_address' => $load_address ,
'address' => $address
) );
2011-08-10 17:11:11 +00:00
}
2011-12-09 17:01:56 +00:00
/**
* Save and and update a billing or shipping address if the
* form was submitted through the user account page .
*
* @ package WooCommerce
* @ since 1.4
*/
function woocommerce_save_address () {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2011-12-09 17:01:56 +00:00
if ( 'POST' !== strtoupper ( $_SERVER [ 'REQUEST_METHOD' ] ) )
return ;
if ( empty ( $_POST [ 'action' ] ) || ( 'edit_address' !== $_POST [ 'action' ] ) )
return ;
$woocommerce -> verify_nonce ( 'edit_address' );
2012-01-13 00:46:56 +00:00
$validation = $woocommerce -> validation ();
2011-12-09 17:01:56 +00:00
$user_id = get_current_user_id ();
2011-12-21 16:03:45 +00:00
if ( $user_id <= 0 ) return ;
2011-12-09 17:01:56 +00:00
$load_address = woocommerce_get_address_to_edit ();
2011-12-21 16:03:45 +00:00
$address = $woocommerce -> countries -> get_address_fields ( esc_attr ( $_POST [ $load_address . '_country' ]), $load_address . '_' );
2011-12-09 17:01:56 +00:00
2011-12-21 16:03:45 +00:00
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
2012-01-05 11:31:22 +00:00
if ( isset ( $field [ 'required' ]) && $field [ 'required' ] && empty ( $_POST [ $key ]) ) $woocommerce -> add_error ( $field [ 'label' ] . ' ' . __ ( 'is a required field.' , 'woocommerce' ) );
2011-12-21 16:03:45 +00:00
// Postcode
if ( $key == 'billing_postcode' || $key == 'shipping_postcode' ) :
2012-01-13 00:46:56 +00:00
if ( ! $validation -> is_postcode ( $_POST [ $key ], $_POST [ $load_address . '_country' ] ) ) :
2012-01-05 11:31:22 +00:00
$woocommerce -> add_error ( __ ( 'Please enter a valid postcode/ZIP.' , 'woocommerce' ) );
2011-12-21 16:03:45 +00:00
else :
2012-01-13 00:46:56 +00:00
$_POST [ $key ] = $validation -> format_postcode ( $_POST [ $key ], $_POST [ $load_address . '_country' ] );
2011-12-21 16:03:45 +00:00
endif ;
endif ;
endforeach ;
2011-12-09 17:01:56 +00:00
if ( $woocommerce -> error_count () == 0 ) {
2011-12-21 16:03:45 +00:00
foreach ( $address as $key => $field ) :
update_user_meta ( $user_id , $key , $_POST [ $key ] );
endforeach ;
2011-12-09 17:01:56 +00:00
do_action ( 'woocommerce_customer_save_address' , $user_id );
2012-01-06 17:14:31 +00:00
wp_safe_redirect ( get_permalink ( woocommerce_get_page_id ( 'myaccount' ) ) );
2011-12-09 17:01:56 +00:00
exit ;
}
}
add_action ( 'template_redirect' , 'woocommerce_save_address' );
/**
* Figure out which address is being viewed / edited .
*
* @ package WooCommerce
* @ since 1.4
*/
function woocommerce_get_address_to_edit () {
2012-02-22 21:06:08 +00:00
$load_address = ( isset ( $_GET [ 'address' ] ) ) ? esc_attr ( $_GET [ 'address' ] ) : '' ;
$load_address = ( $load_address == 'billing' || $load_address == 'shipping' ) ? $load_address : '' ;
2011-12-09 17:01:56 +00:00
return $load_address ;
}
/**
* Change Password Shortcode
*
* @ package WooCommerce
* @ since 1.4
*/
2011-08-10 17:11:11 +00:00
function woocommerce_change_password () {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2012-01-12 00:54:45 +00:00
if ( ! is_user_logged_in () ) return ;
2011-12-09 17:01:56 +00:00
2012-01-29 13:36:33 +00:00
woocommerce_get_template ( 'myaccount/form-change-password.php' );
2011-12-09 17:01:56 +00:00
}
/**
* Save the password and redirect back to the my account page .
*
* @ package WooCommerce
* @ since 1.4
*/
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' );
2011-08-10 17:11:11 +00:00
$user_id = get_current_user_id ();
2011-12-09 17:01:56 +00:00
if ( $user_id <= 0 )
return ;
2011-08-10 17:11:11 +00:00
2011-12-09 17:01:56 +00:00
$_POST = array_map ( 'woocommerce_clean' , $_POST );
if ( empty ( $_POST [ 'password_1' ] ) || empty ( $_POST [ 'password_2' ] ) )
2012-01-05 11:31:22 +00:00
$woocommerce -> add_error ( __ ( 'Please enter your password.' , 'woocommerce' ) );
2011-08-10 17:11:11 +00:00
2011-12-09 17:01:56 +00:00
if ( $_POST [ 'password_1' ] !== $_POST [ 'password_2' ] )
2012-01-05 11:31:22 +00:00
$woocommerce -> add_error ( __ ( 'Passwords do not match.' , 'woocommerce' ) );
2011-08-10 17:11:11 +00:00
2011-12-09 17:01:56 +00:00
if ( $woocommerce -> error_count () == 0 ) {
2011-08-10 17:11:11 +00:00
2011-12-09 17:01:56 +00:00
wp_update_user ( array ( 'ID' => $user_id , 'user_pass' => esc_attr ( $_POST [ 'password_1' ] ) ) ) ;
2011-08-10 17:11:11 +00:00
2011-12-09 17:01:56 +00:00
do_action ( 'woocommerce_customer_change_password' , $user_id );
2012-01-06 17:14:31 +00:00
wp_safe_redirect ( get_permalink ( woocommerce_get_page_id ( 'myaccount' ) ) );
2011-08-10 17:11:11 +00:00
exit ;
2011-12-09 17:01:56 +00:00
}
2011-08-10 17:11:11 +00:00
}
2011-12-09 17:01:56 +00:00
add_action ( 'template_redirect' , 'woocommerce_save_password' );
2011-08-10 17:11:11 +00:00
2011-12-09 17:01:56 +00:00
/**
* View Order Shortcode
*
* @ package WooCommerce
* @ since 1.4
*/
2011-08-10 17:11:11 +00:00
function woocommerce_view_order () {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2011-08-10 17:11:11 +00:00
2012-02-24 21:05:15 +00:00
$woocommerce -> nocache ();
2012-02-07 12:09:23 +00:00
2012-01-12 00:54:45 +00:00
if ( ! is_user_logged_in () ) return ;
2011-08-10 17:11:11 +00:00
2011-12-09 17:01:56 +00:00
$user_id = get_current_user_id ();
2012-04-09 22:57:57 +00:00
$order_id = ( isset ( $_GET [ 'order' ] ) ) ? $_GET [ 'order' ] : 0 ;
2012-01-27 16:38:39 +00:00
$order = new WC_Order ( $order_id );
2012-04-09 22:57:57 +00:00
if ( $order_id == 0 ) {
woocommerce_get_template ( 'myaccount/my-orders.php' , array ( 'recent_orders' => 10 ));
return ;
}
if ( $order -> user_id != $user_id ) {
2012-01-12 00:54:45 +00:00
echo '<div class="woocommerce_error">' . __ ( 'Invalid order.' , 'woocommerce' ) . ' <a href="' . get_permalink ( woocommerce_get_page_id ( 'myaccount' ) ) . '">' . __ ( 'My Account →' , 'woocommerce' ) . '</a>' . '</div>' ;
return ;
2012-04-09 22:57:57 +00:00
}
2011-08-10 17:11:11 +00:00
2011-12-09 17:01:56 +00:00
$status = get_term_by ( 'slug' , $order -> status , 'shop_order_status' );
2011-08-10 17:11:11 +00:00
2011-12-09 17:01:56 +00:00
echo '<p>'
2012-03-26 10:26:50 +00:00
. sprintf ( __ ( 'Order <mark>%s</mark> made on <mark>%s</mark>' , 'woocommerce' ), $order -> get_order_number (), date_i18n ( get_option ( 'date_format' ), strtotime ( $order -> order_date )) )
2012-01-05 11:31:22 +00:00
. sprintf ( __ ( '. Order status: <mark>%s</mark>' , 'woocommerce' ), __ ( $status -> name , 'woocommerce' ) )
2011-12-09 17:01:56 +00:00
. '.</p>' ;
2012-02-24 20:20:17 +00:00
2011-12-09 17:01:56 +00:00
$notes = $order -> get_customer_order_notes ();
if ( $notes ) :
?>
2012-01-05 11:31:22 +00:00
< h2 >< ? php _e ( 'Order Updates' , 'woocommerce' ); ?> </h2>
2011-12-09 17:01:56 +00:00
< ol class = " commentlist notes " >
< ? php foreach ( $notes as $note ) : ?>
< li class = " comment note " >
< div class = " comment_container " >
< div class = " comment-text " >
< p class = " meta " >< ? php echo date_i18n ( 'l jS \of F Y, h:ia' , strtotime ( $note -> comment_date )); ?> </p>
< div class = " description " >
< ? php echo wpautop ( wptexturize ( $note -> comment_content )); ?>
</ div >
< div class = " clear " ></ div >
</ div >
< div class = " clear " ></ div >
</ div >
</ li >
< ? php endforeach ; ?>
</ ol >
< ? php
2011-08-10 17:11:11 +00:00
endif ;
2011-12-09 17:01:56 +00:00
do_action ( 'woocommerce_view_order' , $order_id );
2011-08-10 17:11:11 +00:00
}