2011-12-08 15:13:15 +00:00
< ? php
/**
* WooCommerce Ajax Handlers
2012-08-01 12:43:46 +00:00
*
2011-12-08 15:13:15 +00:00
* Handles AJAX requests via wp_ajax hook ( both admin and front - end events )
*
* @ author WooThemes
2012-08-14 16:19:22 +00:00
* @ category Core
* @ package WooCommerce / Functions / AJAX
2013-01-28 10:59:19 +00:00
* @ version 2.0 . 0
2011-12-08 15:13:15 +00:00
*/
2012-10-15 10:57:58 +00:00
if ( ! defined ( 'ABSPATH' ) ) exit ; // Exit if accessed directly
2012-08-01 12:43:46 +00:00
/** Frontend AJAX events **************************************************/
2011-12-09 19:55:09 +00:00
2013-01-27 18:55:24 +00:00
/**
2013-01-28 10:59:19 +00:00
* woocommerce_get_refreshed_fragments function .
2013-01-27 18:55:24 +00:00
*
* @ access public
* @ return void
*/
2013-01-28 10:59:19 +00:00
function woocommerce_get_refreshed_fragments () {
global $woocommerce ;
2013-01-27 18:55:24 +00:00
header ( 'Content-Type: application/json; charset=utf-8' );
// Get mini cart
ob_start ();
woocommerce_mini_cart ();
$mini_cart = ob_get_clean ();
// Fragments and mini cart are returned
$data = array (
2013-01-28 10:59:19 +00:00
'fragments' => apply_filters ( 'add_to_cart_fragments' , array (
'div.widget_shopping_cart_content' => '<div class="widget_shopping_cart_content">' . $mini_cart . '</div>'
)
),
2013-07-30 14:23:08 +00:00
'cart_hash' => $woocommerce -> cart -> get_cart () ? md5 ( json_encode ( $woocommerce -> cart -> get_cart () ) ) : ''
2013-01-27 18:55:24 +00:00
);
echo json_encode ( $data );
die ();
}
2013-01-28 10:59:19 +00:00
add_action ( 'wp_ajax_nopriv_woocommerce_get_refreshed_fragments' , 'woocommerce_get_refreshed_fragments' );
add_action ( 'wp_ajax_woocommerce_get_refreshed_fragments' , 'woocommerce_get_refreshed_fragments' );
2013-01-27 18:55:24 +00:00
2012-06-10 11:40:07 +00:00
/**
* AJAX apply coupon on checkout page
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2012-06-10 11:40:07 +00:00
*/
function woocommerce_ajax_apply_coupon () {
global $woocommerce ;
2012-08-01 12:43:46 +00:00
2012-06-10 11:40:07 +00:00
check_ajax_referer ( 'apply-coupon' , 'security' );
2012-08-01 12:43:46 +00:00
2012-06-10 11:40:07 +00:00
if ( ! empty ( $_POST [ 'coupon_code' ] ) ) {
2012-11-08 16:57:53 +00:00
$woocommerce -> cart -> add_discount ( sanitize_text_field ( $_POST [ 'coupon_code' ] ) );
2012-06-10 11:40:07 +00:00
} else {
2013-06-11 14:59:54 +00:00
wc_add_error ( WC_Coupon :: get_generic_coupon_error ( WC_Coupon :: E_WC_COUPON_PLEASE_ENTER ) );
2012-06-10 11:40:07 +00:00
}
2012-08-01 12:43:46 +00:00
2013-06-11 14:59:54 +00:00
wc_print_messages ();
2012-08-01 12:43:46 +00:00
2012-06-10 11:40:07 +00:00
die ();
}
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_apply_coupon' , 'woocommerce_ajax_apply_coupon' );
add_action ( 'wp_ajax_nopriv_woocommerce_apply_coupon' , 'woocommerce_ajax_apply_coupon' );
2011-12-09 19:55:09 +00:00
/**
* AJAX update shipping method on cart page
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
function woocommerce_ajax_update_shipping_method () {
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
check_ajax_referer ( 'update-shipping-method' , 'security' );
2012-08-01 12:43:46 +00:00
2012-04-11 12:08:04 +00:00
if ( ! defined ( 'WOOCOMMERCE_CART' ) ) define ( 'WOOCOMMERCE_CART' , true );
2012-08-01 12:43:46 +00:00
2013-08-14 20:00:34 +00:00
$chosen_shipping_methods = WC () -> session -> get ( 'chosen_shipping_methods' );
if ( isset ( $_POST [ 'shipping_method' ] ) && is_array ( $_POST [ 'shipping_method' ] ) )
foreach ( $_POST [ 'shipping_method' ] as $i => $value )
$chosen_shipping_methods [ $i ] = woocommerce_clean ( $value );
WC () -> session -> set ( 'chosen_shipping_methods' , $chosen_shipping_methods );
2012-08-01 12:43:46 +00:00
2013-08-14 20:00:34 +00:00
WC () -> cart -> calculate_totals ();
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
woocommerce_cart_totals ();
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
die ();
}
2011-12-08 15:13:15 +00:00
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_update_shipping_method' , 'woocommerce_ajax_update_shipping_method' );
add_action ( 'wp_ajax_nopriv_woocommerce_update_shipping_method' , 'woocommerce_ajax_update_shipping_method' );
2011-12-09 19:55:09 +00:00
/**
* AJAX update order review on checkout
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
function woocommerce_ajax_update_order_review () {
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
check_ajax_referer ( 'update-order-review' , 'security' );
2012-08-01 12:43:46 +00:00
2012-11-27 16:22:47 +00:00
if ( ! defined ( 'WOOCOMMERCE_CHECKOUT' ) )
2012-09-07 17:26:13 +00:00
define ( 'WOOCOMMERCE_CHECKOUT' , true );
2012-08-01 12:43:46 +00:00
2013-08-14 20:00:34 +00:00
if ( sizeof ( WC () -> cart -> get_cart () ) == 0 ) {
2013-10-28 09:45:59 +00:00
echo '<div class="woocommerce-error">' . __ ( 'Sorry, your session has expired.' , 'woocommerce' ) . ' <a href="' . home_url () . '" class="wc-backward">' . __ ( 'Return to homepage' , 'woocommerce' ) . '</a></div>' ;
2011-12-08 15:13:15 +00:00
die ();
2012-09-07 17:26:13 +00:00
}
2012-08-01 12:43:46 +00:00
2012-12-10 12:34:55 +00:00
do_action ( 'woocommerce_checkout_update_order_review' , $_POST [ 'post_data' ] );
2012-08-01 12:43:46 +00:00
2013-08-14 20:00:34 +00:00
$chosen_shipping_methods = WC () -> session -> get ( 'chosen_shipping_methods' );
if ( isset ( $_POST [ 'shipping_method' ] ) && is_array ( $_POST [ 'shipping_method' ] ) )
foreach ( $_POST [ 'shipping_method' ] as $i => $value )
$chosen_shipping_methods [ $i ] = woocommerce_clean ( $value );
WC () -> session -> set ( 'chosen_shipping_methods' , $chosen_shipping_methods );
WC () -> session -> set ( 'chosen_payment_method' , empty ( $_POST [ 'payment_method' ] ) ? '' : $_POST [ 'payment_method' ] );
2012-11-27 16:22:47 +00:00
if ( isset ( $_POST [ 'country' ] ) )
2013-08-14 20:00:34 +00:00
WC () -> customer -> set_country ( $_POST [ 'country' ] );
2012-11-27 16:22:47 +00:00
if ( isset ( $_POST [ 'state' ] ) )
2013-08-14 20:00:34 +00:00
WC () -> customer -> set_state ( $_POST [ 'state' ] );
2012-11-27 16:22:47 +00:00
if ( isset ( $_POST [ 'postcode' ] ) )
2013-08-14 20:00:34 +00:00
WC () -> customer -> set_postcode ( $_POST [ 'postcode' ] );
2012-11-27 16:22:47 +00:00
if ( isset ( $_POST [ 'city' ] ) )
2013-08-14 20:00:34 +00:00
WC () -> customer -> set_city ( $_POST [ 'city' ] );
2012-11-27 16:22:47 +00:00
if ( isset ( $_POST [ 'address' ] ) )
2013-08-14 20:00:34 +00:00
WC () -> customer -> set_address ( $_POST [ 'address' ] );
2012-11-27 16:22:47 +00:00
if ( isset ( $_POST [ 'address_2' ] ) )
2013-08-14 20:00:34 +00:00
WC () -> customer -> set_address_2 ( $_POST [ 'address_2' ] );
2012-11-27 16:22:47 +00:00
if ( isset ( $_POST [ 's_country' ] ) )
2013-08-14 20:00:34 +00:00
WC () -> customer -> set_shipping_country ( $_POST [ 's_country' ] );
2012-11-27 16:22:47 +00:00
if ( isset ( $_POST [ 's_state' ] ) )
2013-08-14 20:00:34 +00:00
WC () -> customer -> set_shipping_state ( $_POST [ 's_state' ] );
2012-11-27 16:22:47 +00:00
if ( isset ( $_POST [ 's_postcode' ] ) )
2013-08-14 20:00:34 +00:00
WC () -> customer -> set_shipping_postcode ( $_POST [ 's_postcode' ] );
2012-11-27 16:22:47 +00:00
if ( isset ( $_POST [ 's_city' ] ) )
2013-08-14 20:00:34 +00:00
WC () -> customer -> set_shipping_city ( $_POST [ 's_city' ] );
2012-11-27 16:22:47 +00:00
if ( isset ( $_POST [ 's_address' ] ) )
2013-08-14 20:00:34 +00:00
WC () -> customer -> set_shipping_address ( $_POST [ 's_address' ] );
2012-11-27 16:22:47 +00:00
if ( isset ( $_POST [ 's_address_2' ] ) )
2013-08-14 20:00:34 +00:00
WC () -> customer -> set_shipping_address_2 ( $_POST [ 's_address_2' ] );
2012-08-01 12:43:46 +00:00
2013-08-14 20:00:34 +00:00
WC () -> cart -> calculate_totals ();
2012-08-01 12:43:46 +00:00
2012-12-10 12:34:55 +00:00
do_action ( 'woocommerce_checkout_order_review' ); // Display review order table
2011-12-09 19:55:09 +00:00
die ();
}
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_update_order_review' , 'woocommerce_ajax_update_order_review' );
add_action ( 'wp_ajax_nopriv_woocommerce_update_order_review' , 'woocommerce_ajax_update_order_review' );
2011-12-09 19:55:09 +00:00
/**
* AJAX add to cart
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
function woocommerce_ajax_add_to_cart () {
global $woocommerce ;
2012-08-01 12:43:46 +00:00
2013-04-08 17:43:07 +00:00
$product_id = apply_filters ( 'woocommerce_add_to_cart_product_id' , absint ( $_POST [ 'product_id' ] ) );
$quantity = empty ( $_POST [ 'quantity' ] ) ? 1 : apply_filters ( 'woocommerce_stock_amount' , $_POST [ 'quantity' ] );
$passed_validation = apply_filters ( 'woocommerce_add_to_cart_validation' , true , $product_id , $quantity );
2012-08-01 12:43:46 +00:00
2013-01-28 11:42:03 +00:00
if ( $passed_validation && $woocommerce -> cart -> add_to_cart ( $product_id , $quantity ) ) {
2013-01-22 16:31:54 +00:00
do_action ( 'woocommerce_ajax_added_to_cart' , $product_id );
2013-01-28 10:59:19 +00:00
2013-02-10 23:02:52 +00:00
if ( get_option ( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
woocommerce_add_to_cart_message ( $product_id );
}
2013-01-28 10:59:19 +00:00
// Return fragments
woocommerce_get_refreshed_fragments ();
2013-01-22 16:31:54 +00:00
} else {
2013-01-28 10:59:19 +00:00
header ( 'Content-Type: application/json; charset=utf-8' );
2012-01-27 18:31:30 +00:00
// If there was an error adding to the cart, redirect to the product page to show any errors
2011-12-09 19:55:09 +00:00
$data = array (
2012-01-27 18:31:30 +00:00
'error' => true ,
2013-04-08 17:43:07 +00:00
'product_url' => apply_filters ( 'woocommerce_cart_redirect_after_error' , get_permalink ( $product_id ), $product_id )
2011-12-09 19:55:09 +00:00
);
2013-01-28 10:59:19 +00:00
echo json_encode ( $data );
}
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
die ();
}
2011-12-08 15:13:15 +00:00
2013-11-13 11:25:27 +00:00
add_action ( 'wp_ajax_woocommerce_add_to_cart' , 'woocommerce_ajax_add_to_cart' );
add_action ( 'wp_ajax_nopriv_woocommerce_add_to_cart' , 'woocommerce_ajax_add_to_cart' );
2012-08-14 16:19:22 +00:00
2011-12-09 19:55:09 +00:00
/**
* Process ajax checkout form
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
2012-01-12 00:54:45 +00:00
function woocommerce_process_checkout () {
2013-11-13 11:25:27 +00:00
if ( ! defined ( 'WOOCOMMERCE_CHECKOUT' ) )
define ( 'WOOCOMMERCE_CHECKOUT' , true );
2012-08-01 12:43:46 +00:00
2013-11-13 11:25:27 +00:00
$woocommerce_checkout = WC () -> checkout ();
2011-12-09 19:55:09 +00:00
$woocommerce_checkout -> process_checkout ();
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
die ( 0 );
}
2013-11-13 11:25:27 +00:00
add_action ( 'wp_ajax_woocommerce-checkout' , 'woocommerce_process_checkout' );
add_action ( 'wp_ajax_nopriv_woocommerce-checkout' , 'woocommerce_process_checkout' );
2012-08-14 16:19:22 +00:00
2011-12-09 19:55:09 +00:00
2012-08-01 12:43:46 +00:00
/** Admin AJAX events *****************************************************/
2011-12-09 19:55:09 +00:00
/**
* Feature a product from admin
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
function woocommerce_feature_product () {
2012-08-14 16:19:22 +00:00
if ( ! is_admin () ) die ;
2012-08-01 12:43:46 +00:00
2012-11-05 16:50:24 +00:00
if ( ! current_user_can ( 'edit_products' ) ) wp_die ( __ ( 'You do not have sufficient permissions to access this page.' , 'woocommerce' ) );
2012-08-01 12:43:46 +00:00
2012-10-16 09:45:33 +00:00
if ( ! check_admin_referer ( 'woocommerce-feature-product' )) wp_die ( __ ( 'You have taken too long. Please go back and retry.' , 'woocommerce' ) );
2012-08-01 12:43:46 +00:00
2012-04-20 10:02:29 +00:00
$post_id = isset ( $_GET [ 'product_id' ] ) && ( int ) $_GET [ 'product_id' ] ? ( int ) $_GET [ 'product_id' ] : '' ;
2012-08-01 12:43:46 +00:00
2012-08-14 16:19:22 +00:00
if ( ! $post_id ) die ;
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
$post = get_post ( $post_id );
2012-08-01 12:43:46 +00:00
2012-08-14 16:19:22 +00:00
if ( ! $post || $post -> post_type !== 'product' ) die ;
2012-08-01 12:43:46 +00:00
2012-04-20 10:02:29 +00:00
$featured = get_post_meta ( $post -> ID , '_featured' , true );
2011-12-09 19:55:09 +00:00
2012-08-01 12:43:46 +00:00
if ( $featured == 'yes' )
2012-04-20 10:02:29 +00:00
update_post_meta ( $post -> ID , '_featured' , 'no' );
2012-08-01 12:43:46 +00:00
else
2012-04-20 10:02:29 +00:00
update_post_meta ( $post -> ID , '_featured' , 'yes' );
2012-08-01 12:43:46 +00:00
2012-04-20 10:02:29 +00:00
wp_safe_redirect ( remove_query_arg ( array ( 'trashed' , 'untrashed' , 'deleted' , 'ids' ), wp_get_referer () ) );
2011-12-09 19:55:09 +00:00
}
2012-04-20 10:02:29 +00:00
2011-12-09 19:55:09 +00:00
add_action ( 'wp_ajax_woocommerce-feature-product' , 'woocommerce_feature_product' );
2012-08-14 16:19:22 +00:00
2011-12-09 19:55:09 +00:00
/**
* Mark an order as complete
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
function woocommerce_mark_order_complete () {
2012-08-14 16:19:22 +00:00
if ( ! is_admin () ) die ;
2012-11-05 16:50:24 +00:00
if ( ! current_user_can ( 'edit_shop_orders' ) ) wp_die ( __ ( 'You do not have sufficient permissions to access this page.' , 'woocommerce' ) );
2012-10-16 09:45:33 +00:00
if ( ! check_admin_referer ( 'woocommerce-mark-order-complete' )) wp_die ( __ ( 'You have taken too long. Please go back and retry.' , 'woocommerce' ) );
2011-12-09 19:55:09 +00:00
$order_id = isset ( $_GET [ 'order_id' ]) && ( int ) $_GET [ 'order_id' ] ? ( int ) $_GET [ 'order_id' ] : '' ;
2012-08-14 16:19:22 +00:00
if ( ! $order_id ) die ;
2012-08-01 12:43:46 +00:00
2012-01-27 16:38:39 +00:00
$order = new WC_Order ( $order_id );
2011-12-09 19:55:09 +00:00
$order -> update_status ( 'completed' );
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
wp_safe_redirect ( wp_get_referer () );
}
add_action ( 'wp_ajax_woocommerce-mark-order-complete' , 'woocommerce_mark_order_complete' );
2012-08-14 16:19:22 +00:00
2011-12-09 19:55:09 +00:00
/**
* Mark an order as processing
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
function woocommerce_mark_order_processing () {
2012-08-14 16:19:22 +00:00
if ( ! is_admin () ) die ;
2012-11-05 16:50:24 +00:00
if ( ! current_user_can ( 'edit_shop_orders' ) ) wp_die ( __ ( 'You do not have sufficient permissions to access this page.' , 'woocommerce' ) );
2012-10-16 09:45:33 +00:00
if ( ! check_admin_referer ( 'woocommerce-mark-order-processing' )) wp_die ( __ ( 'You have taken too long. Please go back and retry.' , 'woocommerce' ) );
2011-12-09 19:55:09 +00:00
$order_id = isset ( $_GET [ 'order_id' ]) && ( int ) $_GET [ 'order_id' ] ? ( int ) $_GET [ 'order_id' ] : '' ;
2012-08-14 16:19:22 +00:00
if ( ! $order_id ) die ;
2012-08-01 12:43:46 +00:00
2012-01-27 16:38:39 +00:00
$order = new WC_Order ( $order_id );
2011-12-09 19:55:09 +00:00
$order -> update_status ( 'processing' );
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
wp_safe_redirect ( wp_get_referer () );
}
add_action ( 'wp_ajax_woocommerce-mark-order-processing' , 'woocommerce_mark_order_processing' );
2012-08-14 16:19:22 +00:00
2012-04-08 23:57:38 +00:00
/**
* Add a new attribute via ajax function
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2012-04-08 23:57:38 +00:00
*/
function woocommerce_add_new_attribute () {
2012-08-01 12:43:46 +00:00
2012-04-08 23:57:38 +00:00
check_ajax_referer ( 'add-attribute' , 'security' );
2012-08-01 12:43:46 +00:00
2013-01-16 15:00:11 +00:00
header ( 'Content-Type: application/json; charset=utf-8' );
2012-04-08 23:57:38 +00:00
$taxonomy = esc_attr ( $_POST [ 'taxonomy' ] );
$term = stripslashes ( $_POST [ 'term' ] );
2012-08-01 12:43:46 +00:00
2012-04-08 23:57:38 +00:00
if ( taxonomy_exists ( $taxonomy ) ) {
2012-08-01 12:43:46 +00:00
2012-04-08 23:57:38 +00:00
$result = wp_insert_term ( $term , $taxonomy );
2012-08-01 12:43:46 +00:00
2012-04-08 23:57:38 +00:00
if ( is_wp_error ( $result ) ) {
echo json_encode ( array (
'error' => $result -> get_error_message ()
));
} else {
echo json_encode ( array (
'term_id' => $result [ 'term_id' ],
'name' => $term ,
'slug' => sanitize_title ( $term ),
));
}
}
2012-08-01 12:43:46 +00:00
2012-04-08 23:57:38 +00:00
die ();
}
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_add_new_attribute' , 'woocommerce_add_new_attribute' );
2011-12-09 19:55:09 +00:00
/**
* Delete variation via ajax function
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
function woocommerce_remove_variation () {
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
check_ajax_referer ( 'delete-variation' , 'security' );
$variation_id = intval ( $_POST [ 'variation_id' ] );
$variation = get_post ( $variation_id );
2012-08-14 16:19:22 +00:00
if ( $variation && $variation -> post_type == " product_variation " )
wp_delete_post ( $variation_id );
2011-12-09 19:55:09 +00:00
die ();
}
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_remove_variation' , 'woocommerce_remove_variation' );
2012-01-06 15:57:24 +00:00
/**
* Delete variations via ajax function
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2012-01-06 15:57:24 +00:00
*/
function woocommerce_remove_variations () {
2012-08-01 12:43:46 +00:00
2012-01-06 15:57:24 +00:00
check_ajax_referer ( 'delete-variations' , 'security' );
$variation_ids = ( array ) $_POST [ 'variation_ids' ];
2012-08-14 16:19:22 +00:00
foreach ( $variation_ids as $variation_id ) {
2012-01-06 15:57:24 +00:00
$variation = get_post ( $variation_id );
2012-08-14 16:19:22 +00:00
if ( $variation && $variation -> post_type == " product_variation " )
wp_delete_post ( $variation_id );
}
2012-01-06 15:57:24 +00:00
die ();
}
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_remove_variations' , 'woocommerce_remove_variations' );
2012-11-26 17:27:55 +00:00
function woocommerce_save_attributes () {
global $woocommerce ;
2012-11-27 16:22:47 +00:00
2012-11-26 17:27:55 +00:00
check_ajax_referer ( 'save-attributes' , 'security' );
2013-03-10 16:55:53 +00:00
// Get post data
2012-11-26 17:27:55 +00:00
parse_str ( $_POST [ 'data' ], $data );
2013-03-10 16:55:53 +00:00
$post_id = absint ( $_POST [ 'post_id' ] );
2012-11-27 16:22:47 +00:00
2012-11-26 17:27:55 +00:00
// Save Attributes
$attributes = array ();
if ( isset ( $data [ 'attribute_names' ] ) ) {
2013-03-10 16:55:53 +00:00
$attribute_names = array_map ( 'stripslashes' , $data [ 'attribute_names' ] );
2013-03-10 17:01:23 +00:00
$attribute_values = $data [ 'attribute_values' ];
2012-11-27 16:22:47 +00:00
if ( isset ( $data [ 'attribute_visibility' ] ) )
2012-11-26 17:27:55 +00:00
$attribute_visibility = $data [ 'attribute_visibility' ];
2012-11-27 16:22:47 +00:00
if ( isset ( $data [ 'attribute_variation' ] ) )
2012-11-26 17:27:55 +00:00
$attribute_variation = $data [ 'attribute_variation' ];
2012-11-27 16:22:47 +00:00
2012-11-26 17:27:55 +00:00
$attribute_is_taxonomy = $data [ 'attribute_is_taxonomy' ];
$attribute_position = $data [ 'attribute_position' ];
$attribute_names_count = sizeof ( $attribute_names );
for ( $i = 0 ; $i < $attribute_names_count ; $i ++ ) {
2012-11-27 16:22:47 +00:00
if ( ! $attribute_names [ $i ] )
2012-11-26 17:27:55 +00:00
continue ;
$is_visible = isset ( $attribute_visibility [ $i ] ) ? 1 : 0 ;
$is_variation = isset ( $attribute_variation [ $i ] ) ? 1 : 0 ;
$is_taxonomy = $attribute_is_taxonomy [ $i ] ? 1 : 0 ;
if ( $is_taxonomy ) {
if ( isset ( $attribute_values [ $i ] ) ) {
2013-04-15 12:15:42 +00:00
// Select based attributes - Format values (posted values are slugs)
2012-11-26 17:27:55 +00:00
if ( is_array ( $attribute_values [ $i ] ) ) {
2013-04-15 12:15:42 +00:00
$values = array_map ( 'sanitize_title' , $attribute_values [ $i ] );
// Text based attributes - Posted values are term names - don't change to slugs
2012-11-26 17:27:55 +00:00
} else {
2013-10-24 18:36:22 +00:00
$values = array_map ( 'stripslashes' , array_map ( 'strip_tags' , explode ( WC_DELIMITER , $attribute_values [ $i ] ) ) );
2012-11-26 17:27:55 +00:00
}
// Remove empty items in the array
2013-08-26 10:21:11 +00:00
$values = array_filter ( $values , 'strlen' );
2012-11-26 17:27:55 +00:00
} else {
$values = array ();
}
// Update post terms
if ( taxonomy_exists ( $attribute_names [ $i ] ) )
wp_set_object_terms ( $post_id , $values , $attribute_names [ $i ] );
if ( $values ) {
// Add attribute to array, but don't set values
2013-04-16 14:39:07 +00:00
$attributes [ sanitize_title ( $attribute_names [ $i ] ) ] = array (
2013-03-07 19:34:29 +00:00
'name' => woocommerce_clean ( $attribute_names [ $i ] ),
2012-11-26 17:27:55 +00:00
'value' => '' ,
'position' => $attribute_position [ $i ],
'is_visible' => $is_visible ,
'is_variation' => $is_variation ,
'is_taxonomy' => $is_taxonomy
);
}
2012-12-18 07:08:16 +00:00
} elseif ( isset ( $attribute_values [ $i ] ) ) {
2012-11-26 17:27:55 +00:00
// Text based, separate by pipe
2013-10-24 18:36:22 +00:00
$values = implode ( ' ' . WC_DELIMITER . ' ' , array_map ( 'woocommerce_clean' , array_map ( 'stripslashes' , explode ( WC_DELIMITER , $attribute_values [ $i ] ) ) ) );
2012-11-26 17:27:55 +00:00
// Custom attribute - Add attribute to array and set the values
2013-04-16 14:39:07 +00:00
$attributes [ sanitize_title ( $attribute_names [ $i ] ) ] = array (
2013-03-07 19:34:29 +00:00
'name' => woocommerce_clean ( $attribute_names [ $i ] ),
2012-11-26 17:27:55 +00:00
'value' => $values ,
'position' => $attribute_position [ $i ],
'is_visible' => $is_visible ,
'is_variation' => $is_variation ,
'is_taxonomy' => $is_taxonomy
);
}
}
}
if ( ! function_exists ( 'attributes_cmp' ) ) {
function attributes_cmp ( $a , $b ) {
if ( $a [ 'position' ] == $b [ 'position' ] ) return 0 ;
return ( $a [ 'position' ] < $b [ 'position' ] ) ? - 1 : 1 ;
}
}
uasort ( $attributes , 'attributes_cmp' );
update_post_meta ( $post_id , '_product_attributes' , $attributes );
die ();
}
add_action ( 'wp_ajax_woocommerce_save_attributes' , 'woocommerce_save_attributes' );
2011-12-09 19:55:09 +00:00
/**
* Add variation via ajax function
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
function woocommerce_add_variation () {
2012-10-08 08:59:02 +00:00
global $woocommerce ;
2012-11-27 16:22:47 +00:00
2011-12-09 19:55:09 +00:00
check_ajax_referer ( 'add-variation' , 'security' );
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
$post_id = intval ( $_POST [ 'post_id' ] );
2012-10-08 08:59:02 +00:00
$loop = intval ( $_POST [ 'loop' ] );
2011-12-09 19:55:09 +00:00
$variation = array (
2012-10-08 08:59:02 +00:00
'post_title' => 'Product #' . $post_id . ' Variation' ,
'post_content' => '' ,
'post_status' => 'publish' ,
'post_author' => get_current_user_id (),
'post_parent' => $post_id ,
'post_type' => 'product_variation'
2011-12-09 19:55:09 +00:00
);
2012-11-27 16:22:47 +00:00
2011-12-09 19:55:09 +00:00
$variation_id = wp_insert_post ( $variation );
2012-11-27 16:22:47 +00:00
2012-11-27 10:33:58 +00:00
do_action ( 'woocommerce_create_product_variation' , $variation_id );
2012-11-27 16:22:47 +00:00
2012-10-08 08:59:02 +00:00
if ( $variation_id ) {
2012-11-27 16:22:47 +00:00
2012-10-08 08:59:02 +00:00
$variation_post_status = 'publish' ;
2012-12-19 21:01:15 +00:00
$variation_data = get_post_meta ( $variation_id );
2012-10-08 08:59:02 +00:00
$variation_data [ 'variation_post_id' ] = $variation_id ;
2012-11-27 16:22:47 +00:00
2012-10-08 08:59:02 +00:00
// Get attributes
$attributes = ( array ) maybe_unserialize ( get_post_meta ( $post_id , '_product_attributes' , true ) );
2012-11-27 16:22:47 +00:00
2012-10-08 08:59:02 +00:00
// Get tax classes
$tax_classes = array_filter ( array_map ( 'trim' , explode ( " \n " , get_option ( 'woocommerce_tax_classes' ))));
$tax_class_options = array ();
2012-10-16 09:45:33 +00:00
$tax_class_options [ 'parent' ] = __ ( 'Same as parent' , 'woocommerce' );
$tax_class_options [ '' ] = __ ( 'Standard' , 'woocommerce' );
2012-10-08 08:59:02 +00:00
if ( $tax_classes ) foreach ( $tax_classes as $class )
$tax_class_options [ sanitize_title ( $class )] = $class ;
2012-11-27 16:22:47 +00:00
2012-10-08 08:59:02 +00:00
// Get parent data
$parent_data = array (
'id' => $post_id ,
'attributes' => $attributes ,
'tax_class_options' => $tax_class_options ,
'sku' => get_post_meta ( $post_id , '_sku' , true ),
'weight' => get_post_meta ( $post_id , '_weight' , true ),
'length' => get_post_meta ( $post_id , '_length' , true ),
'width' => get_post_meta ( $post_id , '_width' , true ),
'height' => get_post_meta ( $post_id , '_height' , true ),
'tax_class' => get_post_meta ( $post_id , '_tax_class' , true )
);
2012-11-27 16:22:47 +00:00
2012-10-08 08:59:02 +00:00
if ( ! $parent_data [ 'weight' ] )
$parent_data [ 'weight' ] = '0.00' ;
2012-11-27 16:22:47 +00:00
2012-10-08 08:59:02 +00:00
if ( ! $parent_data [ 'length' ] )
$parent_data [ 'length' ] = '0' ;
2012-11-27 16:22:47 +00:00
2012-10-08 08:59:02 +00:00
if ( ! $parent_data [ 'width' ] )
$parent_data [ 'width' ] = '0' ;
2012-11-27 16:22:47 +00:00
2012-10-08 08:59:02 +00:00
if ( ! $parent_data [ 'height' ] )
$parent_data [ 'height' ] = '0' ;
2012-11-27 16:22:47 +00:00
2012-11-26 14:07:58 +00:00
$_tax_class = '' ;
2013-11-08 16:05:17 +00:00
$_downloadable_files = '' ;
2012-11-26 14:07:58 +00:00
$image_id = 0 ;
2013-02-28 04:25:33 +00:00
$variation = get_post ( $variation_id ); // Get the variation object
2012-11-27 16:22:47 +00:00
2013-08-06 10:41:20 +00:00
include ( 'includes/admin/post-types/meta-boxes/views/html-variation-admin.php' );
2012-10-08 08:59:02 +00:00
}
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
die ();
}
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_add_variation' , 'woocommerce_add_variation' );
2011-12-09 19:55:09 +00:00
/**
* Link all variations via ajax function
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
function woocommerce_link_all_variations () {
2012-08-25 10:09:53 +00:00
global $woocommerce ;
2012-10-15 11:12:31 +00:00
if ( ! defined ( 'WC_MAX_LINKED_VARIATIONS' ) ) {
define ( 'WC_MAX_LINKED_VARIATIONS' , 49 );
}
2012-11-27 16:22:47 +00:00
2011-12-09 19:55:09 +00:00
check_ajax_referer ( 'link-variations' , 'security' );
2012-08-01 12:43:46 +00:00
@ set_time_limit ( 0 );
2011-12-09 19:55:09 +00:00
$post_id = intval ( $_POST [ 'post_id' ] );
2012-08-01 12:43:46 +00:00
2012-10-15 11:12:31 +00:00
if ( ! $post_id ) die ();
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
$variations = array ();
2012-08-01 12:43:46 +00:00
2012-12-05 17:57:53 +00:00
$_product = get_product ( $post_id , array ( 'product_type' => 'variable' ) );
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// Put variation attributes into an array
2012-10-15 11:12:31 +00:00
foreach ( $_product -> get_attributes () as $attribute ) {
2012-08-01 12:43:46 +00:00
2012-10-15 11:12:31 +00:00
if ( ! $attribute [ 'is_variation' ] ) continue ;
2012-08-01 12:43:46 +00:00
2012-10-15 11:12:31 +00:00
$attribute_field_name = 'attribute_' . sanitize_title ( $attribute [ 'name' ] );
2012-08-01 12:43:46 +00:00
2012-10-15 11:12:31 +00:00
if ( $attribute [ 'is_taxonomy' ] ) {
2011-12-09 19:55:09 +00:00
$post_terms = wp_get_post_terms ( $post_id , $attribute [ 'name' ] );
$options = array ();
2012-10-15 11:12:31 +00:00
foreach ( $post_terms as $term ) {
2011-12-09 19:55:09 +00:00
$options [] = $term -> slug ;
2012-10-15 11:12:31 +00:00
}
} else {
2013-10-24 18:36:22 +00:00
$options = explode ( WC_DELIMITER , $attribute [ 'value' ] );
2012-10-15 11:12:31 +00:00
}
2012-08-01 12:43:46 +00:00
2013-04-12 09:16:26 +00:00
$options = array_map ( 'sanitize_title' , array_map ( 'trim' , $options ) );
2012-08-01 12:43:46 +00:00
2012-10-15 11:12:31 +00:00
$variations [ $attribute_field_name ] = $options ;
}
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// Quit out if none were found
2012-10-15 11:12:31 +00:00
if ( sizeof ( $variations ) == 0 ) die ();
2012-08-01 12:43:46 +00:00
2012-12-05 17:57:53 +00:00
// Get existing variations so we don't create duplicates
2011-12-09 19:55:09 +00:00
$available_variations = array ();
2012-08-01 12:43:46 +00:00
2012-10-15 11:12:31 +00:00
foreach ( $_product -> get_children () as $child_id ) {
2011-12-09 19:55:09 +00:00
$child = $_product -> get_child ( $child_id );
2012-10-15 11:12:31 +00:00
2012-11-21 18:39:51 +00:00
if ( ! empty ( $child -> variation_id ) ) {
2011-12-09 19:55:09 +00:00
$available_variations [] = $child -> get_variation_attributes ();
}
}
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// Created posts will all have the following data
$variation_post_data = array (
'post_title' => 'Product #' . $post_id . ' Variation' ,
'post_content' => '' ,
'post_status' => 'publish' ,
'post_author' => get_current_user_id (),
'post_parent' => $post_id ,
'post_type' => 'product_variation'
);
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// Now find all combinations and create posts
2012-10-15 11:12:31 +00:00
if ( ! function_exists ( 'array_cartesian' ) ) {
function array_cartesian ( $input ) {
2011-12-09 19:55:09 +00:00
$result = array ();
2012-08-01 12:43:46 +00:00
2012-10-15 11:12:31 +00:00
while ( list ( $key , $values ) = each ( $input ) ) {
2011-12-09 19:55:09 +00:00
// If a sub-array is empty, it doesn't affect the cartesian product
2012-10-15 11:12:31 +00:00
if ( empty ( $values ) ) {
2011-12-09 19:55:09 +00:00
continue ;
}
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// Special case: seeding the product array with the values from the first sub-array
2012-10-15 11:12:31 +00:00
if ( empty ( $result ) ) {
foreach ( $values as $value ) {
$result [] = array ( $key => $value );
2011-12-09 19:55:09 +00:00
}
}
else {
// Second and subsequent input sub-arrays work like this:
// 1. In each existing array inside $product, add an item with
// key == $key and value == first item in input sub-array
// 2. Then, for each remaining item in current input sub-array,
// add a copy of each existing array inside $product with
// key == $key and value == first item in current input sub-array
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// Store all items to be added to $product here; adding them on the spot
// inside the foreach will result in an infinite loop
$append = array ();
2012-10-15 11:12:31 +00:00
foreach ( $result as & $product ) {
2011-12-09 19:55:09 +00:00
// Do step 1 above. array_shift is not the most efficient, but it
// allows us to iterate over the rest of the items with a simple
// foreach, making the code short and familiar.
2012-10-15 11:12:31 +00:00
$product [ $key ] = array_shift ( $values );
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// $product is by reference (that's why the key we added above
// will appear in the end result), so make a copy of it here
$copy = $product ;
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// Do step 2 above.
2012-10-15 11:12:31 +00:00
foreach ( $values as $item ) {
$copy [ $key ] = $item ;
2011-12-09 19:55:09 +00:00
$append [] = $copy ;
}
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// Undo the side effecst of array_shift
2012-10-15 11:12:31 +00:00
array_unshift ( $values , $product [ $key ] );
2011-12-09 19:55:09 +00:00
}
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// Out of the foreach, we can add to $results now
2012-10-15 11:12:31 +00:00
$result = array_merge ( $result , $append );
2011-12-09 19:55:09 +00:00
}
}
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
return $result ;
}
2011-12-08 15:13:15 +00:00
}
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
$variation_ids = array ();
2012-01-12 11:09:54 +00:00
$added = 0 ;
2011-12-09 19:55:09 +00:00
$possible_variations = array_cartesian ( $variations );
2012-08-01 12:43:46 +00:00
2012-10-15 11:12:31 +00:00
foreach ( $possible_variations as $variation ) {
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// Check if variation already exists
2013-04-12 09:16:26 +00:00
if ( in_array ( $variation , $available_variations ) )
continue ;
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
$variation_id = wp_insert_post ( $variation_post_data );
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
$variation_ids [] = $variation_id ;
2012-08-01 12:43:46 +00:00
2012-10-15 11:12:31 +00:00
foreach ( $variation as $key => $value ) {
2011-12-09 19:55:09 +00:00
update_post_meta ( $variation_id , $key , $value );
2012-10-15 11:12:31 +00:00
}
2012-08-01 12:43:46 +00:00
2012-01-12 11:09:54 +00:00
$added ++ ;
2012-11-27 16:22:47 +00:00
2012-11-15 10:58:40 +00:00
do_action ( 'product_variation_linked' , $variation_id );
2012-08-01 12:43:46 +00:00
2013-04-12 09:16:26 +00:00
if ( $added > WC_MAX_LINKED_VARIATIONS )
break ;
2012-10-15 11:12:31 +00:00
}
2012-11-27 16:22:47 +00:00
2013-08-09 16:11:15 +00:00
wc_delete_product_transients ( $post_id );
2012-08-01 12:43:46 +00:00
2012-01-12 11:09:54 +00:00
echo $added ;
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
die ();
}
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_link_all_variations' , 'woocommerce_link_all_variations' );
2012-02-25 19:48:09 +00:00
/**
* Delete download permissions via ajax function
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2012-02-25 19:48:09 +00:00
*/
function woocommerce_revoke_access_to_download () {
2012-08-01 12:43:46 +00:00
2012-02-25 19:48:09 +00:00
check_ajax_referer ( 'revoke-access' , 'security' );
2012-08-01 12:43:46 +00:00
2012-02-25 19:48:09 +00:00
global $wpdb ;
2012-08-01 12:43:46 +00:00
2012-08-28 15:21:54 +00:00
$download_id = $_POST [ 'download_id' ];
2013-10-25 19:02:21 +00:00
$product_id = intval ( $_POST [ 'product_id' ] );
$order_id = intval ( $_POST [ 'order_id' ] );
2012-08-01 12:43:46 +00:00
2013-10-25 19:02:21 +00:00
$wpdb -> query ( $wpdb -> prepare ( " DELETE FROM { $wpdb -> prefix } woocommerce_downloadable_product_permissions WHERE order_id = %d AND product_id = %d AND download_id = %s; " , $order_id , $product_id , $download_id ) );
2012-02-25 19:48:09 +00:00
2013-09-10 11:26:31 +00:00
do_action ( 'woocommerce_ajax_revoke_access_to_product_download' , $download_id , $product_id , $order_id );
2012-08-01 12:43:46 +00:00
die ();
2012-02-25 19:48:09 +00:00
}
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_revoke_access_to_download' , 'woocommerce_revoke_access_to_download' );
2012-02-25 19:48:09 +00:00
/**
* Grant download permissions via ajax function
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2012-02-25 19:48:09 +00:00
*/
function woocommerce_grant_access_to_download () {
2012-08-01 12:43:46 +00:00
2012-02-25 19:48:09 +00:00
check_ajax_referer ( 'grant-access' , 'security' );
2012-08-01 12:43:46 +00:00
2012-02-25 19:48:09 +00:00
global $wpdb ;
2012-08-01 12:43:46 +00:00
2013-07-16 13:40:07 +00:00
$wpdb -> hide_errors ();
2012-02-25 19:48:09 +00:00
$order_id = intval ( $_POST [ 'order_id' ] );
2012-10-16 09:11:50 +00:00
$product_id = intval ( $_POST [ 'product_id' ] );
$loop = intval ( $_POST [ 'loop' ] );
$file_count = 0 ;
$order = new WC_Order ( $order_id );
2012-11-21 18:07:45 +00:00
$product = get_product ( $product_id );
2013-09-20 16:01:09 +00:00
$files = $product -> get_files ();
2012-08-01 12:43:46 +00:00
2013-07-16 13:40:07 +00:00
if ( ! $order -> billing_email )
2013-03-01 20:58:25 +00:00
die ();
2013-09-20 16:01:09 +00:00
if ( $files ) {
foreach ( $files as $download_id => $file ) {
2013-07-16 13:40:07 +00:00
if ( $inserted_id = woocommerce_downloadable_file_permission ( $download_id , $product_id , $order ) ) {
2012-11-27 16:22:47 +00:00
2013-07-16 13:40:07 +00:00
// insert complete - get inserted data
$download = $wpdb -> get_row ( $wpdb -> prepare ( " SELECT * FROM { $wpdb -> prefix } woocommerce_downloadable_product_permissions WHERE permission_id = %d " , $inserted_id ) );
2012-11-27 16:22:47 +00:00
2013-07-16 13:40:07 +00:00
$loop ++ ;
$file_count ++ ;
2012-11-27 16:22:47 +00:00
2013-08-06 10:41:20 +00:00
include ( 'includes/admin/post-types/meta-boxes/views/html-order-download-permission.php' );
2012-09-06 15:16:16 +00:00
}
2012-08-28 15:21:54 +00:00
}
2012-02-25 19:48:09 +00:00
}
2012-08-01 12:43:46 +00:00
die ();
2012-02-25 19:48:09 +00:00
}
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_grant_access_to_download' , 'woocommerce_grant_access_to_download' );
2011-12-09 19:55:09 +00:00
/**
* Get customer details via ajax
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
function woocommerce_get_customer_details () {
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
global $woocommerce ;
check_ajax_referer ( 'get-customer-details' , 'security' );
2013-01-16 15:00:11 +00:00
header ( 'Content-Type: application/json; charset=utf-8' );
2011-12-09 19:55:09 +00:00
$user_id = ( int ) trim ( stripslashes ( $_POST [ 'user_id' ]));
$type_to_load = esc_attr ( trim ( stripslashes ( $_POST [ 'type_to_load' ])));
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
$customer_data = array (
$type_to_load . '_first_name' => get_user_meta ( $user_id , $type_to_load . '_first_name' , true ),
$type_to_load . '_last_name' => get_user_meta ( $user_id , $type_to_load . '_last_name' , true ),
$type_to_load . '_company' => get_user_meta ( $user_id , $type_to_load . '_company' , true ),
$type_to_load . '_address_1' => get_user_meta ( $user_id , $type_to_load . '_address_1' , true ),
$type_to_load . '_address_2' => get_user_meta ( $user_id , $type_to_load . '_address_2' , true ),
$type_to_load . '_city' => get_user_meta ( $user_id , $type_to_load . '_city' , true ),
$type_to_load . '_postcode' => get_user_meta ( $user_id , $type_to_load . '_postcode' , true ),
$type_to_load . '_country' => get_user_meta ( $user_id , $type_to_load . '_country' , true ),
$type_to_load . '_state' => get_user_meta ( $user_id , $type_to_load . '_state' , true ),
$type_to_load . '_email' => get_user_meta ( $user_id , $type_to_load . '_email' , true ),
$type_to_load . '_phone' => get_user_meta ( $user_id , $type_to_load . '_phone' , true ),
);
2012-08-01 12:43:46 +00:00
2013-01-22 13:06:51 +00:00
$customer_data = apply_filters ( 'woocommerce_found_customer_details' , $customer_data );
2013-01-16 15:00:11 +00:00
echo json_encode ( $customer_data );
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// Quit out
die ();
}
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_get_customer_details' , 'woocommerce_get_customer_details' );
2011-12-09 19:55:09 +00:00
/**
* Add order item via ajax
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
2012-10-19 17:59:17 +00:00
function woocommerce_ajax_add_order_item () {
2012-01-13 21:25:39 +00:00
global $woocommerce , $wpdb ;
2011-12-09 19:55:09 +00:00
2012-10-18 17:56:28 +00:00
check_ajax_referer ( 'order-item' , 'security' );
2012-08-01 12:43:46 +00:00
2012-10-19 14:05:23 +00:00
$item_to_add = sanitize_text_field ( $_POST [ 'item_to_add' ] );
$order_id = absint ( $_POST [ 'order_id' ] );
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// Find the item
2012-10-19 14:05:23 +00:00
if ( ! is_numeric ( $item_to_add ) )
2011-12-08 15:13:15 +00:00
die ();
2012-11-27 16:22:47 +00:00
2012-10-19 14:05:23 +00:00
$post = get_post ( $item_to_add );
2012-08-01 12:43:46 +00:00
2012-10-19 14:05:23 +00:00
if ( ! $post || ( $post -> post_type !== 'product' && $post -> post_type !== 'product_variation' ) )
die ();
2012-11-27 16:22:47 +00:00
2012-11-21 18:07:45 +00:00
$_product = get_product ( $post -> ID );
2012-11-27 16:22:47 +00:00
2012-10-23 16:41:42 +00:00
$order = new WC_Order ( $order_id );
2012-10-19 14:05:23 +00:00
$class = 'new_row' ;
2012-11-27 16:22:47 +00:00
2012-10-19 17:59:17 +00:00
// Set values
$item = array ();
2012-11-27 16:22:47 +00:00
2012-10-19 17:59:17 +00:00
$item [ 'product_id' ] = $_product -> id ;
$item [ 'variation_id' ] = isset ( $_product -> variation_id ) ? $_product -> variation_id : '' ;
2012-10-23 16:41:42 +00:00
$item [ 'name' ] = $_product -> get_title ();
$item [ 'tax_class' ] = $_product -> get_tax_class ();
$item [ 'qty' ] = 1 ;
2013-11-12 17:43:30 +00:00
$item [ 'line_subtotal' ] = woocommerce_format_decimal ( $_product -> get_price_excluding_tax () );
2012-10-19 17:59:17 +00:00
$item [ 'line_subtotal_tax' ] = '' ;
2013-11-12 17:43:30 +00:00
$item [ 'line_total' ] = woocommerce_format_decimal ( $_product -> get_price_excluding_tax () );
2012-10-19 17:59:17 +00:00
$item [ 'line_tax' ] = '' ;
2012-11-27 16:22:47 +00:00
2012-10-23 16:41:42 +00:00
// Add line item
$item_id = woocommerce_add_order_item ( $order_id , array (
'order_item_name' => $item [ 'name' ],
'order_item_type' => 'line_item'
) );
2012-11-27 16:22:47 +00:00
2012-10-23 16:41:42 +00:00
// Add line item meta
if ( $item_id ) {
woocommerce_add_order_item_meta ( $item_id , '_qty' , $item [ 'qty' ] );
woocommerce_add_order_item_meta ( $item_id , '_tax_class' , $item [ 'tax_class' ] );
woocommerce_add_order_item_meta ( $item_id , '_product_id' , $item [ 'product_id' ] );
woocommerce_add_order_item_meta ( $item_id , '_variation_id' , $item [ 'variation_id' ] );
woocommerce_add_order_item_meta ( $item_id , '_line_subtotal' , $item [ 'line_subtotal' ] );
woocommerce_add_order_item_meta ( $item_id , '_line_subtotal_tax' , $item [ 'line_subtotal_tax' ] );
woocommerce_add_order_item_meta ( $item_id , '_line_total' , $item [ 'line_total' ] );
woocommerce_add_order_item_meta ( $item_id , '_line_tax' , $item [ 'line_tax' ] );
}
2012-11-27 16:22:47 +00:00
2013-04-17 15:15:28 +00:00
do_action ( 'woocommerce_ajax_add_order_item_meta' , $item_id , $item );
2013-08-17 00:40:51 +00:00
$item = apply_filters ( 'woocommerce_ajax_order_item' , $item , $item_id );
2013-08-06 10:41:20 +00:00
include ( 'includes/admin/post-types/meta-boxes/views/html-order-item.php' );
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// Quit out
die ();
2012-01-13 21:25:39 +00:00
}
2012-10-19 17:59:17 +00:00
add_action ( 'wp_ajax_woocommerce_add_order_item' , 'woocommerce_ajax_add_order_item' );
2012-08-14 16:19:22 +00:00
2012-11-12 13:41:54 +00:00
/**
* Add order fee via ajax
*
* @ access public
* @ return void
*/
function woocommerce_ajax_add_order_fee () {
global $woocommerce ;
check_ajax_referer ( 'order-item' , 'security' );
$order_id = absint ( $_POST [ 'order_id' ] );
$order = new WC_Order ( $order_id );
2012-11-27 16:22:47 +00:00
2012-11-12 13:41:54 +00:00
// Add line item
$item_id = woocommerce_add_order_item ( $order_id , array (
'order_item_name' => '' ,
'order_item_type' => 'fee'
) );
2012-11-27 16:22:47 +00:00
2012-11-12 13:41:54 +00:00
// Add line item meta
if ( $item_id ) {
woocommerce_add_order_item_meta ( $item_id , '_tax_class' , '' );
woocommerce_add_order_item_meta ( $item_id , '_line_total' , '' );
woocommerce_add_order_item_meta ( $item_id , '_line_tax' , '' );
}
2012-11-27 16:22:47 +00:00
2013-08-15 15:49:09 +00:00
include ( 'includes/admin/post-types/meta-boxes/views/html-order-fee.php' );
2012-11-12 13:41:54 +00:00
// Quit out
die ();
}
add_action ( 'wp_ajax_woocommerce_add_order_fee' , 'woocommerce_ajax_add_order_fee' );
2012-11-12 17:15:54 +00:00
2012-10-19 14:05:23 +00:00
/**
2012-10-19 17:59:17 +00:00
* woocommerce_ajax_remove_order_item function .
2012-11-27 16:22:47 +00:00
*
2012-10-19 14:05:23 +00:00
* @ access public
* @ return void
*/
2012-10-19 17:59:17 +00:00
function woocommerce_ajax_remove_order_item () {
2012-10-19 14:05:23 +00:00
global $woocommerce , $wpdb ;
check_ajax_referer ( 'order-item' , 'security' );
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
$order_item_ids = $_POST [ 'order_item_ids' ];
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
if ( sizeof ( $order_item_ids ) > 0 ) {
foreach ( $order_item_ids as $id ) {
woocommerce_delete_order_item ( absint ( $id ) );
}
}
2012-11-27 16:22:47 +00:00
2012-10-19 14:05:23 +00:00
die ();
}
2012-10-19 17:59:17 +00:00
add_action ( 'wp_ajax_woocommerce_remove_order_item' , 'woocommerce_ajax_remove_order_item' );
2012-10-19 14:05:23 +00:00
2012-11-13 13:26:30 +00:00
2012-11-12 12:22:35 +00:00
/**
* woocommerce_ajax_reduce_order_item_stock function .
2012-11-27 16:22:47 +00:00
*
2012-11-12 12:22:35 +00:00
* @ access public
* @ return void
*/
function woocommerce_ajax_reduce_order_item_stock () {
global $woocommerce , $wpdb ;
check_ajax_referer ( 'order-item' , 'security' );
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
$order_id = absint ( $_POST [ 'order_id' ] );
2013-02-27 12:50:02 +00:00
$order_item_ids = isset ( $_POST [ 'order_item_ids' ] ) ? $_POST [ 'order_item_ids' ] : array ();
$order_item_qty = isset ( $_POST [ 'order_item_qty' ] ) ? $_POST [ 'order_item_qty' ] : array ();
2012-11-12 12:22:35 +00:00
$order = new WC_Order ( $order_id );
$order_items = $order -> get_items ();
$return = array ();
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
if ( $order && ! empty ( $order_items ) && sizeof ( $order_item_ids ) > 0 ) {
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
foreach ( $order_items as $item_id => $order_item ) {
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
// Only reduce checked items
if ( ! in_array ( $item_id , $order_item_ids ) )
continue ;
$_product = $order -> get_product_from_item ( $order_item );
if ( $_product -> exists () && $_product -> managing_stock () && isset ( $order_item_qty [ $item_id ] ) && $order_item_qty [ $item_id ] > 0 ) {
$old_stock = $_product -> stock ;
2013-03-18 05:38:22 +00:00
$stock_change = apply_filters ( 'woocommerce_reduce_order_stock_quantity' , $order_item_qty [ $item_id ], $item_id );
$new_quantity = $_product -> reduce_stock ( $stock_change );
2012-11-27 16:22:47 +00:00
2012-11-12 16:08:05 +00:00
$return [] = sprintf ( __ ( 'Item #%s stock reduced from %s to %s.' , 'woocommerce' ), $order_item [ 'product_id' ], $old_stock , $new_quantity );
$order -> add_order_note ( sprintf ( __ ( 'Item #%s stock reduced from %s to %s.' , 'woocommerce' ), $order_item [ 'product_id' ], $old_stock , $new_quantity ) );
2012-11-12 12:22:35 +00:00
$order -> send_stock_notifications ( $_product , $new_quantity , $order_item_qty [ $item_id ] );
}
2012-11-27 16:22:47 +00:00
}
2012-11-12 12:22:35 +00:00
do_action ( 'woocommerce_reduce_order_stock' , $order );
2012-11-27 16:22:47 +00:00
2013-02-27 12:50:02 +00:00
if ( empty ( $return ) )
$return [] = __ ( 'No products had their stock reduced - they may not have stock management enabled.' , 'woocommerce' );
2012-11-12 12:22:35 +00:00
echo implode ( ', ' , $return );
}
2012-11-27 16:22:47 +00:00
die ();
2012-11-12 12:22:35 +00:00
}
add_action ( 'wp_ajax_woocommerce_reduce_order_item_stock' , 'woocommerce_ajax_reduce_order_item_stock' );
/**
* woocommerce_ajax_increase_order_item_stock function .
2012-11-27 16:22:47 +00:00
*
2012-11-12 12:22:35 +00:00
* @ access public
* @ return void
*/
function woocommerce_ajax_increase_order_item_stock () {
global $woocommerce , $wpdb ;
check_ajax_referer ( 'order-item' , 'security' );
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
$order_id = absint ( $_POST [ 'order_id' ] );
2013-02-27 12:50:02 +00:00
$order_item_ids = isset ( $_POST [ 'order_item_ids' ] ) ? $_POST [ 'order_item_ids' ] : array ();
$order_item_qty = isset ( $_POST [ 'order_item_qty' ] ) ? $_POST [ 'order_item_qty' ] : array ();
2012-11-12 12:22:35 +00:00
$order = new WC_Order ( $order_id );
$order_items = $order -> get_items ();
$return = array ();
if ( $order && ! empty ( $order_items ) && sizeof ( $order_item_ids ) > 0 ) {
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
foreach ( $order_items as $item_id => $order_item ) {
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
// Only reduce checked items
if ( ! in_array ( $item_id , $order_item_ids ) )
continue ;
$_product = $order -> get_product_from_item ( $order_item );
if ( $_product -> exists () && $_product -> managing_stock () && isset ( $order_item_qty [ $item_id ] ) && $order_item_qty [ $item_id ] > 0 ) {
$old_stock = $_product -> stock ;
2013-03-18 05:38:22 +00:00
$stock_change = apply_filters ( 'woocommerce_restore_order_stock_quantity' , $order_item_qty [ $item_id ], $item_id );
$new_quantity = $_product -> increase_stock ( $stock_change );
2012-11-27 16:22:47 +00:00
2012-11-12 16:08:05 +00:00
$return [] = sprintf ( __ ( 'Item #%s stock increased from %s to %s.' , 'woocommerce' ), $order_item [ 'product_id' ], $old_stock , $new_quantity );
$order -> add_order_note ( sprintf ( __ ( 'Item #%s stock increased from %s to %s.' , 'woocommerce' ), $order_item [ 'product_id' ], $old_stock , $new_quantity ) );
2012-11-12 12:22:35 +00:00
}
2012-11-27 16:22:47 +00:00
}
2012-11-12 12:22:35 +00:00
do_action ( 'woocommerce_restore_order_stock' , $order );
2012-11-27 16:22:47 +00:00
2013-02-27 12:50:02 +00:00
if ( empty ( $return ) )
$return [] = __ ( 'No products had their stock increased - they may not have stock management enabled.' , 'woocommerce' );
2012-11-12 12:22:35 +00:00
echo implode ( ', ' , $return );
}
2012-11-27 16:22:47 +00:00
die ();
2012-11-12 12:22:35 +00:00
}
add_action ( 'wp_ajax_woocommerce_increase_order_item_stock' , 'woocommerce_ajax_increase_order_item_stock' );
2012-10-19 14:05:23 +00:00
2012-10-18 17:56:28 +00:00
/**
2012-10-19 17:59:17 +00:00
* woocommerce_ajax_add_order_item_meta function .
2012-11-27 16:22:47 +00:00
*
2012-10-18 17:56:28 +00:00
* @ access public
* @ return void
*/
2012-10-19 17:59:17 +00:00
function woocommerce_ajax_add_order_item_meta () {
2012-10-18 17:56:28 +00:00
global $woocommerce , $wpdb ;
check_ajax_referer ( 'order-item' , 'security' );
2012-11-27 16:22:47 +00:00
$meta_id = woocommerce_add_order_item_meta ( absint ( $_POST [ 'order_item_id' ] ), __ ( 'Name' , 'woocommerce' ), __ ( 'Value' , 'woocommerce' ) );
2012-10-18 17:56:28 +00:00
if ( $meta_id ) {
2012-11-27 16:22:47 +00:00
2013-11-08 17:03:40 +00:00
echo '<tr data-meta_id="' . $meta_id . '"><td><input type="text" name="meta_key[' . $meta_id . ']" /><textarea name="meta_value[' . $meta_id . ']"></textarea></td><td width="1%"><button class="remove_order_item_meta button">×</button></td></tr>' ;
2012-11-27 16:22:47 +00:00
2012-10-18 17:56:28 +00:00
}
2012-11-27 16:22:47 +00:00
2012-10-18 17:56:28 +00:00
die ();
}
2012-10-19 17:59:17 +00:00
add_action ( 'wp_ajax_woocommerce_add_order_item_meta' , 'woocommerce_ajax_add_order_item_meta' );
2012-10-18 17:56:28 +00:00
/**
2012-10-19 17:59:17 +00:00
* woocommerce_ajax_remove_order_item_meta function .
2012-11-27 16:22:47 +00:00
*
2012-10-18 17:56:28 +00:00
* @ access public
* @ return void
*/
2012-10-19 17:59:17 +00:00
function woocommerce_ajax_remove_order_item_meta () {
2012-10-18 17:56:28 +00:00
global $woocommerce , $wpdb ;
check_ajax_referer ( 'order-item' , 'security' );
2012-11-27 16:22:47 +00:00
2012-10-18 17:56:28 +00:00
$meta_id = absint ( $_POST [ 'meta_id' ] );
2012-11-27 16:22:47 +00:00
2012-10-18 17:56:28 +00:00
$wpdb -> query ( $wpdb -> prepare ( " DELETE FROM { $wpdb -> prefix } woocommerce_order_itemmeta WHERE meta_id = %d " , $meta_id ) );
2012-11-27 16:22:47 +00:00
2012-10-18 17:56:28 +00:00
die ();
}
2012-10-19 17:59:17 +00:00
add_action ( 'wp_ajax_woocommerce_remove_order_item_meta' , 'woocommerce_ajax_remove_order_item_meta' );
2012-08-14 16:19:22 +00:00
2012-01-13 21:25:39 +00:00
/**
2012-01-22 15:49:41 +00:00
* Calc line tax
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2012-01-13 21:25:39 +00:00
*/
function woocommerce_calc_line_taxes () {
2012-11-13 14:54:34 +00:00
global $woocommerce , $wpdb ;
2012-01-13 21:25:39 +00:00
check_ajax_referer ( 'calc-totals' , 'security' );
2012-08-01 12:43:46 +00:00
2013-01-16 15:00:11 +00:00
header ( 'Content-Type: application/json; charset=utf-8' );
2012-01-27 16:38:39 +00:00
$tax = new WC_Tax ();
2012-08-01 12:43:46 +00:00
2012-11-12 18:53:40 +00:00
$taxes = $tax_rows = $item_taxes = $shipping_taxes = array ();
2012-11-27 16:22:47 +00:00
2012-11-13 14:54:34 +00:00
$order_id = absint ( $_POST [ 'order_id' ] );
2013-09-13 15:34:37 +00:00
$order = new WC_Order ( $order_id );
2012-06-21 12:06:48 +00:00
$country = strtoupper ( esc_attr ( $_POST [ 'country' ] ) );
$state = strtoupper ( esc_attr ( $_POST [ 'state' ] ) );
$postcode = strtoupper ( esc_attr ( $_POST [ 'postcode' ] ) );
2012-09-23 16:16:39 +00:00
$city = sanitize_title ( esc_attr ( $_POST [ 'city' ] ) );
2012-08-01 12:43:46 +00:00
2012-11-12 18:53:40 +00:00
$items = $_POST [ 'items' ];
$shipping = $_POST [ 'shipping' ];
2012-11-13 14:59:28 +00:00
$item_tax = 0 ;
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
// Calculate sales tax first
if ( sizeof ( $items ) > 0 ) {
foreach ( $items as $item_id => $item ) {
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
$item_id = absint ( $item_id );
2013-11-12 17:43:30 +00:00
$line_subtotal = isset ( $item [ 'line_subtotal' ] ) ? woocommerce_format_decimal ( $item [ 'line_subtotal' ] ) : 0 ;
$line_total = woocommerce_format_decimal ( $item [ 'line_total' ] );
$tax_class = sanitize_text_field ( $item [ 'tax_class' ] );
2013-09-13 15:34:37 +00:00
$product_id = $order -> get_item_meta ( $item_id , '_product_id' , true );
2012-11-27 16:22:47 +00:00
if ( ! $item_id || $tax_class == '0' )
2012-11-12 18:53:40 +00:00
continue ;
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
// Get product details
2013-09-13 15:34:37 +00:00
if ( get_post_type ( $product_id ) == 'product' ) {
$_product = get_product ( $product_id );
2012-11-12 18:53:40 +00:00
$item_tax_status = $_product -> get_tax_status ();
} else {
$item_tax_status = 'taxable' ;
}
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
// Only calc if taxable
if ( $item_tax_status == 'taxable' ) {
$tax_rates = $tax -> find_rates ( array (
2012-11-27 16:22:47 +00:00
'country' => $country ,
'state' => $state ,
'postcode' => $postcode ,
2012-11-12 18:53:40 +00:00
'city' => $city ,
2012-11-27 16:22:47 +00:00
'tax_class' => $tax_class
2012-11-12 18:53:40 +00:00
) );
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
$line_subtotal_taxes = $tax -> calc_tax ( $line_subtotal , $tax_rates , false );
2013-10-24 15:16:39 +00:00
$line_taxes = $tax -> calc_tax ( $line_total , $tax_rates , false );
$line_subtotal_tax = array_sum ( $line_subtotal_taxes );
$line_tax = array_sum ( $line_taxes );
2012-12-13 01:23:35 +00:00
2012-11-27 16:22:47 +00:00
if ( $line_subtotal_tax < 0 )
2012-11-12 18:53:40 +00:00
$line_subtotal_tax = 0 ;
2012-11-27 16:22:47 +00:00
if ( $line_tax < 0 )
2012-11-12 18:53:40 +00:00
$line_tax = 0 ;
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
$item_taxes [ $item_id ] = array (
2013-11-12 17:43:30 +00:00
'line_subtotal_tax' => wc_format_localized_price ( $line_subtotal_tax ),
'line_tax' => wc_format_localized_price ( $line_tax )
2012-11-12 18:53:40 +00:00
);
2012-11-27 16:22:47 +00:00
2012-11-13 14:59:28 +00:00
$item_tax += $line_tax ;
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
// Sum the item taxes
foreach ( array_keys ( $taxes + $line_taxes ) as $key )
$taxes [ $key ] = ( isset ( $line_taxes [ $key ] ) ? $line_taxes [ $key ] : 0 ) + ( isset ( $taxes [ $key ] ) ? $taxes [ $key ] : 0 );
}
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
}
2012-11-12 14:34:10 +00:00
}
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
// Now calculate shipping tax
$matched_tax_rates = array ();
$tax_rates = $tax -> find_rates ( array (
2012-11-27 16:22:47 +00:00
'country' => $country ,
'state' => $state ,
'postcode' => $postcode ,
2012-11-12 18:53:40 +00:00
'city' => $city ,
'tax_class' => ''
) );
2012-08-01 12:43:46 +00:00
2012-11-27 16:22:47 +00:00
if ( $tax_rates )
2012-11-12 18:53:40 +00:00
foreach ( $tax_rates as $key => $rate )
if ( isset ( $rate [ 'shipping' ] ) && $rate [ 'shipping' ] == 'yes' )
$matched_tax_rates [ $key ] = $rate ;
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
$shipping_taxes = $tax -> calc_shipping_tax ( $shipping , $matched_tax_rates );
2012-12-13 01:23:35 +00:00
$shipping_tax = $tax -> round ( array_sum ( $shipping_taxes ) );
2012-08-01 12:43:46 +00:00
2012-11-13 14:54:34 +00:00
// Remove old tax rows
$wpdb -> query ( $wpdb -> prepare ( " DELETE FROM { $wpdb -> prefix } woocommerce_order_itemmeta WHERE order_item_id IN ( SELECT order_item_id FROM { $wpdb -> prefix } woocommerce_order_items WHERE order_id = %d AND order_item_type = 'tax' ) " , $order_id ) );
2012-11-27 16:22:47 +00:00
2012-11-13 14:54:34 +00:00
$wpdb -> query ( $wpdb -> prepare ( " DELETE FROM { $wpdb -> prefix } woocommerce_order_items WHERE order_id = %d AND order_item_type = 'tax' " , $order_id ) );
2012-12-06 19:49:00 +00:00
// Get tax rates
$rates = $wpdb -> get_results ( " SELECT tax_rate_id, tax_rate_country, tax_rate_state, tax_rate_name, tax_rate_priority FROM { $wpdb -> prefix } woocommerce_tax_rates ORDER BY tax_rate_name " );
$tax_codes = array ();
foreach ( $rates as $rate ) {
$code = array ();
$code [] = $rate -> tax_rate_country ;
$code [] = $rate -> tax_rate_state ;
$code [] = $rate -> tax_rate_name ? sanitize_title ( $rate -> tax_rate_name ) : 'TAX' ;
$code [] = absint ( $rate -> tax_rate_priority );
$tax_codes [ $rate -> tax_rate_id ] = strtoupper ( implode ( '-' , array_filter ( $code ) ) );
}
2012-11-12 18:53:40 +00:00
// Now merge to keep tax rows
2012-11-13 14:54:34 +00:00
ob_start ();
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
foreach ( array_keys ( $taxes + $shipping_taxes ) as $key ) {
2012-08-01 12:43:46 +00:00
2012-11-13 14:54:34 +00:00
$item = array ();
2012-12-13 01:23:35 +00:00
$item [ 'rate_id' ] = $key ;
2012-12-06 19:49:00 +00:00
$item [ 'name' ] = $tax_codes [ $key ];
$item [ 'label' ] = $tax -> get_rate_label ( $key );
2012-11-13 14:54:34 +00:00
$item [ 'compound' ] = $tax -> is_compound ( $key ) ? 1 : 0 ;
2013-11-12 17:43:30 +00:00
$item [ 'tax_amount' ] = woocommerce_format_decimal ( isset ( $taxes [ $key ] ) ? $taxes [ $key ] : 0 );
$item [ 'shipping_tax_amount' ] = woocommerce_format_decimal ( isset ( $shipping_taxes [ $key ] ) ? $shipping_taxes [ $key ] : 0 );
2012-11-13 14:54:34 +00:00
2012-12-06 19:49:00 +00:00
if ( ! $item [ 'label' ] )
$item [ 'label' ] = $woocommerce -> countries -> tax_or_vat ();
2012-11-13 14:54:34 +00:00
// Add line item
$item_id = woocommerce_add_order_item ( $order_id , array (
'order_item_name' => $item [ 'name' ],
'order_item_type' => 'tax'
) );
2012-11-27 16:22:47 +00:00
2012-11-13 14:54:34 +00:00
// Add line item meta
if ( $item_id ) {
2012-12-13 01:23:35 +00:00
woocommerce_add_order_item_meta ( $item_id , 'rate_id' , $item [ 'rate_id' ] );
2012-12-06 19:49:00 +00:00
woocommerce_add_order_item_meta ( $item_id , 'label' , $item [ 'label' ] );
2012-11-13 14:54:34 +00:00
woocommerce_add_order_item_meta ( $item_id , 'compound' , $item [ 'compound' ] );
woocommerce_add_order_item_meta ( $item_id , 'tax_amount' , $item [ 'tax_amount' ] );
woocommerce_add_order_item_meta ( $item_id , 'shipping_tax_amount' , $item [ 'shipping_tax_amount' ] );
}
2012-11-27 16:22:47 +00:00
2013-08-06 10:41:20 +00:00
include ( 'includes/admin/post-types/meta-boxes/views/html-order-tax.php' );
2012-11-12 18:53:40 +00:00
}
2012-11-27 16:22:47 +00:00
2012-11-13 14:54:34 +00:00
$tax_row_html = ob_get_clean ();
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
// Return
2012-06-21 12:06:48 +00:00
echo json_encode ( array (
2012-11-13 14:59:28 +00:00
'item_tax' => $item_tax ,
2012-11-12 18:53:40 +00:00
'item_taxes' => $item_taxes ,
'shipping_tax' => $shipping_tax ,
2012-11-13 14:54:34 +00:00
'tax_row_html' => $tax_row_html
2012-06-21 12:06:48 +00:00
) );
2012-08-01 12:43:46 +00:00
2012-01-14 01:23:16 +00:00
// Quit out
die ();
}
2012-01-13 21:25:39 +00:00
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_calc_line_taxes' , 'woocommerce_calc_line_taxes' );
2011-12-09 19:55:09 +00:00
/**
* Add order note via ajax
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
function woocommerce_add_order_note () {
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
global $woocommerce ;
check_ajax_referer ( 'add-order-note' , 'security' );
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
$post_id = ( int ) $_POST [ 'post_id' ];
2012-12-11 17:10:35 +00:00
$note = wp_kses_post ( trim ( stripslashes ( $_POST [ 'note' ] ) ) );
2011-12-09 19:55:09 +00:00
$note_type = $_POST [ 'note_type' ];
2012-08-01 12:43:46 +00:00
2012-06-20 17:19:15 +00:00
$is_customer_note = $note_type == 'customer' ? 1 : 0 ;
2012-08-01 12:43:46 +00:00
2012-06-20 17:19:15 +00:00
if ( $post_id > 0 ) {
2012-01-27 16:38:39 +00:00
$order = new WC_Order ( $post_id );
2011-12-09 19:55:09 +00:00
$comment_id = $order -> add_order_note ( $note , $is_customer_note );
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
echo '<li rel="' . $comment_id . '" class="note ' ;
if ( $is_customer_note ) echo 'customer-note' ;
echo '"><div class="note_content">' ;
2012-06-20 17:19:15 +00:00
echo wpautop ( wptexturize ( $note ) );
2012-10-16 09:45:33 +00:00
echo '</div><p class="meta"><a href="#" class="delete_note">' . __ ( 'Delete note' , 'woocommerce' ) . '</a></p>' ;
2011-12-09 19:55:09 +00:00
echo '</li>' ;
2012-08-01 12:43:46 +00:00
2012-06-20 17:19:15 +00:00
}
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// Quit out
die ();
}
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_add_order_note' , 'woocommerce_add_order_note' );
2011-12-09 19:55:09 +00:00
/**
* Delete order note via ajax
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
function woocommerce_delete_order_note () {
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
global $woocommerce ;
check_ajax_referer ( 'delete-order-note' , 'security' );
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
$note_id = ( int ) $_POST [ 'note_id' ];
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
if ( $note_id > 0 ) :
wp_delete_comment ( $note_id );
endif ;
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
// Quit out
die ();
}
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_delete_order_note' , 'woocommerce_delete_order_note' );
2012-03-06 15:46:53 +00:00
/**
* Search for products and return json
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ param string $x ( default : '' )
* @ param string $post_types ( default : array ( 'product' ))
* @ return void
2012-03-06 15:46:53 +00:00
*/
2012-03-12 17:26:11 +00:00
function woocommerce_json_search_products ( $x = '' , $post_types = array ( 'product' ) ) {
2012-03-06 15:46:53 +00:00
check_ajax_referer ( 'search-products' , 'security' );
2012-08-01 12:43:46 +00:00
2013-01-16 15:00:11 +00:00
header ( 'Content-Type: application/json; charset=utf-8' );
2012-03-06 15:46:53 +00:00
$term = ( string ) urldecode ( stripslashes ( strip_tags ( $_GET [ 'term' ])));
2012-08-01 12:43:46 +00:00
2012-03-06 15:46:53 +00:00
if ( empty ( $term )) die ();
2012-08-01 12:43:46 +00:00
2012-04-16 12:52:28 +00:00
if ( is_numeric ( $term ) ) {
2012-08-01 12:43:46 +00:00
2012-03-06 15:46:53 +00:00
$args = array (
2012-03-12 17:26:11 +00:00
'post_type' => $post_types ,
2012-03-06 15:46:53 +00:00
'post_status' => 'publish' ,
'posts_per_page' => - 1 ,
'post__in' => array ( 0 , $term ),
'fields' => 'ids'
);
2012-08-01 12:43:46 +00:00
2012-04-16 12:52:28 +00:00
$args2 = array (
'post_type' => $post_types ,
'post_status' => 'publish' ,
'posts_per_page' => - 1 ,
'post_parent' => $term ,
'fields' => 'ids'
);
2012-08-01 12:43:46 +00:00
2012-08-03 17:19:35 +00:00
$args3 = array (
'post_type' => $post_types ,
'post_status' => 'publish' ,
'posts_per_page' => - 1 ,
'meta_query' => array (
array (
'key' => '_sku' ,
'value' => $term ,
'compare' => 'LIKE'
)
),
'fields' => 'ids'
);
$posts = array_unique ( array_merge ( get_posts ( $args ), get_posts ( $args2 ), get_posts ( $args3 ) ));
2012-08-01 12:43:46 +00:00
2012-03-06 15:46:53 +00:00
} else {
2012-08-01 12:43:46 +00:00
2012-03-06 15:46:53 +00:00
$args = array (
2012-03-12 17:26:11 +00:00
'post_type' => $post_types ,
2012-03-06 15:46:53 +00:00
'post_status' => 'publish' ,
'posts_per_page' => - 1 ,
's' => $term ,
'fields' => 'ids'
);
2012-08-01 12:43:46 +00:00
2012-03-06 15:46:53 +00:00
$args2 = array (
2012-03-12 17:26:11 +00:00
'post_type' => $post_types ,
2012-03-06 15:46:53 +00:00
'post_status' => 'publish' ,
'posts_per_page' => - 1 ,
'meta_query' => array (
array (
'key' => '_sku' ,
'value' => $term ,
'compare' => 'LIKE'
)
),
'fields' => 'ids'
);
2012-08-01 12:43:46 +00:00
2012-03-06 15:46:53 +00:00
$posts = array_unique ( array_merge ( get_posts ( $args ), get_posts ( $args2 ) ));
2012-08-01 12:43:46 +00:00
2012-03-06 15:46:53 +00:00
}
2012-08-01 12:43:46 +00:00
2012-03-06 15:46:53 +00:00
$found_products = array ();
2013-01-17 11:23:11 +00:00
if ( $posts ) foreach ( $posts as $post ) {
2012-08-01 12:43:46 +00:00
2013-01-17 11:23:11 +00:00
$product = get_product ( $post );
2012-08-01 12:43:46 +00:00
2013-03-27 07:58:48 +00:00
$found_products [ $post ] = $product -> get_formatted_name ();
2012-08-01 12:43:46 +00:00
2012-03-06 15:46:53 +00:00
}
2012-08-01 12:43:46 +00:00
2013-01-17 11:23:11 +00:00
$found_products = apply_filters ( 'woocommerce_json_search_found_products' , $found_products );
2012-10-09 13:32:49 +00:00
2012-03-06 15:46:53 +00:00
echo json_encode ( $found_products );
2012-08-01 12:43:46 +00:00
2012-03-06 15:46:53 +00:00
die ();
}
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_json_search_products' , 'woocommerce_json_search_products' );
2012-03-12 17:26:11 +00:00
2012-08-14 16:19:22 +00:00
/**
* Search for product variations and return json
*
* @ access public
* @ return void
* @ see woocommerce_json_search_products ()
*/
function woocommerce_json_search_products_and_variations () {
2012-03-12 17:26:11 +00:00
woocommerce_json_search_products ( '' , array ( 'product' , 'product_variation' ) );
}
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_json_search_products_and_variations' , 'woocommerce_json_search_products_and_variations' );
2012-07-31 17:26:25 +00:00
/**
* Search for customers and return json
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2012-07-31 17:26:25 +00:00
*/
function woocommerce_json_search_customers () {
check_ajax_referer ( 'search-customers' , 'security' );
2012-08-01 12:43:46 +00:00
2013-01-16 15:00:11 +00:00
header ( 'Content-Type: application/json; charset=utf-8' );
2012-07-31 17:26:25 +00:00
$term = urldecode ( stripslashes ( strip_tags ( $_GET [ 'term' ] ) ) );
2012-08-01 12:43:46 +00:00
if ( empty ( $term ) )
2012-07-31 17:26:25 +00:00
die ();
2012-08-01 12:43:46 +00:00
2012-10-16 09:45:33 +00:00
$default = isset ( $_GET [ 'default' ] ) ? $_GET [ 'default' ] : __ ( 'Guest' , 'woocommerce' );
2012-08-01 12:43:46 +00:00
$found_customers = array ( '' => $default );
2013-08-13 13:38:53 +00:00
add_action ( 'pre_user_query' , 'woocommerce_json_search_customer_name' );
2012-08-01 12:43:46 +00:00
$customers_query = new WP_User_Query ( array (
2012-07-31 17:26:25 +00:00
'fields' => 'all' ,
2012-08-01 12:43:46 +00:00
'orderby' => 'display_name' ,
2012-07-31 17:26:25 +00:00
'search' => '*' . $term . '*' ,
'search_columns' => array ( 'ID' , 'user_login' , 'user_email' , 'user_nicename' )
) );
2012-08-01 12:43:46 +00:00
2013-08-13 13:38:53 +00:00
remove_action ( 'pre_user_query' , 'woocommerce_json_search_customer_name' );
2012-07-31 17:26:25 +00:00
$customers = $customers_query -> get_results ();
2012-08-01 12:43:46 +00:00
2012-07-31 17:26:25 +00:00
if ( $customers ) {
foreach ( $customers as $customer ) {
2012-10-16 13:35:17 +00:00
$found_customers [ $customer -> ID ] = $customer -> display_name . ' (#' . $customer -> ID . ' – ' . sanitize_email ( $customer -> user_email ) . ')' ;
2012-07-31 17:26:25 +00:00
}
}
echo json_encode ( $found_customers );
die ();
}
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce_json_search_customers' , 'woocommerce_json_search_customers' );
2013-08-13 13:38:53 +00:00
/**
* When searching using the WP_User_Query , search names ( user meta ) too
* @ param object $query
* @ return object
*/
function woocommerce_json_search_customer_name ( $query ) {
global $wpdb ;
$term = urldecode ( stripslashes ( strip_tags ( $_GET [ 'term' ] ) ) );
$query -> query_from .= " LEFT JOIN { $wpdb -> usermeta } as meta2 ON ( { $wpdb -> users } .ID = meta2.user_id) " ;
$query -> query_from .= " LEFT JOIN { $wpdb -> usermeta } as meta3 ON ( { $wpdb -> users } .ID = meta3.user_id) " ;
$query -> query_where .= $wpdb -> prepare ( " OR ( meta2.meta_value LIKE %s OR meta3.meta_value LIKE %s ) " , '%' . like_escape ( $term ) . '%' , '%' . like_escape ( $term ) . '%' );
$query -> query_where .= " AND meta2.meta_key = 'first_name' AND meta3.meta_key = 'last_name' " ;
}
2012-07-31 17:26:25 +00:00
2011-12-09 19:55:09 +00:00
/**
* Ajax request handling for categories ordering
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2011-12-09 19:55:09 +00:00
*/
function woocommerce_term_ordering () {
global $wpdb ;
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
$id = ( int ) $_POST [ 'id' ];
$next_id = isset ( $_POST [ 'nextid' ]) && ( int ) $_POST [ 'nextid' ] ? ( int ) $_POST [ 'nextid' ] : null ;
$taxonomy = isset ( $_POST [ 'thetaxonomy' ]) ? esc_attr ( $_POST [ 'thetaxonomy' ] ) : null ;
$term = get_term_by ( 'id' , $id , $taxonomy );
2012-08-01 12:43:46 +00:00
2012-08-14 16:19:22 +00:00
if ( ! $id || ! $term || ! $taxonomy ) die ( 0 );
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
woocommerce_order_terms ( $term , $next_id , $taxonomy );
2012-08-01 12:43:46 +00:00
2011-12-09 19:55:09 +00:00
$children = get_terms ( $taxonomy , " child_of= $id &menu_order=ASC&hide_empty=0 " );
2012-08-01 12:43:46 +00:00
2012-08-14 16:19:22 +00:00
if ( $term && sizeof ( $children ) ) {
2011-12-09 19:55:09 +00:00
echo 'children' ;
2012-08-01 12:43:46 +00:00
die ;
2011-12-08 15:13:15 +00:00
}
2011-12-09 19:55:09 +00:00
}
2012-05-08 19:30:18 +00:00
2012-08-14 16:19:22 +00:00
add_action ( 'wp_ajax_woocommerce-term-ordering' , 'woocommerce_term_ordering' );
2012-05-08 19:30:18 +00:00
/**
* Ajax request handling for product ordering
*
* Based on Simple Page Ordering by 10 up ( http :// wordpress . org / extend / plugins / simple - page - ordering / )
2012-08-14 16:19:22 +00:00
*
* @ access public
* @ return void
2012-05-08 19:30:18 +00:00
*/
function woocommerce_product_ordering () {
2012-05-25 11:34:33 +00:00
global $wpdb ;
2012-08-01 12:43:46 +00:00
2012-05-08 19:30:18 +00:00
// check permissions again and make sure we have what we need
2012-11-05 16:50:24 +00:00
if ( ! current_user_can ( 'edit_products' ) || empty ( $_POST [ 'id' ] ) || ( ! isset ( $_POST [ 'previd' ] ) && ! isset ( $_POST [ 'nextid' ] ) ) )
2012-05-08 19:30:18 +00:00
die ( - 1 );
2012-08-01 12:43:46 +00:00
2012-05-08 19:30:18 +00:00
// real post?
if ( ! $post = get_post ( $_POST [ 'id' ] ) )
die ( - 1 );
2012-08-01 12:43:46 +00:00
2013-01-16 15:00:11 +00:00
header ( 'Content-Type: application/json; charset=utf-8' );
2012-05-08 19:30:18 +00:00
$previd = isset ( $_POST [ 'previd' ] ) ? $_POST [ 'previd' ] : false ;
$nextid = isset ( $_POST [ 'nextid' ] ) ? $_POST [ 'nextid' ] : false ;
$new_pos = array (); // store new positions for ajax
2012-08-01 12:43:46 +00:00
2012-12-04 13:15:01 +00:00
$siblings = $wpdb -> get_results ( $wpdb -> prepare ( '
SELECT ID , menu_order FROM % 1 $s AS posts
WHERE posts . post_type = \ ' product\ '
AND posts . post_status IN ( \ ' publish\ ' , \ ' pending\ ' , \ ' draft\ ' , \ ' future\ ' , \ ' private \ ' )
AND posts . ID NOT IN ( % 2 $d )
2012-10-05 18:02:02 +00:00
ORDER BY posts . menu_order ASC , posts . ID DESC
2012-12-04 13:15:01 +00:00
' , $wpdb -> posts , $post -> ID ) );
2012-08-01 12:43:46 +00:00
2012-05-08 19:30:18 +00:00
$menu_order = 0 ;
2012-08-01 12:43:46 +00:00
2012-05-25 11:34:33 +00:00
foreach ( $siblings as $sibling ) {
2012-08-01 12:43:46 +00:00
2012-05-08 19:30:18 +00:00
// if this is the post that comes after our repositioned post, set our repositioned post position and increment menu order
if ( $nextid == $sibling -> ID ) {
2012-08-01 12:43:46 +00:00
$wpdb -> update (
$wpdb -> posts ,
array (
2012-05-25 11:34:33 +00:00
'menu_order' => $menu_order
2012-08-01 12:43:46 +00:00
),
array ( 'ID' => $post -> ID ),
array ( '%d' ),
array ( '%d' )
2012-05-25 11:34:33 +00:00
);
$new_pos [ $post -> ID ] = $menu_order ;
2012-05-08 19:30:18 +00:00
$menu_order ++ ;
}
2012-08-01 12:43:46 +00:00
2012-05-08 19:30:18 +00:00
// if repositioned post has been set, and new items are already in the right order, we can stop
2012-05-25 11:34:33 +00:00
if ( isset ( $new_pos [ $post -> ID ] ) && $sibling -> menu_order >= $menu_order )
2012-05-08 19:30:18 +00:00
break ;
2012-08-01 12:43:46 +00:00
2012-05-08 19:30:18 +00:00
// set the menu order of the current sibling and increment the menu order
2012-08-01 12:43:46 +00:00
$wpdb -> update (
$wpdb -> posts ,
array (
2012-05-25 11:34:33 +00:00
'menu_order' => $menu_order
2012-08-01 12:43:46 +00:00
),
array ( 'ID' => $sibling -> ID ),
array ( '%d' ),
array ( '%d' )
2012-05-25 11:34:33 +00:00
);
$new_pos [ $sibling -> ID ] = $menu_order ;
2012-05-08 19:30:18 +00:00
$menu_order ++ ;
2012-08-01 12:43:46 +00:00
2012-05-08 19:30:18 +00:00
if ( ! $nextid && $previd == $sibling -> ID ) {
2012-08-01 12:43:46 +00:00
$wpdb -> update (
$wpdb -> posts ,
array (
2012-05-25 11:34:33 +00:00
'menu_order' => $menu_order
2012-08-01 12:43:46 +00:00
),
array ( 'ID' => $post -> ID ),
array ( '%d' ),
array ( '%d' )
2012-05-25 11:34:33 +00:00
);
2012-05-08 19:30:18 +00:00
$new_pos [ $post -> ID ] = $menu_order ;
$menu_order ++ ;
}
2012-08-01 12:43:46 +00:00
2012-05-25 11:34:33 +00:00
}
2012-08-01 12:43:46 +00:00
2012-05-25 11:34:33 +00:00
die ( json_encode ( $new_pos ) );
2012-05-08 19:30:18 +00:00
}
2012-08-14 16:19:22 +00:00
2013-02-22 16:30:01 +00:00
add_action ( 'wp_ajax_woocommerce_product_ordering' , 'woocommerce_product_ordering' );