2013-11-14 12:13:34 +00:00
< ? php
2014-05-28 13:52:50 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
2013-11-14 12:13:34 +00:00
/**
* WooCommerce WC_AJAX
*
* AJAX Event Handler
*
* @ class WC_AJAX
2014-05-28 13:52:50 +00:00
* @ version 2.2 . 0
2013-11-14 12:13:34 +00:00
* @ package WooCommerce / Classes
* @ category Class
* @ author WooThemes
*/
class WC_AJAX {
/**
2014-05-28 13:52:50 +00:00
* Hook in methods
2013-11-14 12:13:34 +00:00
*/
2014-05-28 13:52:50 +00:00
public static function init () {
2013-11-14 12:13:34 +00:00
// woocommerce_EVENT => nopriv
$ajax_events = array (
2014-07-10 12:33:05 +00:00
'get_refreshed_fragments' => true ,
'apply_coupon' => true ,
'update_shipping_method' => true ,
'update_order_review' => true ,
'add_to_cart' => true ,
'checkout' => true ,
'feature_product' => false ,
'mark_order_complete' => false ,
'mark_order_processing' => false ,
'add_new_attribute' => false ,
'remove_variation' => false ,
'remove_variations' => false ,
'save_attributes' => false ,
'add_variation' => false ,
'link_all_variations' => false ,
'revoke_access_to_download' => false ,
'grant_access_to_download' => false ,
'get_customer_details' => false ,
'add_order_item' => false ,
'add_order_fee' => false ,
2014-07-16 18:41:18 +00:00
'add_order_shipping' => false ,
2014-07-21 01:36:12 +00:00
'add_order_tax' => false ,
2014-07-10 12:33:05 +00:00
'remove_order_item' => false ,
2014-07-21 01:57:23 +00:00
'remote_order_tax' => false ,
2014-07-10 12:33:05 +00:00
'reduce_order_item_stock' => false ,
'increase_order_item_stock' => false ,
'add_order_item_meta' => false ,
'remove_order_item_meta' => false ,
'calc_line_taxes' => false ,
2014-07-17 20:17:54 +00:00
'save_order_items' => false ,
'load_order_items' => false ,
2014-07-10 12:33:05 +00:00
'add_order_note' => false ,
'delete_order_note' => false ,
'json_search_products' => false ,
'json_search_products_and_variations' => false ,
'json_search_downloadable_products_and_variations' => false ,
'json_search_customers' => false ,
'term_ordering' => false ,
'product_ordering' => false ,
2014-07-10 15:39:10 +00:00
'refund_line_items' => false ,
'delete_refund' => false
2013-11-14 12:13:34 +00:00
);
2014-01-26 09:19:17 +00:00
2013-11-14 12:13:34 +00:00
foreach ( $ajax_events as $ajax_event => $nopriv ) {
2014-05-28 13:52:50 +00:00
add_action ( 'wp_ajax_woocommerce_' . $ajax_event , array ( __CLASS__ , $ajax_event ) );
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( $nopriv ) {
2014-05-28 13:52:50 +00:00
add_action ( 'wp_ajax_nopriv_woocommerce_' . $ajax_event , array ( __CLASS__ , $ajax_event ) );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
}
2014-06-04 10:16:19 +00:00
add_action ( 'wp_ajax_page_slurp' , array ( 'WC_Gateway_Mijireh' , 'page_slurp' ) );
2013-11-14 12:13:34 +00:00
}
/**
* Get a refreshed cart fragment
*/
2014-05-28 13:52:50 +00:00
public static function get_refreshed_fragments () {
2014-01-26 09:19:17 +00:00
2013-11-14 12:13:34 +00:00
// Get mini cart
ob_start ();
2013-11-25 14:16:26 +00:00
woocommerce_mini_cart ();
2013-11-14 12:13:34 +00:00
$mini_cart = ob_get_clean ();
// Fragments and mini cart are returned
$data = array (
'fragments' => apply_filters ( 'add_to_cart_fragments' , array (
'div.widget_shopping_cart_content' => '<div class="widget_shopping_cart_content">' . $mini_cart . '</div>'
)
),
'cart_hash' => WC () -> cart -> get_cart () ? md5 ( json_encode ( WC () -> cart -> get_cart () ) ) : ''
);
2014-05-31 07:32:22 +00:00
wp_send_json ( $data );
2013-11-14 12:13:34 +00:00
}
/**
* AJAX apply coupon on checkout page
*/
2014-05-28 13:52:50 +00:00
public static function apply_coupon () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'apply-coupon' , 'security' );
if ( ! empty ( $_POST [ 'coupon_code' ] ) ) {
WC () -> cart -> add_discount ( sanitize_text_field ( $_POST [ 'coupon_code' ] ) );
} else {
2013-11-18 11:44:22 +00:00
wc_add_notice ( WC_Coupon :: get_generic_coupon_error ( WC_Coupon :: E_WC_COUPON_PLEASE_ENTER ), 'error' );
2013-11-14 12:13:34 +00:00
}
2013-11-18 11:44:22 +00:00
wc_print_notices ();
2013-11-14 12:13:34 +00:00
die ();
}
/**
* AJAX update shipping method on cart page
*/
2014-05-28 13:52:50 +00:00
public static function update_shipping_method () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'update-shipping-method' , 'security' );
2014-05-12 13:04:36 +00:00
if ( ! defined ( 'WOOCOMMERCE_CART' ) ) {
define ( 'WOOCOMMERCE_CART' , true );
}
2013-11-14 12:13:34 +00:00
$chosen_shipping_methods = WC () -> session -> get ( 'chosen_shipping_methods' );
2014-04-25 11:30:39 +00:00
if ( isset ( $_POST [ 'shipping_method' ] ) && is_array ( $_POST [ 'shipping_method' ] ) ) {
foreach ( $_POST [ 'shipping_method' ] as $i => $value ) {
2013-11-25 13:34:21 +00:00
$chosen_shipping_methods [ $i ] = wc_clean ( $value );
2014-04-25 11:30:39 +00:00
}
}
2013-11-14 12:13:34 +00:00
WC () -> session -> set ( 'chosen_shipping_methods' , $chosen_shipping_methods );
WC () -> cart -> calculate_totals ();
2013-11-25 14:16:26 +00:00
woocommerce_cart_totals ();
2013-11-14 12:13:34 +00:00
die ();
}
/**
* AJAX update order review on checkout
*/
2014-05-28 13:52:50 +00:00
public static function update_order_review () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'update-order-review' , 'security' );
2014-05-12 13:04:36 +00:00
if ( ! defined ( 'WOOCOMMERCE_CHECKOUT' ) ) {
2013-11-14 12:13:34 +00:00
define ( 'WOOCOMMERCE_CHECKOUT' , true );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( 0 == sizeof ( WC () -> cart -> get_cart () ) ) {
2013-11-14 12:13:34 +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>' ;
die ();
}
do_action ( 'woocommerce_checkout_update_order_review' , $_POST [ 'post_data' ] );
$chosen_shipping_methods = WC () -> session -> get ( 'chosen_shipping_methods' );
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 'shipping_method' ] ) && is_array ( $_POST [ 'shipping_method' ] ) ) {
foreach ( $_POST [ 'shipping_method' ] as $i => $value ) {
2013-11-25 13:34:21 +00:00
$chosen_shipping_methods [ $i ] = wc_clean ( $value );
2014-05-12 13:04:36 +00:00
}
}
2013-11-14 12:13:34 +00:00
WC () -> session -> set ( 'chosen_shipping_methods' , $chosen_shipping_methods );
WC () -> session -> set ( 'chosen_payment_method' , empty ( $_POST [ 'payment_method' ] ) ? '' : $_POST [ 'payment_method' ] );
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 'country' ] ) ) {
2013-11-14 12:13:34 +00:00
WC () -> customer -> set_country ( $_POST [ 'country' ] );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 'state' ] ) ) {
2013-11-14 12:13:34 +00:00
WC () -> customer -> set_state ( $_POST [ 'state' ] );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 'postcode' ] ) ) {
2013-11-14 12:13:34 +00:00
WC () -> customer -> set_postcode ( $_POST [ 'postcode' ] );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 'city' ] ) ) {
2013-11-14 12:13:34 +00:00
WC () -> customer -> set_city ( $_POST [ 'city' ] );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 'address' ] ) ) {
2013-11-14 12:13:34 +00:00
WC () -> customer -> set_address ( $_POST [ 'address' ] );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 'address_2' ] ) ) {
2013-11-14 12:13:34 +00:00
WC () -> customer -> set_address_2 ( $_POST [ 'address_2' ] );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-04-08 15:28:27 +00:00
if ( wc_ship_to_billing_address_only () ) {
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 'country' ] ) ) {
2014-02-14 11:19:32 +00:00
WC () -> customer -> set_shipping_country ( $_POST [ 'country' ] );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 'state' ] ) ) {
2014-02-14 11:19:32 +00:00
WC () -> customer -> set_shipping_state ( $_POST [ 'state' ] );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 'postcode' ] ) ) {
2014-02-14 11:19:32 +00:00
WC () -> customer -> set_shipping_postcode ( $_POST [ 'postcode' ] );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 'city' ] ) ) {
2014-02-14 11:19:32 +00:00
WC () -> customer -> set_shipping_city ( $_POST [ 'city' ] );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 'address' ] ) ) {
2014-02-14 11:19:32 +00:00
WC () -> customer -> set_shipping_address ( $_POST [ 'address' ] );
2014-05-12 13:04:36 +00:00
}
2014-02-14 11:19:32 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 'address_2' ] ) ) {
2014-02-14 11:19:32 +00:00
WC () -> customer -> set_shipping_address_2 ( $_POST [ 'address_2' ] );
2014-05-12 13:04:36 +00:00
}
2014-02-14 11:19:32 +00:00
} else {
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 's_country' ] ) ) {
2014-02-14 11:19:32 +00:00
WC () -> customer -> set_shipping_country ( $_POST [ 's_country' ] );
2014-05-12 13:04:36 +00:00
}
2014-02-14 11:19:32 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 's_state' ] ) ) {
2014-02-14 11:19:32 +00:00
WC () -> customer -> set_shipping_state ( $_POST [ 's_state' ] );
2014-05-12 13:04:36 +00:00
}
2014-02-14 11:19:32 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 's_postcode' ] ) ) {
2014-02-14 11:19:32 +00:00
WC () -> customer -> set_shipping_postcode ( $_POST [ 's_postcode' ] );
2014-05-12 13:04:36 +00:00
}
2014-02-14 11:19:32 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 's_city' ] ) ) {
2014-02-14 11:19:32 +00:00
WC () -> customer -> set_shipping_city ( $_POST [ 's_city' ] );
2014-05-12 13:04:36 +00:00
}
2014-02-14 11:19:32 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 's_address' ] ) ) {
2014-02-14 11:19:32 +00:00
WC () -> customer -> set_shipping_address ( $_POST [ 's_address' ] );
2014-05-12 13:04:36 +00:00
}
2014-02-14 11:19:32 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $_POST [ 's_address_2' ] ) ) {
2014-02-14 11:19:32 +00:00
WC () -> customer -> set_shipping_address_2 ( $_POST [ 's_address_2' ] );
2014-05-12 13:04:36 +00:00
}
2014-02-14 11:19:32 +00:00
}
2013-11-14 12:13:34 +00:00
WC () -> cart -> calculate_totals ();
do_action ( 'woocommerce_checkout_order_review' ); // Display review order table
die ();
}
/**
* AJAX add to cart
*/
2014-05-28 13:52:50 +00:00
public static function add_to_cart () {
2013-11-14 12:13:34 +00:00
$product_id = apply_filters ( 'woocommerce_add_to_cart_product_id' , absint ( $_POST [ 'product_id' ] ) );
2014-06-25 10:25:28 +00:00
$quantity = empty ( $_POST [ 'quantity' ] ) ? 1 : wc_stock_amount ( $_POST [ 'quantity' ] );
2013-11-14 12:13:34 +00:00
$passed_validation = apply_filters ( 'woocommerce_add_to_cart_validation' , true , $product_id , $quantity );
if ( $passed_validation && WC () -> cart -> add_to_cart ( $product_id , $quantity ) ) {
do_action ( 'woocommerce_ajax_added_to_cart' , $product_id );
if ( get_option ( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
2013-11-25 12:44:32 +00:00
wc_add_to_cart_message ( $product_id );
2013-11-14 12:13:34 +00:00
}
// Return fragments
2014-05-28 13:52:50 +00:00
self :: get_refreshed_fragments ();
2013-11-14 12:13:34 +00:00
} else {
// If there was an error adding to the cart, redirect to the product page to show any errors
$data = array (
'error' => true ,
'product_url' => apply_filters ( 'woocommerce_cart_redirect_after_error' , get_permalink ( $product_id ), $product_id )
);
2014-05-31 07:32:22 +00:00
wp_send_json ( $data );
2013-11-14 12:13:34 +00:00
}
die ();
}
/**
* Process ajax checkout form
*/
2014-05-28 13:52:50 +00:00
public static function checkout () {
2014-05-12 13:04:36 +00:00
if ( ! defined ( 'WOOCOMMERCE_CHECKOUT' ) ) {
2013-11-14 12:13:34 +00:00
define ( 'WOOCOMMERCE_CHECKOUT' , true );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-28 13:52:50 +00:00
WC () -> checkout () -> process_checkout ();
2013-11-14 12:13:34 +00:00
die ( 0 );
}
/**
* Feature a product from admin
*/
2014-05-28 13:52:50 +00:00
public static function feature_product () {
2014-04-04 13:16:11 +00:00
if ( ! current_user_can ( 'edit_products' ) ) {
2014-06-11 20:24:27 +00:00
wp_die ( __ ( 'You do not have sufficient permissions to access this page.' , 'woocommerce' ), '' , array ( 'response' => 403 ) );
2014-04-04 13:16:11 +00:00
}
2013-11-14 12:13:34 +00:00
2014-04-04 13:16:11 +00:00
if ( ! check_admin_referer ( 'woocommerce-feature-product' ) ) {
2014-06-11 20:24:27 +00:00
wp_die ( __ ( 'You have taken too long. Please go back and retry.' , 'woocommerce' ), '' , array ( 'response' => 403 ) );
2014-04-04 13:16:11 +00:00
}
2013-11-14 12:13:34 +00:00
2014-04-04 13:16:11 +00:00
$post_id = ! empty ( $_GET [ 'product_id' ] ) ? ( int ) $_GET [ 'product_id' ] : '' ;
2013-11-14 12:13:34 +00:00
2014-04-04 13:16:11 +00:00
if ( ! $post_id || get_post_type ( $post_id ) !== 'product' ) {
die ;
}
2013-11-14 12:13:34 +00:00
2014-04-04 13:16:11 +00:00
$featured = get_post_meta ( $post_id , '_featured' , true );
2013-11-14 12:13:34 +00:00
2014-04-04 13:16:11 +00:00
if ( 'yes' === $featured ) {
update_post_meta ( $post_id , '_featured' , 'no' );
} else {
update_post_meta ( $post_id , '_featured' , 'yes' );
}
2013-11-14 12:13:34 +00:00
2014-06-19 10:33:55 +00:00
delete_transient ( 'wc_featured_products' );
2013-11-14 12:13:34 +00:00
2014-04-04 13:16:11 +00:00
wp_safe_redirect ( remove_query_arg ( array ( 'trashed' , 'untrashed' , 'deleted' , 'ids' ), wp_get_referer () ) );
2013-11-14 12:13:34 +00:00
die ();
}
/**
* Mark an order as complete
*/
2014-05-28 13:52:50 +00:00
public static function mark_order_complete () {
2014-05-12 13:04:36 +00:00
if ( ! current_user_can ( 'edit_shop_orders' ) ) {
2014-06-11 20:24:27 +00:00
wp_die ( __ ( 'You do not have sufficient permissions to access this page.' , 'woocommerce' ), '' , array ( 'response' => 403 ) );
2014-05-12 13:04:36 +00:00
}
if ( ! check_admin_referer ( 'woocommerce-mark-order-complete' ) ) {
2014-06-11 20:24:27 +00:00
wp_die ( __ ( 'You have taken too long. Please go back and retry.' , 'woocommerce' ), '' , array ( 'response' => 403 ) );
2014-05-12 13:04:36 +00:00
}
$order_id = isset ( $_GET [ 'order_id' ] ) && ( int ) $_GET [ 'order_id' ] ? ( int ) $_GET [ 'order_id' ] : '' ;
if ( ! $order_id ) {
die ();
}
2013-11-14 12:13:34 +00:00
2014-06-13 13:24:51 +00:00
$order = get_order ( $order_id );
2013-11-14 12:13:34 +00:00
$order -> update_status ( 'completed' );
wp_safe_redirect ( wp_get_referer () );
die ();
}
/**
* Mark an order as processing
*/
2014-05-28 13:52:50 +00:00
public static function mark_order_processing () {
2014-05-12 13:04:36 +00:00
if ( ! current_user_can ( 'edit_shop_orders' ) ) {
2014-06-11 20:24:27 +00:00
wp_die ( __ ( 'You do not have sufficient permissions to access this page.' , 'woocommerce' ), '' , array ( 'response' => 403 ) );
2014-05-12 13:04:36 +00:00
}
if ( ! check_admin_referer ( 'woocommerce-mark-order-processing' ) ) {
2014-06-11 20:24:27 +00:00
wp_die ( __ ( 'You have taken too long. Please go back and retry.' , 'woocommerce' ), '' , array ( 'response' => 403 ) );
2014-05-12 13:04:36 +00:00
}
$order_id = isset ( $_GET [ 'order_id' ] ) && ( int ) $_GET [ 'order_id' ] ? ( int ) $_GET [ 'order_id' ] : '' ;
if ( ! $order_id ) {
die ();
}
2013-11-14 12:13:34 +00:00
2014-06-13 13:24:51 +00:00
$order = get_order ( $order_id );
2013-11-14 12:13:34 +00:00
$order -> update_status ( 'processing' );
wp_safe_redirect ( wp_get_referer () );
die ();
}
/**
* Add a new attribute via ajax function
*/
2014-05-28 13:52:50 +00:00
public static function add_new_attribute () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'add-attribute' , 'security' );
$taxonomy = esc_attr ( $_POST [ 'taxonomy' ] );
2014-05-12 13:04:36 +00:00
$term = stripslashes ( $_POST [ 'term' ] );
2013-11-14 12:13:34 +00:00
if ( taxonomy_exists ( $taxonomy ) ) {
$result = wp_insert_term ( $term , $taxonomy );
2014-05-12 13:04:36 +00:00
if ( is_wp_error ( $result ) ) {
2014-05-31 07:32:22 +00:00
wp_send_json ( array (
2014-05-12 13:04:36 +00:00
'error' => $result -> get_error_message ()
2014-05-31 07:32:22 +00:00
) );
2014-05-12 13:04:36 +00:00
} else {
2014-05-31 07:32:22 +00:00
wp_send_json ( array (
2014-05-12 13:04:36 +00:00
'term_id' => $result [ 'term_id' ],
'name' => $term ,
'slug' => sanitize_title ( $term ),
2014-05-31 07:32:22 +00:00
) );
2013-11-14 12:13:34 +00:00
}
}
die ();
}
/**
* Delete variation via ajax function
*/
2014-05-28 13:52:50 +00:00
public static function remove_variation () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'delete-variation' , 'security' );
2014-05-12 13:04:36 +00:00
2013-11-14 12:13:34 +00:00
$variation_id = intval ( $_POST [ 'variation_id' ] );
2014-05-12 13:04:36 +00:00
$variation = get_post ( $variation_id );
if ( $variation && 'product_variation' == $variation -> post_type ) {
2013-11-14 12:13:34 +00:00
wp_delete_post ( $variation_id );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
die ();
}
/**
* Delete variations via ajax function
*/
2014-05-28 13:52:50 +00:00
public static function remove_variations () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'delete-variations' , 'security' );
2014-05-12 13:04:36 +00:00
2013-11-14 12:13:34 +00:00
$variation_ids = ( array ) $_POST [ 'variation_ids' ];
2014-05-12 13:04:36 +00:00
2013-11-14 12:13:34 +00:00
foreach ( $variation_ids as $variation_id ) {
2014-05-12 13:04:36 +00:00
$variation = get_post ( $variation_id );
if ( $variation && 'product_variation' == $variation -> post_type ) {
2013-11-14 12:13:34 +00:00
wp_delete_post ( $variation_id );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
}
2014-05-12 13:04:36 +00:00
2013-11-14 12:13:34 +00:00
die ();
}
/**
* Save attributes via ajax
*/
2014-05-28 13:52:50 +00:00
public static function save_attributes () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'save-attributes' , 'security' );
// Get post data
parse_str ( $_POST [ 'data' ], $data );
$post_id = absint ( $_POST [ 'post_id' ] );
// Save Attributes
$attributes = array ();
if ( isset ( $data [ 'attribute_names' ] ) ) {
$attribute_names = array_map ( 'stripslashes' , $data [ 'attribute_names' ] );
2013-11-14 18:40:15 +00:00
$attribute_values = isset ( $data [ 'attribute_values' ] ) ? $data [ 'attribute_values' ] : array ();
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $data [ 'attribute_visibility' ] ) ) {
2013-11-14 12:13:34 +00:00
$attribute_visibility = $data [ 'attribute_visibility' ];
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( isset ( $data [ 'attribute_variation' ] ) ) {
2013-11-14 12:13:34 +00:00
$attribute_variation = $data [ 'attribute_variation' ];
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
$attribute_is_taxonomy = $data [ 'attribute_is_taxonomy' ];
2014-05-12 13:04:36 +00:00
$attribute_position = $data [ 'attribute_position' ];
2013-11-14 12:13:34 +00:00
$attribute_names_count = sizeof ( $attribute_names );
2014-05-12 13:04:36 +00:00
for ( $i = 0 ; $i < $attribute_names_count ; $i ++ ) {
if ( ! $attribute_names [ $i ] ) {
2013-11-14 12:13:34 +00:00
continue ;
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
$is_visible = isset ( $attribute_visibility [ $i ] ) ? 1 : 0 ;
$is_variation = isset ( $attribute_variation [ $i ] ) ? 1 : 0 ;
$is_taxonomy = $attribute_is_taxonomy [ $i ] ? 1 : 0 ;
2013-11-14 12:13:34 +00:00
if ( $is_taxonomy ) {
if ( isset ( $attribute_values [ $i ] ) ) {
2014-05-12 13:04:36 +00:00
// Select based attributes - Format values (posted values are slugs)
if ( is_array ( $attribute_values [ $i ] ) ) {
$values = array_map ( 'sanitize_title' , $attribute_values [ $i ] );
// Text based attributes - Posted values are term names - don't change to slugs
} else {
$values = array_map ( 'stripslashes' , array_map ( 'strip_tags' , explode ( WC_DELIMITER , $attribute_values [ $i ] ) ) );
}
// Remove empty items in the array
$values = array_filter ( $values , 'strlen' );
} 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
$attributes [ sanitize_title ( $attribute_names [ $i ] ) ] = array (
'name' => wc_clean ( $attribute_names [ $i ] ),
'value' => '' ,
'position' => $attribute_position [ $i ],
'is_visible' => $is_visible ,
'is_variation' => $is_variation ,
'is_taxonomy' => $is_taxonomy
);
}
} elseif ( isset ( $attribute_values [ $i ] ) ) {
// Text based, separate by pipe
$values = implode ( ' ' . WC_DELIMITER . ' ' , array_map ( 'wc_clean' , array_map ( 'stripslashes' , explode ( WC_DELIMITER , $attribute_values [ $i ] ) ) ) );
// Custom attribute - Add attribute to array and set the values
$attributes [ sanitize_title ( $attribute_names [ $i ] ) ] = array (
'name' => wc_clean ( $attribute_names [ $i ] ),
'value' => $values ,
'position' => $attribute_position [ $i ],
'is_visible' => $is_visible ,
'is_variation' => $is_variation ,
'is_taxonomy' => $is_taxonomy
);
}
2013-11-14 12:13:34 +00:00
}
}
if ( ! function_exists ( 'attributes_cmp' ) ) {
function attributes_cmp ( $a , $b ) {
2014-05-12 13:04:36 +00:00
if ( $a [ 'position' ] == $b [ 'position' ] ) {
return 0 ;
}
return ( $a [ 'position' ] < $b [ 'position' ] ) ? - 1 : 1 ;
2013-11-14 12:13:34 +00:00
}
}
uasort ( $attributes , 'attributes_cmp' );
update_post_meta ( $post_id , '_product_attributes' , $attributes );
die ();
}
/**
* Add variation via ajax function
*/
2014-05-28 13:52:50 +00:00
public static function add_variation () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'add-variation' , 'security' );
$post_id = intval ( $_POST [ 'post_id' ] );
$loop = intval ( $_POST [ 'loop' ] );
$variation = array (
2014-05-12 13:04:36 +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'
2013-11-14 12:13:34 +00:00
);
$variation_id = wp_insert_post ( $variation );
do_action ( 'woocommerce_create_product_variation' , $variation_id );
if ( $variation_id ) {
$variation_post_status = 'publish' ;
$variation_data = get_post_meta ( $variation_id );
$variation_data [ 'variation_post_id' ] = $variation_id ;
// Get attributes
$attributes = ( array ) maybe_unserialize ( get_post_meta ( $post_id , '_product_attributes' , true ) );
// Get tax classes
2014-05-12 13:04:36 +00:00
$tax_classes = array_filter ( array_map ( 'trim' , explode ( " \n " , get_option ( 'woocommerce_tax_classes' ))));
$tax_class_options = array ();
2013-11-14 12:13:34 +00:00
$tax_class_options [ 'parent' ] = __ ( 'Same as parent' , 'woocommerce' );
2014-05-12 13:04:36 +00:00
$tax_class_options [ '' ] = __ ( 'Standard' , 'woocommerce' );
if ( $tax_classes ) {
foreach ( $tax_classes as $class ) {
$tax_class_options [ sanitize_title ( $class ) ] = $class ;
}
}
2013-11-14 12:13:34 +00:00
// Get parent data
$parent_data = array (
2014-05-12 13:04:36 +00:00
'id' => $post_id ,
'attributes' => $attributes ,
2013-11-14 12:13:34 +00:00
'tax_class_options' => $tax_class_options ,
2014-05-12 13:04:36 +00:00
'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 )
2013-11-14 12:13:34 +00:00
);
2014-05-12 13:04:36 +00:00
if ( ! $parent_data [ 'weight' ] ) {
2013-11-14 12:13:34 +00:00
$parent_data [ 'weight' ] = '0.00' ;
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( ! $parent_data [ 'length' ] ) {
2013-11-14 12:13:34 +00:00
$parent_data [ 'length' ] = '0' ;
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( ! $parent_data [ 'width' ] ) {
2013-11-14 12:13:34 +00:00
$parent_data [ 'width' ] = '0' ;
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( ! $parent_data [ 'height' ] ) {
2013-11-14 12:13:34 +00:00
$parent_data [ 'height' ] = '0' ;
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
$_tax_class = '' ;
2013-11-14 12:13:34 +00:00
$_downloadable_files = '' ;
2014-05-12 13:04:36 +00:00
$image_id = 0 ;
$variation = get_post ( $variation_id ); // Get the variation object
2013-11-14 12:13:34 +00:00
2014-06-04 10:16:19 +00:00
include ( 'admin/meta-boxes/views/html-variation-admin.php' );
2013-11-14 12:13:34 +00:00
}
die ();
}
/**
* Link all variations via ajax function
*/
2014-05-28 13:52:50 +00:00
public static function link_all_variations () {
2013-11-14 12:13:34 +00:00
if ( ! defined ( 'WC_MAX_LINKED_VARIATIONS' ) ) {
define ( 'WC_MAX_LINKED_VARIATIONS' , 49 );
}
check_ajax_referer ( 'link-variations' , 'security' );
@ set_time_limit ( 0 );
$post_id = intval ( $_POST [ 'post_id' ] );
2014-05-12 13:04:36 +00:00
if ( ! $post_id ) {
die ();
}
2013-11-14 12:13:34 +00:00
$variations = array ();
2014-05-12 13:04:36 +00:00
$_product = get_product ( $post_id , array ( 'product_type' => 'variable' ) );
2013-11-14 12:13:34 +00:00
// Put variation attributes into an array
foreach ( $_product -> get_attributes () as $attribute ) {
2014-05-12 13:04:36 +00:00
if ( ! $attribute [ 'is_variation' ] ) {
continue ;
}
2013-11-14 12:13:34 +00:00
$attribute_field_name = 'attribute_' . sanitize_title ( $attribute [ 'name' ] );
if ( $attribute [ 'is_taxonomy' ] ) {
2014-05-31 07:32:22 +00:00
$options = wc_get_product_terms ( $post_id , $attribute [ 'name' ], array ( 'fields' => 'names' ) );
2013-11-14 12:13:34 +00:00
} else {
$options = explode ( WC_DELIMITER , $attribute [ 'value' ] );
}
$options = array_map ( 'sanitize_title' , array_map ( 'trim' , $options ) );
$variations [ $attribute_field_name ] = $options ;
}
// Quit out if none were found
2014-05-12 13:04:36 +00:00
if ( sizeof ( $variations ) == 0 ) {
die ();
}
2013-11-14 12:13:34 +00:00
// Get existing variations so we don't create duplicates
2014-05-12 13:04:36 +00:00
$available_variations = array ();
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
foreach ( $_product -> get_children () as $child_id ) {
$child = $_product -> get_child ( $child_id );
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( ! empty ( $child -> variation_id ) ) {
$available_variations [] = $child -> get_variation_attributes ();
}
}
2013-11-14 12:13:34 +00:00
// Created posts will all have the following data
$variation_post_data = array (
2014-02-07 11:39:37 +00:00
'post_title' => 'Product #' . $post_id . ' Variation' ,
2013-11-14 12:13:34 +00:00
'post_content' => '' ,
2014-02-07 11:39:37 +00:00
'post_status' => 'publish' ,
'post_author' => get_current_user_id (),
'post_parent' => $post_id ,
'post_type' => 'product_variation'
2013-11-14 12:13:34 +00:00
);
// Now find all combinations and create posts
if ( ! function_exists ( 'array_cartesian' ) ) {
2014-05-12 13:04:36 +00:00
2013-11-28 17:59:09 +00:00
/**
* @ param array $input
* @ return array
*/
2013-11-14 12:13:34 +00:00
function array_cartesian ( $input ) {
2014-05-12 13:04:36 +00:00
$result = array ();
while ( list ( $key , $values ) = each ( $input ) ) {
// If a sub-array is empty, it doesn't affect the cartesian product
if ( empty ( $values ) ) {
continue ;
}
// Special case: seeding the product array with the values from the first sub-array
if ( empty ( $result ) ) {
foreach ( $values as $value ) {
$result [] = array ( $key => $value );
}
}
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
// 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 ();
foreach ( $result as & $product ) {
// 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.
$product [ $key ] = array_shift ( $values );
// $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 ;
// Do step 2 above.
foreach ( $values as $item ) {
$copy [ $key ] = $item ;
$append [] = $copy ;
}
// Undo the side effecst of array_shift
array_unshift ( $values , $product [ $key ] );
}
// Out of the foreach, we can add to $results now
$result = array_merge ( $result , $append );
}
}
return $result ;
2013-11-14 12:13:34 +00:00
}
}
2014-05-12 13:04:36 +00:00
$variation_ids = array ();
$added = 0 ;
2013-11-14 12:13:34 +00:00
$possible_variations = array_cartesian ( $variations );
foreach ( $possible_variations as $variation ) {
// Check if variation already exists
2014-05-12 13:04:36 +00:00
if ( in_array ( $variation , $available_variations ) ) {
2013-11-14 12:13:34 +00:00
continue ;
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
$variation_id = wp_insert_post ( $variation_post_data );
$variation_ids [] = $variation_id ;
foreach ( $variation as $key => $value ) {
update_post_meta ( $variation_id , $key , $value );
}
$added ++ ;
do_action ( 'product_variation_linked' , $variation_id );
2014-05-12 13:04:36 +00:00
if ( $added > WC_MAX_LINKED_VARIATIONS ) {
2013-11-14 12:13:34 +00:00
break ;
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
}
2014-06-19 10:33:55 +00:00
delete_transient ( 'wc_product_children_ids_' . $post_id );
2013-11-14 12:13:34 +00:00
echo $added ;
die ();
}
/**
* Delete download permissions via ajax function
*/
2014-05-28 13:52:50 +00:00
public static function revoke_access_to_download () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'revoke-access' , 'security' );
global $wpdb ;
$download_id = $_POST [ 'download_id' ];
$product_id = intval ( $_POST [ 'product_id' ] );
$order_id = intval ( $_POST [ 'order_id' ] );
$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 ) );
do_action ( 'woocommerce_ajax_revoke_access_to_product_download' , $download_id , $product_id , $order_id );
die ();
}
/**
* Grant download permissions via ajax function
*/
2014-05-28 13:52:50 +00:00
public static function grant_access_to_download () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'grant-access' , 'security' );
global $wpdb ;
$wpdb -> hide_errors ();
2014-05-12 13:04:36 +00:00
$order_id = intval ( $_POST [ 'order_id' ] );
$product_ids = $_POST [ 'product_ids' ];
$loop = intval ( $_POST [ 'loop' ] );
$file_counter = 0 ;
2014-06-13 13:24:51 +00:00
$order = get_order ( $order_id );
2013-11-14 12:13:34 +00:00
2013-11-26 13:54:34 +00:00
if ( ! is_array ( $product_ids ) ) {
$product_ids = array ( $product_ids );
}
2013-11-14 12:13:34 +00:00
2013-11-26 13:54:34 +00:00
foreach ( $product_ids as $product_id ) {
2014-05-12 13:04:36 +00:00
$product = get_product ( $product_id );
$files = $product -> get_files ();
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( ! $order -> billing_email ) {
2013-11-26 13:54:34 +00:00
die ();
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2013-11-26 13:54:34 +00:00
if ( $files ) {
foreach ( $files as $download_id => $file ) {
if ( $inserted_id = wc_downloadable_file_permission ( $download_id , $product_id , $order ) ) {
2013-11-14 12:13:34 +00:00
2013-11-26 13:54:34 +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 ) );
$loop ++ ;
$file_counter ++ ;
if ( isset ( $file [ 'name' ] ) ) {
$file_count = $file [ 'name' ];
} else {
$file_count = sprintf ( __ ( 'File %d' , 'woocommerce' ), $file_counter );
}
2014-06-04 10:16:19 +00:00
include ( 'admin/meta-boxes/views/html-order-download-permission.php' );
2013-11-26 13:54:34 +00:00
}
2013-11-14 12:13:34 +00:00
}
}
}
die ();
}
/**
* Get customer details via ajax
*/
2014-05-28 13:52:50 +00:00
public static function get_customer_details () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'get-customer-details' , 'security' );
2014-05-12 13:04:36 +00:00
$user_id = ( int ) trim ( stripslashes ( $_POST [ 'user_id' ]));
2013-11-14 12:13:34 +00:00
$type_to_load = esc_attr ( trim ( stripslashes ( $_POST [ 'type_to_load' ])));
$customer_data = array (
$type_to_load . '_first_name' => get_user_meta ( $user_id , $type_to_load . '_first_name' , true ),
2014-05-12 13:04:36 +00:00
$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 ),
2013-11-14 12:13:34 +00:00
);
$customer_data = apply_filters ( 'woocommerce_found_customer_details' , $customer_data );
2014-05-31 07:32:22 +00:00
wp_send_json ( $customer_data );
2013-11-14 12:13:34 +00:00
}
/**
* Add order item via ajax
*/
2014-05-28 13:52:50 +00:00
public static function add_order_item () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'order-item' , 'security' );
$item_to_add = sanitize_text_field ( $_POST [ 'item_to_add' ] );
2014-05-12 13:04:36 +00:00
$order_id = absint ( $_POST [ 'order_id' ] );
2013-11-14 12:13:34 +00:00
// Find the item
2014-05-12 13:04:36 +00:00
if ( ! is_numeric ( $item_to_add ) ) {
2013-11-14 12:13:34 +00:00
die ();
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
$post = get_post ( $item_to_add );
2014-05-12 13:04:36 +00:00
if ( ! $post || ( 'product' !== $post -> post_type && 'product_variation' !== $post -> post_type ) ) {
2013-11-14 12:13:34 +00:00
die ();
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-07-20 04:28:16 +00:00
$_product = get_product ( $post -> ID );
$order = get_order ( $order_id );
$order_taxes = $order -> get_taxes ();
$class = 'new_row' ;
2013-11-14 12:13:34 +00:00
// Set values
$item = array ();
2014-03-24 06:02:21 +00:00
$item [ 'product_id' ] = $_product -> id ;
$item [ 'variation_id' ] = isset ( $_product -> variation_id ) ? $_product -> variation_id : '' ;
$item [ 'variation_data' ] = isset ( $_product -> variation_data ) ? $_product -> variation_data : '' ;
$item [ 'name' ] = $_product -> get_title ();
$item [ 'tax_class' ] = $_product -> get_tax_class ();
$item [ 'qty' ] = 1 ;
$item [ 'line_subtotal' ] = wc_format_decimal ( $_product -> get_price_excluding_tax () );
$item [ 'line_subtotal_tax' ] = '' ;
$item [ 'line_total' ] = wc_format_decimal ( $_product -> get_price_excluding_tax () );
$item [ 'line_tax' ] = '' ;
2013-11-14 12:13:34 +00:00
// Add line item
2014-05-12 13:04:36 +00:00
$item_id = wc_add_order_item ( $order_id , array (
'order_item_name' => $item [ 'name' ],
'order_item_type' => 'line_item'
) );
// Add line item meta
if ( $item_id ) {
wc_add_order_item_meta ( $item_id , '_qty' , $item [ 'qty' ] );
wc_add_order_item_meta ( $item_id , '_tax_class' , $item [ 'tax_class' ] );
wc_add_order_item_meta ( $item_id , '_product_id' , $item [ 'product_id' ] );
wc_add_order_item_meta ( $item_id , '_variation_id' , $item [ 'variation_id' ] );
wc_add_order_item_meta ( $item_id , '_line_subtotal' , $item [ 'line_subtotal' ] );
wc_add_order_item_meta ( $item_id , '_line_subtotal_tax' , $item [ 'line_subtotal_tax' ] );
wc_add_order_item_meta ( $item_id , '_line_total' , $item [ 'line_total' ] );
wc_add_order_item_meta ( $item_id , '_line_tax' , $item [ 'line_tax' ] );
2014-07-20 04:28:16 +00:00
// Since 2.2
wc_add_order_item_meta ( $item_id , '_line_tax_data' , array ( 'total' => array (), 'subtotal' => array () ) );
2014-05-12 13:04:36 +00:00
// Store variation data in meta
2014-03-24 06:02:21 +00:00
if ( $item [ 'variation_data' ] && is_array ( $item [ 'variation_data' ] ) ) {
foreach ( $item [ 'variation_data' ] as $key => $value ) {
2014-03-24 13:53:40 +00:00
wc_add_order_item_meta ( $item_id , str_replace ( 'attribute_' , '' , $key ), $value );
2014-03-24 06:02:21 +00:00
}
}
2014-05-12 13:04:36 +00:00
2014-03-24 06:02:21 +00:00
do_action ( 'woocommerce_ajax_add_order_item_meta' , $item_id , $item );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-07-22 20:33:49 +00:00
$item = apply_filters ( 'woocommerce_ajax_order_item' , $item , $item_id );
$can_be_edited = in_array ( $order -> get_status (), apply_filters ( 'wc_order_can_be_edited' , array ( 'pending' , 'on-hold' ) ) );
2013-11-14 12:13:34 +00:00
2014-06-04 10:16:19 +00:00
include ( 'admin/meta-boxes/views/html-order-item.php' );
2013-11-14 12:13:34 +00:00
// Quit out
die ();
}
/**
* Add order fee via ajax
*/
2014-05-28 13:52:50 +00:00
public static function add_order_fee () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'order-item' , 'security' );
2014-07-22 20:33:49 +00:00
$order_id = absint ( $_POST [ 'order_id' ] );
$order = get_order ( $order_id );
$order_taxes = $order -> get_taxes ();
$can_be_edited = in_array ( $order -> get_status (), apply_filters ( 'wc_order_can_be_edited' , array ( 'pending' , 'on-hold' ) ) );
2014-07-24 20:55:25 +00:00
$item = array ();
// Add new fee
$fee = new stdClass ();
$fee -> name = '' ;
$fee -> tax_class = '' ;
$fee -> taxable = $fee -> tax_class !== '0' ;
$fee -> amount = '' ;
$fee -> tax = '' ;
$fee -> tax_data = array ();
$item_id = $order -> add_fee ( $fee );
2013-11-14 12:13:34 +00:00
2014-06-04 10:16:19 +00:00
include ( 'admin/meta-boxes/views/html-order-fee.php' );
2013-11-14 12:13:34 +00:00
// Quit out
die ();
}
2014-07-16 18:41:18 +00:00
/**
* Add order shipping cost via ajax
*/
public static function add_order_shipping () {
check_ajax_referer ( 'order-item' , 'security' );
$order_id = absint ( $_POST [ 'order_id' ] );
$order = get_order ( $order_id );
2014-07-20 04:28:16 +00:00
$order_taxes = $order -> get_taxes ();
2014-07-16 18:41:18 +00:00
$shipping_methods = WC () -> shipping () ? WC () -> shipping -> load_shipping_methods () : array ();
2014-07-22 20:33:49 +00:00
$can_be_edited = in_array ( $order -> get_status (), apply_filters ( 'wc_order_can_be_edited' , array ( 'pending' , 'on-hold' ) ) );
2014-07-24 20:55:25 +00:00
$item = array ();
// Add new shipping
$shipping = new stdClass ();
$shipping -> label = '' ;
$shipping -> id = '' ;
$shipping -> cost = '' ;
$shipping -> taxes = array ();
$item_id = $order -> add_shipping ( $shipping );
2014-07-16 18:41:18 +00:00
include ( 'admin/meta-boxes/views/html-order-shipping.php' );
// Quit out
die ();
}
2014-07-21 01:36:12 +00:00
/**
* Add order tax column via ajax
*/
public static function add_order_tax () {
global $wpdb ;
check_ajax_referer ( 'order-item' , 'security' );
$order_id = absint ( $_POST [ 'order_id' ] );
$rate_id = absint ( $_POST [ 'rate_id' ] );
2014-07-24 20:37:02 +00:00
$order = new WC_Order ( $order_id );
$data = get_post_meta ( $order_id );
2014-07-21 01:36:12 +00:00
2014-07-24 20:37:02 +00:00
// Add new tax
$order -> add_tax ( $rate_id , 0 , 0 );
2014-07-21 01:36:12 +00:00
// Return HTML items
include ( 'admin/meta-boxes/views/html-order-items.php' );
die ();
}
2013-11-14 12:13:34 +00:00
/**
* Remove an order item
*/
2014-05-28 13:52:50 +00:00
public static function remove_order_item () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'order-item' , 'security' );
$order_item_ids = $_POST [ 'order_item_ids' ];
2014-07-07 15:45:08 +00:00
if ( ! is_array ( $order_item_ids ) && is_numeric ( $order_item_ids ) ) {
$order_item_ids = array ( $order_item_ids );
}
2013-11-14 12:13:34 +00:00
if ( sizeof ( $order_item_ids ) > 0 ) {
foreach ( $order_item_ids as $id ) {
2013-11-25 13:54:52 +00:00
wc_delete_order_item ( absint ( $id ) );
2013-11-14 12:13:34 +00:00
}
}
die ();
}
2014-07-21 01:57:23 +00:00
/**
* Remove an order tax
*/
public static function remote_order_tax () {
check_ajax_referer ( 'order-item' , 'security' );
$order_id = absint ( $_POST [ 'order_id' ] );
$rate_id = absint ( $_POST [ 'rate_id' ] );
wc_delete_order_item ( $rate_id );
// Return HTML items
$order = new WC_Order ( $order_id );
$data = get_post_meta ( $order_id );
include ( 'admin/meta-boxes/views/html-order-items.php' );
die ();
}
2013-11-14 12:13:34 +00:00
/**
* Reduce order item stock
*/
2014-05-28 13:52:50 +00:00
public static function reduce_order_item_stock () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'order-item' , 'security' );
2014-05-12 13:04:36 +00:00
$order_id = absint ( $_POST [ 'order_id' ] );
$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 ();
2014-06-13 13:24:51 +00:00
$order = get_order ( $order_id );
2014-05-12 13:04:36 +00:00
$order_items = $order -> get_items ();
$return = array ();
2013-11-14 12:13:34 +00:00
if ( $order && ! empty ( $order_items ) && sizeof ( $order_item_ids ) > 0 ) {
foreach ( $order_items as $item_id => $order_item ) {
// Only reduce checked items
2014-05-12 13:04:36 +00:00
if ( ! in_array ( $item_id , $order_item_ids ) ) {
2013-11-14 12:13:34 +00:00
continue ;
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
$_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 ) {
2014-04-25 14:27:58 +00:00
$stock_change = apply_filters ( 'woocommerce_reduce_order_stock_quantity' , $order_item_qty [ $item_id ], $item_id );
$new_stock = $_product -> reduce_stock ( $stock_change );
2013-11-14 12:13:34 +00:00
2014-04-25 14:27:58 +00:00
$return [] = sprintf ( __ ( 'Item #%s stock reduced from %s to %s.' , 'woocommerce' ), $order_item [ 'product_id' ], $new_stock + $stock_change , $new_stock );
$order -> add_order_note ( sprintf ( __ ( 'Item #%s stock reduced from %s to %s.' , 'woocommerce' ), $order_item [ 'product_id' ], $new_stock + $stock_change , $new_stock ) );
$order -> send_stock_notifications ( $_product , $new_stock , $order_item_qty [ $item_id ] );
2013-11-14 12:13:34 +00:00
}
}
do_action ( 'woocommerce_reduce_order_stock' , $order );
2014-05-12 13:04:36 +00:00
if ( empty ( $return ) ) {
2013-11-14 12:13:34 +00:00
$return [] = __ ( 'No products had their stock reduced - they may not have stock management enabled.' , 'woocommerce' );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
echo implode ( ', ' , $return );
}
die ();
}
/**
* Increase order item stock
*/
2014-05-28 13:52:50 +00:00
public static function increase_order_item_stock () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'order-item' , 'security' );
2014-05-12 13:04:36 +00:00
$order_id = absint ( $_POST [ 'order_id' ] );
$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 ();
2014-06-13 13:24:51 +00:00
$order = get_order ( $order_id );
2014-05-12 13:04:36 +00:00
$order_items = $order -> get_items ();
$return = array ();
2013-11-14 12:13:34 +00:00
if ( $order && ! empty ( $order_items ) && sizeof ( $order_item_ids ) > 0 ) {
foreach ( $order_items as $item_id => $order_item ) {
// Only reduce checked items
2014-05-12 13:04:36 +00:00
if ( ! in_array ( $item_id , $order_item_ids ) ) {
2013-11-14 12:13:34 +00:00
continue ;
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
$_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 ) {
2014-05-12 13:04:36 +00:00
$old_stock = $_product -> stock ;
$stock_change = apply_filters ( 'woocommerce_restore_order_stock_quantity' , $order_item_qty [ $item_id ], $item_id );
$new_quantity = $_product -> increase_stock ( $stock_change );
2013-11-14 12:13:34 +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 ) );
}
}
do_action ( 'woocommerce_restore_order_stock' , $order );
2014-05-12 13:04:36 +00:00
if ( empty ( $return ) ) {
2013-11-14 12:13:34 +00:00
$return [] = __ ( 'No products had their stock increased - they may not have stock management enabled.' , 'woocommerce' );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
echo implode ( ', ' , $return );
}
die ();
}
/**
* Add some meta to a line item
*/
2014-05-28 13:52:50 +00:00
public static function add_order_item_meta () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'order-item' , 'security' );
2013-11-25 13:54:52 +00:00
$meta_id = wc_add_order_item_meta ( absint ( $_POST [ 'order_item_id' ] ), __ ( 'Name' , 'woocommerce' ), __ ( 'Value' , 'woocommerce' ) );
2013-11-14 12:13:34 +00:00
if ( $meta_id ) {
2013-11-20 19:11:59 +00:00
echo '<tr data-meta_id="' . esc_attr ( $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>' ;
2013-11-14 12:13:34 +00:00
}
die ();
}
/**
* Remove meta from a line item
*/
2014-05-28 13:52:50 +00:00
public static function remove_order_item_meta () {
2013-11-14 12:13:34 +00:00
global $wpdb ;
check_ajax_referer ( 'order-item' , 'security' );
$meta_id = absint ( $_POST [ 'meta_id' ] );
$wpdb -> query ( $wpdb -> prepare ( " DELETE FROM { $wpdb -> prefix } woocommerce_order_itemmeta WHERE meta_id = %d " , $meta_id ) );
die ();
}
/**
* Calc line tax
*/
2014-05-28 13:52:50 +00:00
public static function calc_line_taxes () {
2013-11-14 12:13:34 +00:00
global $wpdb ;
check_ajax_referer ( 'calc-totals' , 'security' );
2014-07-24 20:33:26 +00:00
$tax = new WC_Tax ();
$order_id = absint ( $_POST [ 'order_id' ] );
$items = array ();
$country = strtoupper ( esc_attr ( $_POST [ 'country' ] ) );
$state = strtoupper ( esc_attr ( $_POST [ 'state' ] ) );
$postcode = strtoupper ( esc_attr ( $_POST [ 'postcode' ] ) );
$city = sanitize_title ( esc_attr ( $_POST [ 'city' ] ) );
$order = get_order ( $order_id );
$taxes = array ();
$shipping_taxes = array ();
2013-11-14 12:13:34 +00:00
2014-07-24 21:09:13 +00:00
// Parse the jQuery serialized items
parse_str ( $_POST [ 'items' ], $items );
2014-07-24 20:33:26 +00:00
// Prevent undefined warnings
2014-07-24 21:09:13 +00:00
if ( ! isset ( $items [ 'line_tax' ] ) ) {
2014-07-24 20:33:26 +00:00
$items [ 'line_tax' ] = array ();
}
2014-07-24 21:09:13 +00:00
if ( ! isset ( $items [ 'line_subtotal_tax' ] ) ) {
2014-07-24 20:33:26 +00:00
$items [ 'line_subtotal_tax' ] = array ();
}
2014-07-24 21:09:13 +00:00
$items [ 'order_taxes' ] = array ();
2014-07-24 20:33:26 +00:00
// Get items and fees taxes
if ( isset ( $items [ 'order_item_id' ] ) ) {
$get_values = array ( 'order_item_id' , 'line_subtotal' , 'line_total' , 'order_item_tax_class' );
foreach ( $get_values as $value ) {
$$value = isset ( $items [ $value ] ) ? $items [ $value ] : array ();
}
foreach ( $order_item_id as $item_id ) {
$item_id = absint ( $item_id );
$line_total [ $item_id ] = isset ( $line_total [ $item_id ] ) ? wc_format_decimal ( $line_total [ $item_id ] ) : 0 ;
$line_subtotal [ $item_id ] = isset ( $line_subtotal [ $item_id ] ) ? wc_format_decimal ( $line_subtotal [ $item_id ] ) : $line_total [ $item_id ];
$order_item_tax_class [ $item_id ] = isset ( $order_item_tax_class [ $item_id ] ) ? sanitize_text_field ( $order_item_tax_class [ $item_id ] ) : '' ;
$product_id = $order -> get_item_meta ( $item_id , '_product_id' , true );
2013-11-14 12:13:34 +00:00
// Get product details
if ( get_post_type ( $product_id ) == 'product' ) {
2014-05-12 13:04:36 +00:00
$_product = get_product ( $product_id );
$item_tax_status = $_product -> get_tax_status ();
2013-11-14 12:13:34 +00:00
} else {
2014-05-12 13:04:36 +00:00
$item_tax_status = 'taxable' ;
2013-11-14 12:13:34 +00:00
}
2014-07-24 20:33:26 +00:00
if ( '0' !== $order_item_tax_class [ $item_id ] && 'taxable' === $item_tax_status ) {
2014-06-12 15:47:43 +00:00
$tax_rates = WC_Tax :: find_rates ( array (
2014-05-12 13:04:36 +00:00
'country' => $country ,
'state' => $state ,
'postcode' => $postcode ,
'city' => $city ,
2014-07-24 20:33:26 +00:00
'tax_class' => $order_item_tax_class [ $item_id ]
2013-11-14 12:13:34 +00:00
) );
2014-07-24 20:33:26 +00:00
$line_taxes = WC_Tax :: calc_tax ( $line_total [ $item_id ], $tax_rates , false );
$line_subtotal_taxes = WC_Tax :: calc_tax ( $line_subtotal [ $item_id ], $tax_rates , false );
2013-11-14 12:13:34 +00:00
2014-07-24 20:33:26 +00:00
// Set the new line_tax
foreach ( $line_taxes as $_tax_id => $_tax_value ) {
$items [ 'line_tax' ][ $item_id ][ $_tax_id ] = $_tax_value ;
}
2013-11-14 12:13:34 +00:00
2014-07-24 20:33:26 +00:00
// Set the new line_subtotal_tax
foreach ( $line_subtotal_taxes as $_tax_id => $_tax_value ) {
$items [ 'line_subtotal_tax' ][ $item_id ][ $_tax_id ] = $_tax_value ;
}
2013-11-14 12:13:34 +00:00
// Sum the item taxes
2014-05-12 13:04:36 +00:00
foreach ( array_keys ( $taxes + $line_taxes ) as $key ) {
2013-11-14 12:13:34 +00:00
$taxes [ $key ] = ( isset ( $line_taxes [ $key ] ) ? $line_taxes [ $key ] : 0 ) + ( isset ( $taxes [ $key ] ) ? $taxes [ $key ] : 0 );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
}
}
}
2014-07-24 20:33:26 +00:00
// Get shipping taxes
if ( isset ( $items [ 'shipping_method_id' ] ) ) {
$matched_tax_rates = array ();
$tax_rates = WC_Tax :: find_rates ( array (
'country' => $country ,
'state' => $state ,
'postcode' => $postcode ,
'city' => $city ,
'tax_class' => ''
) );
if ( $tax_rates ) {
foreach ( $tax_rates as $key => $rate ) {
if ( isset ( $rate [ 'shipping' ] ) && 'yes' == $rate [ 'shipping' ] ) {
$matched_tax_rates [ $key ] = $rate ;
}
}
}
2013-11-14 12:13:34 +00:00
2014-07-24 20:33:26 +00:00
$get_values = array ( 'shipping_method_id' , 'shipping_cost' , 'shipping_taxes' );
foreach ( $get_values as $value ) {
$$value = isset ( $items [ $value ] ) ? $items [ $value ] : array ();
}
foreach ( $shipping_method_id as $item_id ) {
$item_id = absint ( $item_id );
$shipping_cost [ $item_id ] = isset ( $shipping_cost [ $item_id ] ) ? wc_format_decimal ( $shipping_cost [ $item_id ] ) : 0 ;
$shipping_taxes = WC_Tax :: calc_shipping_tax ( $shipping_cost [ $item_id ], $matched_tax_rates );
2013-11-14 12:13:34 +00:00
2014-07-24 20:33:26 +00:00
// Set the new shipping_taxes
foreach ( $shipping_taxes as $_tax_id => $_tax_value ) {
$items [ 'shipping_taxes' ][ $item_id ][ $_tax_id ] = $_tax_value ;
2014-05-12 13:04:36 +00:00
}
}
}
2013-11-14 12:13:34 +00:00
// Remove old tax rows
2014-06-13 14:54:08 +00:00
$order -> remove_order_items ( 'tax' );
2013-11-14 12:13:34 +00:00
2014-06-13 14:54:08 +00:00
// Add tax rows
foreach ( array_keys ( $taxes + $shipping_taxes ) as $tax_rate_id ) {
$order -> add_tax ( $tax_rate_id , isset ( $taxes [ $tax_rate_id ] ) ? $taxes [ $tax_rate_id ] : 0 , isset ( $shipping_taxes [ $tax_rate_id ] ) ? $shipping_taxes [ $tax_rate_id ] : 0 );
2013-11-14 12:13:34 +00:00
}
2014-07-24 20:33:26 +00:00
// Create the new order_taxes
foreach ( $order -> get_taxes () as $tax_id => $tax_item ) {
$items [ 'order_taxes' ][ $tax_id ] = absint ( $tax_item [ 'rate_id' ] );
2013-11-14 12:13:34 +00:00
}
2014-07-24 20:33:26 +00:00
// Save order items
wc_save_order_items ( $order_id , $items );
2013-11-14 12:13:34 +00:00
2014-07-24 20:33:26 +00:00
// Return HTML items
$order = new WC_Order ( $order_id );
$data = get_post_meta ( $order_id );
include ( 'admin/meta-boxes/views/html-order-items.php' );
2013-11-14 12:13:34 +00:00
2014-07-24 20:33:26 +00:00
die ();
2013-11-14 12:13:34 +00:00
}
2014-07-17 20:17:54 +00:00
/**
* Save order items via ajax
*/
public static function save_order_items () {
check_ajax_referer ( 'order-item' , 'security' );
if ( isset ( $_POST [ 'order_id' ] ) && isset ( $_POST [ 'items' ] ) ) {
$order_id = absint ( $_POST [ 'order_id' ] );
// Parse the jQuery serialized items
$items = array ();
parse_str ( $_POST [ 'items' ], $items );
// Save order items
wc_save_order_items ( $order_id , $items );
// Return HTML items
$order = new WC_Order ( $order_id );
2014-07-17 21:17:42 +00:00
$data = get_post_meta ( $order_id );
2014-07-17 20:17:54 +00:00
include ( 'admin/meta-boxes/views/html-order-items.php' );
}
die ();
}
/**
* Load order items via ajax
*/
public static function load_order_items () {
check_ajax_referer ( 'order-item' , 'security' );
// Return HTML items
$order_id = absint ( $_POST [ 'order_id' ] );
$order = new WC_Order ( $order_id );
2014-07-17 21:17:42 +00:00
$data = get_post_meta ( $order_id );
2014-07-17 20:17:54 +00:00
include ( 'admin/meta-boxes/views/html-order-items.php' );
die ();
}
2013-11-14 12:13:34 +00:00
/**
* Add order note via ajax
*/
2014-05-28 13:52:50 +00:00
public static function add_order_note () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'add-order-note' , 'security' );
2014-05-12 13:04:36 +00:00
$post_id = ( int ) $_POST [ 'post_id' ];
$note = wp_kses_post ( trim ( stripslashes ( $_POST [ 'note' ] ) ) );
$note_type = $_POST [ 'note_type' ];
2013-11-14 12:13:34 +00:00
$is_customer_note = $note_type == 'customer' ? 1 : 0 ;
if ( $post_id > 0 ) {
2014-06-13 13:24:51 +00:00
$order = get_order ( $post_id );
2013-11-14 12:13:34 +00:00
$comment_id = $order -> add_order_note ( $note , $is_customer_note );
2013-11-20 19:11:59 +00:00
echo '<li rel="' . esc_attr ( $comment_id ) . '" class="note ' ;
2014-05-12 13:04:36 +00:00
if ( $is_customer_note ) {
echo 'customer-note' ;
}
2013-11-14 12:13:34 +00:00
echo '"><div class="note_content">' ;
echo wpautop ( wptexturize ( $note ) );
echo '</div><p class="meta"><a href="#" class="delete_note">' . __ ( 'Delete note' , 'woocommerce' ) . '</a></p>' ;
echo '</li>' ;
}
// Quit out
die ();
}
/**
* Delete order note via ajax
*/
2014-05-28 13:52:50 +00:00
public static function delete_order_note () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'delete-order-note' , 'security' );
2014-05-12 13:04:36 +00:00
$note_id = ( int ) $_POST [ 'note_id' ];
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( $note_id > 0 ) {
2013-11-14 12:13:34 +00:00
wp_delete_comment ( $note_id );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
// Quit out
die ();
}
/**
* Search for products and echo json
*
* @ param string $x ( default : '' )
* @ param string $post_types ( default : array ( 'product' ))
*/
2014-05-28 13:52:50 +00:00
public static function json_search_products ( $x = '' , $post_types = array ( 'product' ) ) {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'search-products' , 'security' );
2014-01-26 09:19:17 +00:00
$term = ( string ) wc_clean ( stripslashes ( $_GET [ 'term' ] ) );
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( empty ( $term ) ) {
die ();
}
2013-11-14 12:13:34 +00:00
if ( is_numeric ( $term ) ) {
$args = array (
2014-05-12 13:04:36 +00:00
'post_type' => $post_types ,
'post_status' => 'publish' ,
'posts_per_page' => - 1 ,
'post__in' => array ( 0 , $term ),
'fields' => 'ids'
2013-11-14 12:13:34 +00:00
);
$args2 = array (
2014-05-12 13:04:36 +00:00
'post_type' => $post_types ,
'post_status' => 'publish' ,
'posts_per_page' => - 1 ,
'post_parent' => $term ,
'fields' => 'ids'
2013-11-14 12:13:34 +00:00
);
$args3 = array (
2014-05-12 13:04:36 +00:00
'post_type' => $post_types ,
'post_status' => 'publish' ,
'posts_per_page' => - 1 ,
'meta_query' => array (
2013-11-14 12:13:34 +00:00
array (
2014-05-12 13:04:36 +00:00
'key' => '_sku' ,
'value' => $term ,
'compare' => 'LIKE'
2013-11-14 12:13:34 +00:00
)
),
2014-05-12 13:04:36 +00:00
'fields' => 'ids'
2013-11-14 12:13:34 +00:00
);
2014-05-12 13:04:36 +00:00
$posts = array_unique ( array_merge ( get_posts ( $args ), get_posts ( $args2 ), get_posts ( $args3 ) ) );
2013-11-14 12:13:34 +00:00
} else {
$args = array (
2014-05-12 13:04:36 +00:00
'post_type' => $post_types ,
'post_status' => 'publish' ,
'posts_per_page' => - 1 ,
's' => $term ,
'fields' => 'ids'
2013-11-14 12:13:34 +00:00
);
$args2 = array (
2014-05-12 13:04:36 +00:00
'post_type' => $post_types ,
'post_status' => 'publish' ,
'posts_per_page' => - 1 ,
'meta_query' => array (
2013-11-14 12:13:34 +00:00
array (
2014-05-12 13:04:36 +00:00
'key' => '_sku' ,
'value' => $term ,
2013-11-14 12:13:34 +00:00
'compare' => 'LIKE'
)
),
2014-05-12 13:04:36 +00:00
'fields' => 'ids'
2013-11-14 12:13:34 +00:00
);
2014-05-12 13:04:36 +00:00
$posts = array_unique ( array_merge ( get_posts ( $args ), get_posts ( $args2 ) ) );
2013-11-14 12:13:34 +00:00
}
$found_products = array ();
2014-05-12 13:04:36 +00:00
if ( $posts ) {
foreach ( $posts as $post ) {
$product = get_product ( $post );
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
$found_products [ $post ] = $product -> get_formatted_name ();
}
2013-11-14 12:13:34 +00:00
}
$found_products = apply_filters ( 'woocommerce_json_search_found_products' , $found_products );
2014-05-31 07:32:22 +00:00
wp_send_json ( $found_products );
2013-11-14 12:13:34 +00:00
}
/**
* Search for product variations and return json
*
* @ access public
* @ return void
* @ see WC_AJAX :: json_search_products ()
*/
2014-05-28 13:52:50 +00:00
public static function json_search_products_and_variations () {
self :: json_search_products ( '' , array ( 'product' , 'product_variation' ) );
2013-11-14 12:13:34 +00:00
}
/**
* Search for customers and return json
*/
2014-05-28 13:52:50 +00:00
public static function json_search_customers () {
2013-11-14 12:13:34 +00:00
check_ajax_referer ( 'search-customers' , 'security' );
2014-01-26 09:19:17 +00:00
$term = wc_clean ( stripslashes ( $_GET [ 'term' ] ) );
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( empty ( $term ) ) {
2013-11-14 12:13:34 +00:00
die ();
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
$default = isset ( $_GET [ 'default' ] ) ? $_GET [ 'default' ] : __ ( 'Guest' , 'woocommerce' );
$found_customers = array ( '' => $default );
2014-05-28 13:52:50 +00:00
add_action ( 'pre_user_query' , array ( __CLASS__ , 'json_search_customer_name' ) );
2013-11-14 12:13:34 +00:00
2014-03-10 21:54:27 +00:00
$customers_query = new WP_User_Query ( apply_filters ( 'woocommerce_json_search_customers_query' , array (
2014-05-12 13:04:36 +00:00
'fields' => 'all' ,
'orderby' => 'display_name' ,
'search' => '*' . $term . '*' ,
'search_columns' => array ( 'ID' , 'user_login' , 'user_email' , 'user_nicename' )
2014-03-10 21:54:27 +00:00
) ) );
2013-11-14 12:13:34 +00:00
2014-05-28 13:52:50 +00:00
remove_action ( 'pre_user_query' , array ( __CLASS__ , 'json_search_customer_name' ) );
2013-11-14 12:13:34 +00:00
$customers = $customers_query -> get_results ();
if ( $customers ) {
foreach ( $customers as $customer ) {
$found_customers [ $customer -> ID ] = $customer -> display_name . ' (#' . $customer -> ID . ' – ' . sanitize_email ( $customer -> user_email ) . ')' ;
}
}
2014-05-31 07:32:22 +00:00
wp_send_json ( $found_customers );
2013-11-14 12:13:34 +00:00
}
2013-11-26 13:54:34 +00:00
/**
* Search for downloadable product variations and return json
*
* @ access public
* @ return void
* @ see WC_AJAX :: json_search_products ()
*/
2014-05-28 13:52:50 +00:00
public static function json_search_downloadable_products_and_variations () {
2014-01-26 09:19:17 +00:00
$term = ( string ) wc_clean ( stripslashes ( $_GET [ 'term' ] ) );
2013-11-26 13:54:34 +00:00
$args = array (
2014-05-12 13:04:36 +00:00
'post_type' => array ( 'product' , 'product_variation' ),
'posts_per_page' => - 1 ,
'post_status' => 'publish' ,
'order' => 'ASC' ,
'orderby' => 'parent title' ,
'meta_query' => array (
2013-11-26 13:54:34 +00:00
array (
2014-05-12 13:04:36 +00:00
'key' => '_downloadable' ,
2013-11-26 13:54:34 +00:00
'value' => 'yes'
)
),
2014-05-12 13:04:36 +00:00
's' => $term
2013-11-26 13:54:34 +00:00
);
2014-05-12 13:04:36 +00:00
2013-11-26 13:54:34 +00:00
$posts = get_posts ( $args );
$found_products = array ();
2014-05-12 13:04:36 +00:00
if ( $posts ) {
foreach ( $posts as $post ) {
$product = get_product ( $post -> ID );
$found_products [ $post -> ID ] = $product -> get_formatted_name ();
}
2013-11-26 13:54:34 +00:00
}
2014-05-31 07:32:22 +00:00
wp_send_json ( $found_products );
2013-11-26 13:54:34 +00:00
}
2013-11-14 12:13:34 +00:00
/**
* When searching using the WP_User_Query , search names ( user meta ) too
* @ param object $query
* @ return object
*/
2014-05-28 13:52:50 +00:00
public static function json_search_customer_name ( $query ) {
2013-11-14 12:13:34 +00:00
global $wpdb ;
2014-01-26 09:19:17 +00:00
$term = wc_clean ( stripslashes ( $_GET [ 'term' ] ) );
2013-11-14 12:13:34 +00:00
2014-04-15 10:10:57 +00:00
$query -> query_from .= " INNER JOIN { $wpdb -> usermeta } AS user_name ON { $wpdb -> users } .ID = user_name.user_id AND ( user_name.meta_key = 'first_name' OR user_name.meta_key = 'last_name' ) " ;
$query -> query_where .= $wpdb -> prepare ( " OR user_name.meta_value LIKE %s " , '%' . like_escape ( $term ) . '%' );
2013-11-14 12:13:34 +00:00
}
/**
* Ajax request handling for categories ordering
*/
2014-05-28 13:52:50 +00:00
public static function term_ordering () {
2014-05-12 13:04:36 +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 );
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( ! $id || ! $term || ! $taxonomy ) {
die ( 0 );
}
2013-11-14 12:13:34 +00:00
2013-12-05 16:07:44 +00:00
wc_reorder_terms ( $term , $next_id , $taxonomy );
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
$children = get_terms ( $taxonomy , " child_of= $id &menu_order=ASC&hide_empty=0 " );
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
if ( $term && sizeof ( $children ) ) {
2013-11-14 12:13:34 +00:00
echo 'children' ;
2014-05-12 13:04:36 +00:00
die ();
2013-11-14 12:13:34 +00:00
}
}
/**
* Ajax request handling for product ordering
*
* Based on Simple Page Ordering by 10 up ( http :// wordpress . org / extend / plugins / simple - page - ordering / )
*/
2014-05-28 13:52:50 +00:00
public static function product_ordering () {
2013-11-14 12:13:34 +00:00
global $wpdb ;
// check permissions again and make sure we have what we need
2014-05-12 13:04:36 +00:00
if ( ! current_user_can ( 'edit_products' ) || empty ( $_POST [ 'id' ] ) || ( ! isset ( $_POST [ 'previd' ] ) && ! isset ( $_POST [ 'nextid' ] ) ) ) {
2013-11-14 12:13:34 +00:00
die ( - 1 );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
// real post?
2014-05-12 13:04:36 +00:00
if ( ! $post = get_post ( $_POST [ 'id' ] ) ) {
2013-11-14 12:13:34 +00:00
die ( - 1 );
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
2014-05-12 13:04:36 +00:00
$previd = isset ( $_POST [ 'previd' ] ) ? $_POST [ 'previd' ] : false ;
$nextid = isset ( $_POST [ 'nextid' ] ) ? $_POST [ 'nextid' ] : false ;
2013-11-14 12:13:34 +00:00
$new_pos = array (); // store new positions for ajax
$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 )
ORDER BY posts . menu_order ASC , posts . ID DESC
' , $wpdb -> posts , $post -> ID ) );
$menu_order = 0 ;
2014-05-12 13:04:36 +00:00
foreach ( $siblings as $sibling ) {
2013-11-14 12:13:34 +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 ) {
$wpdb -> update (
$wpdb -> posts ,
array (
'menu_order' => $menu_order
),
array ( 'ID' => $post -> ID ),
array ( '%d' ),
array ( '%d' )
);
$new_pos [ $post -> ID ] = $menu_order ;
$menu_order ++ ;
}
// if repositioned post has been set, and new items are already in the right order, we can stop
2014-05-12 13:04:36 +00:00
if ( isset ( $new_pos [ $post -> ID ] ) && $sibling -> menu_order >= $menu_order ) {
2013-11-14 12:13:34 +00:00
break ;
2014-05-12 13:04:36 +00:00
}
2013-11-14 12:13:34 +00:00
// set the menu order of the current sibling and increment the menu order
$wpdb -> update (
$wpdb -> posts ,
array (
'menu_order' => $menu_order
),
array ( 'ID' => $sibling -> ID ),
array ( '%d' ),
array ( '%d' )
);
$new_pos [ $sibling -> ID ] = $menu_order ;
$menu_order ++ ;
if ( ! $nextid && $previd == $sibling -> ID ) {
$wpdb -> update (
$wpdb -> posts ,
array (
'menu_order' => $menu_order
),
array ( 'ID' => $post -> ID ),
array ( '%d' ),
array ( '%d' )
);
$new_pos [ $post -> ID ] = $menu_order ;
$menu_order ++ ;
}
}
2014-05-31 07:32:22 +00:00
wp_send_json ( $new_pos );
2014-07-10 12:33:05 +00:00
}
/**
* Handle a refund via the edit order screen
*/
public static function refund_line_items () {
check_ajax_referer ( 'order-item' , 'security' );
2014-07-24 14:34:14 +00:00
$order_id = absint ( $_POST [ 'order_id' ] );
$refund_amount = sanitize_text_field ( $_POST [ 'refund_amount' ] );
$refund_reason = sanitize_text_field ( $_POST [ 'refund_reason' ] );
$line_item_qtys = json_decode ( sanitize_text_field ( stripslashes ( $_POST [ 'line_item_qtys' ] ) ), true );
$line_item_totals = json_decode ( sanitize_text_field ( stripslashes ( $_POST [ 'line_item_totals' ] ) ), true );
$line_item_tax_totals = json_decode ( sanitize_text_field ( stripslashes ( $_POST [ 'line_item_tax_totals' ] ) ), true );
$api_refund = $_POST [ 'api_refund' ] === 'true' ? true : false ;
2014-05-31 07:32:22 +00:00
2014-07-10 13:49:04 +00:00
try {
// Validate that the refund can occur
$order = get_order ( $order_id );
$max_refund = $order -> get_total () - $order -> get_total_refunded ();
if ( ! $refund_amount || $max_refund < $refund_amount ) {
throw new exception ( __ ( 'Invalid refund amount' , 'woocommerce' ) );
}
2014-07-16 18:41:18 +00:00
2014-07-24 14:34:14 +00:00
// Prepare line items which we are refunding
$line_items = array ();
$item_ids = array_unique ( array_merge ( array_keys ( $line_item_qtys , $line_item_totals ) ) );
foreach ( $item_ids as $item_id ) {
$line_items [ $item_id ] = array ( 'qty' => 0 , 'refund_total' => 0 , 'refund_tax' => array () );
}
foreach ( $line_item_qtys as $item_id => $qty ) {
$line_items [ $item_id ][ 'qty' ] = max ( $qty , 0 );
}
foreach ( $line_item_totals as $item_id => $total ) {
$line_items [ $item_id ][ 'refund_total' ] = $total ;
}
foreach ( $line_item_tax_totals as $item_id => $tax_totals ) {
$line_items [ $item_id ][ 'refund_tax' ] = $tax_totals ;
}
2014-07-10 13:49:04 +00:00
// Create the refund object
$refund = wc_create_refund ( array (
2014-07-24 14:34:14 +00:00
'amount' => $refund_amount ,
'reason' => $refund_reason ,
'order_id' => $order_id ,
'line_items' => $line_items
2014-07-10 13:49:04 +00:00
) );
if ( is_wp_error ( $refund ) ) {
throw new exception ( $refund -> get_error_message () );
}
// Refund via API
if ( $api_refund ) {
if ( WC () -> payment_gateways () ) {
$payment_gateways = WC () -> payment_gateways -> payment_gateways ();
}
if ( isset ( $payment_gateways [ $order -> payment_method ] ) && $payment_gateways [ $order -> payment_method ] -> supports ( 'refunds' ) ) {
$result = $payment_gateways [ $order -> payment_method ] -> process_refund ( $order_id , $refund_amount );
if ( is_wp_error ( $result ) ) {
throw new exception ( $result -> get_error_message () );
} elseif ( ! $result ) {
throw new exception ( __ ( 'Refund failed' , 'woocommerce' ) );
}
}
}
2014-07-24 15:03:52 +00:00
// Clear transients
wc_delete_shop_order_transients ( $order_id );
2014-07-10 13:49:04 +00:00
wp_send_json ( true );
} catch ( Exception $e ) {
wp_send_json ( array ( 'error' => $e -> getMessage () ) );
}
2013-11-14 12:13:34 +00:00
}
2014-07-10 15:39:10 +00:00
/**
* Delete a refund
*/
public static function delete_refund () {
check_ajax_referer ( 'order-item' , 'security' );
$refund_id = absint ( $_POST [ 'refund_id' ] );
2014-07-16 18:41:18 +00:00
2014-07-11 11:43:42 +00:00
if ( $refund_id && 'shop_order_refund' === get_post_type ( $refund_id ) ) {
2014-07-10 15:39:10 +00:00
wp_delete_post ( $refund_id );
}
die ();
}
2013-11-14 12:13:34 +00:00
}
2014-05-28 13:52:50 +00:00
WC_AJAX :: init ();