woocommerce/woocommerce-ajax.php

1292 lines
36 KiB
PHP
Raw Normal View History

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
* @version 1.6.4
2011-12-08 15:13:15 +00:00
*/
2012-08-01 12:43:46 +00:00
/** Frontend AJAX events **************************************************/
/**
* Process ajax login
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_sidebar_login_ajax_process() {
check_ajax_referer( 'woocommerce-sidebar-login-action', 'security' );
2012-08-01 12:43:46 +00:00
// Get post data
$creds = array();
2012-06-10 14:14:03 +00:00
$creds['user_login'] = esc_attr($_REQUEST['user_login']);
$creds['user_password'] = esc_attr($_REQUEST['user_password']);
$creds['remember'] = 'forever';
2012-06-10 14:14:03 +00:00
$redirect_to = esc_attr($_REQUEST['redirect_to']);
2012-08-01 12:43:46 +00:00
// Check for Secure Cookie
$secure_cookie = '';
2012-08-01 12:43:46 +00:00
// If the user wants ssl but the session is not ssl, force a secure cookie.
2012-06-10 14:14:03 +00:00
if ( ! force_ssl_admin() ) {
$user_name = sanitize_user( $creds['user_login'] );
if ( $user = get_user_by('login', $user_name ) ) {
if ( get_user_option( 'use_ssl', $user->ID ) ) {
$secure_cookie = true;
force_ssl_admin(true);
}
}
}
2012-08-01 12:43:46 +00:00
2012-06-10 14:14:03 +00:00
if ( force_ssl_admin() ) $secure_cookie = true;
if ( $secure_cookie=='' && force_ssl_login() ) $secure_cookie = false;
// Login
2012-06-10 14:14:03 +00:00
$user = wp_signon( $creds, $secure_cookie );
2012-08-01 12:43:46 +00:00
// Redirect filter
2012-06-10 14:14:03 +00:00
if ( $secure_cookie && strstr($redirect_to, 'wp-admin') ) $redirect_to = str_replace('http:', 'https:', $redirect_to);
// Result
$result = array();
2012-08-01 12:43:46 +00:00
if ( !is_wp_error($user) ) :
$result['success'] = 1;
$result['redirect'] = $redirect_to;
else :
$result['success'] = 0;
2012-06-10 14:14:03 +00:00
if ( $user->errors ) {
foreach ($user->errors as $error) {
$result['error'] = $error[0];
break;
}
} else {
$result['error'] = __('Please enter your username and password to login.', 'woocommerce');
}
endif;
2012-08-01 12:43:46 +00:00
2012-06-10 14:14:03 +00:00
header('content-type: application/json; charset=utf-8');
echo $_GET['callback'] . '(' . json_encode( $result ) . ')';
die();
}
2012-08-14 16:19:22 +00:00
add_action('wp_ajax_nopriv_woocommerce_sidebar_login_process', 'woocommerce_sidebar_login_ajax_process');
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'] ) ) {
$woocommerce->cart->add_discount( stripslashes( trim( $_POST['coupon_code'] ) ) );
} else {
2012-08-01 12:43:46 +00:00
$woocommerce->add_error( __('Please enter a coupon code.', 'woocommerce') );
2012-06-10 11:40:07 +00:00
}
2012-08-01 12:43:46 +00:00
2012-06-10 11:40:07 +00:00
$woocommerce->show_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');
/**
* AJAX update shipping method on cart page
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_ajax_update_shipping_method() {
global $woocommerce;
2012-08-01 12:43:46 +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
2012-04-11 12:08:04 +00:00
if ( isset( $_POST['shipping_method'] ) ) $_SESSION['_chosen_shipping_method'] = $_POST['shipping_method'];
2012-08-01 12:43:46 +00:00
$woocommerce->cart->calculate_totals();
2012-08-01 12:43:46 +00:00
woocommerce_cart_totals();
2012-08-01 12:43:46 +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');
/**
* AJAX update order review on checkout
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_ajax_update_order_review() {
global $woocommerce;
2012-08-01 12:43:46 +00:00
check_ajax_referer( 'update-order-review', 'security' );
2012-08-01 12:43:46 +00:00
if (!defined('WOOCOMMERCE_CHECKOUT')) define('WOOCOMMERCE_CHECKOUT', true);
2012-08-01 12:43:46 +00:00
if (sizeof($woocommerce->cart->get_cart())==0) :
2012-01-12 00:54:45 +00:00
echo '<div class="woocommerce_error">'.__('Sorry, your session has expired.', 'woocommerce').' <a href="'.home_url().'">'.__('Return to homepage &rarr;', 'woocommerce').'</a></div>';
2011-12-08 15:13:15 +00:00
die();
endif;
2012-08-01 12:43:46 +00:00
do_action('woocommerce_checkout_update_order_review', $_POST['post_data']);
2012-08-01 12:43:46 +00:00
if (isset($_POST['shipping_method'])) $_SESSION['_chosen_shipping_method'] = $_POST['shipping_method'];
2012-01-05 12:11:34 +00:00
if (isset($_POST['payment_method'])) $_SESSION['_chosen_payment_method'] = $_POST['payment_method'];
if (isset($_POST['country'])) $woocommerce->customer->set_country( $_POST['country'] );
if (isset($_POST['state'])) $woocommerce->customer->set_state( $_POST['state'] );
if (isset($_POST['postcode'])) $woocommerce->customer->set_postcode( $_POST['postcode'] );
if (isset($_POST['s_country'])) $woocommerce->customer->set_shipping_country( $_POST['s_country'] );
if (isset($_POST['s_state'])) $woocommerce->customer->set_shipping_state( $_POST['s_state'] );
if (isset($_POST['s_postcode'])) $woocommerce->customer->set_shipping_postcode( $_POST['s_postcode'] );
2012-08-01 12:43:46 +00:00
$woocommerce->cart->calculate_totals();
2012-08-01 12:43:46 +00:00
do_action('woocommerce_checkout_order_review'); // Display review order table
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');
/**
* AJAX add to cart
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_ajax_add_to_cart() {
2012-08-01 12:43:46 +00:00
global $woocommerce;
2012-08-01 12:43:46 +00:00
check_ajax_referer( 'add-to-cart', 'security' );
2012-08-01 12:43:46 +00:00
2012-01-06 17:14:31 +00:00
$product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $_POST['product_id']);
2012-08-01 12:43:46 +00:00
$passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, 1);
2012-08-01 12:43:46 +00:00
if ($passed_validation && $woocommerce->cart->add_to_cart($product_id, 1)) :
// Return html fragments
$data = apply_filters('add_to_cart_fragments', array());
do_action( 'woocommerce_ajax_added_to_cart', $product_id);
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' => get_permalink( $product_id )
);
$woocommerce->set_messages();
endif;
2012-08-01 12:43:46 +00:00
echo json_encode( $data );
2012-08-01 12:43:46 +00:00
die();
}
2011-12-08 15:13:15 +00:00
2012-08-14 16:19:22 +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');
/**
* Process ajax checkout form
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
2012-01-12 00:54:45 +00:00
function woocommerce_process_checkout() {
global $woocommerce;
2012-08-01 12:43:46 +00:00
if (!defined('WOOCOMMERCE_CHECKOUT')) define('WOOCOMMERCE_CHECKOUT', true);
2012-08-01 12:43:46 +00:00
$woocommerce_checkout = $woocommerce->checkout();
$woocommerce_checkout->process_checkout();
2012-08-01 12:43:46 +00:00
die(0);
}
2012-08-14 16:19:22 +00:00
add_action('wp_ajax_woocommerce-checkout', 'woocommerce_process_checkout');
add_action('wp_ajax_nopriv_woocommerce-checkout', 'woocommerce_process_checkout');
2012-08-01 12:43:46 +00:00
/** Admin AJAX events *****************************************************/
/**
* Feature a product from admin
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
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-08-14 16:19:22 +00:00
if ( ! current_user_can('edit_posts') ) wp_die( __('You do not have sufficient permissions to access this page.', 'woocommerce') );
2012-08-01 12:43:46 +00:00
2012-08-14 16:19:22 +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
$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 );
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() ) );
}
2012-04-20 10:02:29 +00:00
add_action('wp_ajax_woocommerce-feature-product', 'woocommerce_feature_product');
2012-08-14 16:19:22 +00:00
/**
* Mark an order as complete
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_mark_order_complete() {
2012-08-14 16:19:22 +00:00
if ( !is_admin() ) die;
if ( !current_user_can('edit_posts') ) wp_die( __('You do not have sufficient permissions to access this page.', 'woocommerce') );
if ( !check_admin_referer('woocommerce-mark-order-complete')) wp_die( __('You have taken too long. Please go back and retry.', 'woocommerce') );
$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 );
$order->update_status( 'completed' );
2012-08-01 12:43:46 +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
/**
* Mark an order as processing
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_mark_order_processing() {
2012-08-14 16:19:22 +00:00
if ( !is_admin() ) die;
if ( !current_user_can('edit_posts') ) wp_die( __('You do not have sufficient permissions to access this page.', 'woocommerce') );
if ( !check_admin_referer('woocommerce-mark-order-processing')) wp_die( __('You have taken too long. Please go back and retry.', 'woocommerce') );
$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 );
$order->update_status( 'processing' );
2012-08-01 12:43:46 +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
/**
* Add a new attribute via ajax function
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_add_new_attribute() {
2012-08-01 12:43:46 +00:00
check_ajax_referer( 'add-attribute', 'security' );
2012-08-01 12:43:46 +00:00
$taxonomy = esc_attr( $_POST['taxonomy'] );
$term = stripslashes( $_POST['term'] );
2012-08-01 12:43:46 +00:00
if ( taxonomy_exists( $taxonomy ) ) {
2012-08-01 12:43:46 +00:00
$result = wp_insert_term( $term, $taxonomy );
2012-08-01 12:43:46 +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
die();
}
2012-08-14 16:19:22 +00:00
add_action('wp_ajax_woocommerce_add_new_attribute', 'woocommerce_add_new_attribute');
/**
* Delete variation via ajax function
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_remove_variation() {
2012-08-01 12:43:46 +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 );
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');
/**
* Add variation via ajax function
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_add_variation() {
2012-08-01 12:43:46 +00:00
check_ajax_referer( 'add-variation', 'security' );
2012-08-01 12:43:46 +00:00
$post_id = intval( $_POST['post_id'] );
$variation = 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'
);
$variation_id = wp_insert_post( $variation );
2012-08-01 12:43:46 +00:00
echo $variation_id;
2012-08-01 12:43:46 +00:00
die();
}
2012-08-14 16:19:22 +00:00
add_action('wp_ajax_woocommerce_add_variation', 'woocommerce_add_variation');
/**
* Link all variations via ajax function
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_link_all_variations() {
2012-08-25 10:09:53 +00:00
global $woocommerce;
check_ajax_referer( 'link-variations', 'security' );
2012-08-01 12:43:46 +00:00
@set_time_limit(0);
$post_id = intval( $_POST['post_id'] );
2012-08-01 12:43:46 +00:00
if (!$post_id) die();
2012-08-01 12:43:46 +00:00
$variations = array();
2012-08-01 12:43:46 +00:00
2012-01-27 16:38:39 +00:00
$_product = new WC_Product( $post_id );
2012-08-01 12:43:46 +00:00
// Put variation attributes into an array
foreach ($_product->get_attributes() as $attribute) :
2012-08-01 12:43:46 +00:00
if ( !$attribute['is_variation'] ) continue;
2012-08-01 12:43:46 +00:00
$attribute_field_name = 'attribute_' . sanitize_title($attribute['name']);
2012-08-01 12:43:46 +00:00
if ($attribute['is_taxonomy']) :
$post_terms = wp_get_post_terms( $post_id, $attribute['name'] );
$options = array();
foreach ($post_terms as $term) :
$options[] = $term->slug;
endforeach;
else :
$options = explode('|', $attribute['value']);
endif;
2012-08-01 12:43:46 +00:00
$options = array_map('trim', $options);
2012-08-01 12:43:46 +00:00
$variations[$attribute_field_name] = $options;
2012-08-01 12:43:46 +00:00
endforeach;
2012-08-01 12:43:46 +00:00
// Quit out if none were found
if (sizeof($variations)==0) die();
2012-08-01 12:43:46 +00:00
// Get existing variations so we don't create duplicated
$available_variations = array();
2012-08-01 12:43:46 +00:00
foreach($_product->get_children() as $child_id) {
$child = $_product->get_child( $child_id );
2012-08-14 16:19:22 +00:00
if ($child instanceof WC_Product_Variation) {
$available_variations[] = $child->get_variation_attributes();
}
}
2012-08-01 12:43:46 +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
// Now find all combinations and create posts
if (!function_exists('array_cartesian')) {
function array_cartesian($input) {
$result = array();
2012-08-01 12:43:46 +00:00
while (list($key, $values) = each($input)) {
// If a sub-array is empty, it doesn't affect the cartesian product
if (empty($values)) {
continue;
}
2012-08-01 12:43:46 +00:00
// 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
2012-08-01 12:43:46 +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();
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);
2012-08-01 12:43:46 +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
// Do step 2 above.
foreach($values as $item) {
$copy[$key] = $item;
$append[] = $copy;
}
2012-08-01 12:43:46 +00:00
// Undo the side effecst of array_shift
array_unshift($values, $product[$key]);
}
2012-08-01 12:43:46 +00:00
// Out of the foreach, we can add to $results now
$result = array_merge($result, $append);
}
}
2012-08-01 12:43:46 +00:00
return $result;
}
2011-12-08 15:13:15 +00:00
}
2012-08-01 12:43:46 +00:00
$variation_ids = array();
2012-01-12 11:09:54 +00:00
$added = 0;
$possible_variations = array_cartesian( $variations );
2012-08-01 12:43:46 +00:00
foreach ($possible_variations as $variation) :
2012-08-01 12:43:46 +00:00
// Check if variation already exists
if (in_array($variation, $available_variations)) continue;
2012-08-01 12:43:46 +00:00
$variation_id = wp_insert_post( $variation_post_data );
2012-08-01 12:43:46 +00:00
$variation_ids[] = $variation_id;
2012-08-01 12:43:46 +00:00
foreach ($variation as $key => $value) :
2012-08-01 12:43:46 +00:00
update_post_meta( $variation_id, $key, $value );
2012-08-01 12:43:46 +00:00
2011-12-08 15:13:15 +00:00
endforeach;
2012-08-01 12:43:46 +00:00
2012-01-12 11:09:54 +00:00
$added++;
2012-08-01 12:43:46 +00:00
2012-01-12 11:09:54 +00:00
// Max 100
if ($added>49) break;
2012-08-01 12:43:46 +00:00
endforeach;
2012-08-25 10:09:53 +00:00
$woocommerce->clear_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
die();
}
2012-08-14 16:19:22 +00:00
add_action( 'wp_ajax_woocommerce_link_all_variations', 'woocommerce_link_all_variations' );
/**
* Delete download permissions via ajax function
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_revoke_access_to_download() {
2012-08-01 12:43:46 +00:00
check_ajax_referer( 'revoke-access', 'security' );
2012-08-01 12:43:46 +00:00
global $wpdb;
2012-08-01 12:43:46 +00:00
$product_id = intval( $_POST['product_id'] );
$order_id = intval( $_POST['order_id'] );
2012-08-01 12:43:46 +00:00
$wpdb->query("
DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE order_id = $order_id
AND product_id = $product_id
");
2012-08-01 12:43:46 +00:00
die();
}
2012-08-14 16:19:22 +00:00
add_action('wp_ajax_woocommerce_revoke_access_to_download', 'woocommerce_revoke_access_to_download');
/**
* Grant download permissions via ajax function
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_grant_access_to_download() {
2012-08-01 12:43:46 +00:00
check_ajax_referer( 'grant-access', 'security' );
2012-08-01 12:43:46 +00:00
global $wpdb;
2012-08-01 12:43:46 +00:00
$product_id = intval( $_POST['product_id'] );
$order_id = intval( $_POST['order_id'] );
2012-08-01 12:43:46 +00:00
$order = new WC_Order( $order_id );
2012-08-01 12:43:46 +00:00
$user_email = $order->billing_email;
2012-08-01 12:43:46 +00:00
$limit = trim(get_post_meta($product_id, '_download_limit', true));
$expiry = trim(get_post_meta($product_id, '_download_expiry', true));
2012-08-01 12:43:46 +00:00
$limit = (empty($limit)) ? '' : (int) $limit;
// Default value is NULL in the table schema
$expiry = (empty($expiry)) ? null : (int) $expiry;
2012-08-01 12:43:46 +00:00
if ($expiry) $expiry = date_i18n( "Y-m-d", strtotime( 'NOW + ' . $expiry . ' DAY' ) );
2012-08-01 12:43:46 +00:00
$wpdb->hide_errors();
$data = array(
2012-08-01 12:43:46 +00:00
'product_id' => $product_id,
2012-05-12 17:45:26 +00:00
'user_id' => (int) $order->user_id,
'user_email' => $user_email,
'order_id' => $order->id,
'order_key' => $order->order_key,
'downloads_remaining' => $limit,
'access_granted' => current_time('mysql'),
'download_count' => 0
);
$format = array(
2012-08-01 12:43:46 +00:00
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%d'
);
if ( ! is_null($expiry)) {
$data['access_expires'] = $expiry;
$format[] = '%s';
}
// Downloadable product - give access to the customer
2012-08-01 12:43:46 +00:00
$success = $wpdb->insert( $wpdb->prefix . 'woocommerce_downloadable_product_permissions',
$data,
$format
2012-08-01 12:43:46 +00:00
);
if ($success) {
echo json_encode(array(
'success' => 1,
'download_id' => $product_id,
'title' => get_the_title($product_id),
'expires' => is_null($expiry) ? '' : $expiry,
'remaining' => $limit
));
}
2012-08-01 12:43:46 +00:00
die();
}
2012-08-14 16:19:22 +00:00
add_action('wp_ajax_woocommerce_grant_access_to_download', 'woocommerce_grant_access_to_download');
/**
* Get customer details via ajax
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_get_customer_details() {
2012-08-01 12:43:46 +00:00
global $woocommerce;
check_ajax_referer( 'get-customer-details', 'security' );
$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
$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
echo json_encode($customer_data);
2012-08-01 12:43:46 +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');
/**
* Add order item via ajax
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_add_order_item() {
2012-01-13 21:25:39 +00:00
global $woocommerce, $wpdb;
check_ajax_referer( 'add-order-item', 'security' );
2012-08-01 12:43:46 +00:00
$index = trim(stripslashes($_POST['index']));
$item_to_add = trim(stripslashes($_POST['item_to_add']));
2012-08-01 12:43:46 +00:00
$post = '';
2012-08-01 12:43:46 +00:00
// Find the item
if (is_numeric($item_to_add)) :
$post = get_post( $item_to_add );
endif;
2012-08-01 12:43:46 +00:00
if (!$post || ($post->post_type!=='product' && $post->post_type!=='product_variation')) :
$post_id = $wpdb->get_var($wpdb->prepare("
SELECT post_id
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
2011-12-24 17:05:51 +00:00
WHERE $wpdb->postmeta.meta_key = '_sku'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'shop_product'
AND $wpdb->postmeta.meta_value = %s
LIMIT 1
"), $item_to_add );
$post = get_post( $post_id );
endif;
2012-08-01 12:43:46 +00:00
if (!$post || ($post->post_type!=='product' && $post->post_type!=='product_variation')) :
2011-12-08 15:13:15 +00:00
die();
endif;
2012-08-01 12:43:46 +00:00
if ($post->post_type=="product") :
2012-01-27 16:38:39 +00:00
$_product = new WC_Product( $post->ID );
else :
2012-01-27 16:38:39 +00:00
$_product = new WC_Product_Variation( $post->ID );
endif;
?>
<tr class="item" rel="<?php echo $index; ?>">
2012-04-12 17:14:46 +00:00
<td class="thumb">
<a href="<?php echo esc_url( admin_url('post.php?post='. $_product->id .'&action=edit') ); ?>" class="tips" data-tip="<?php
2012-01-05 11:31:22 +00:00
echo '<strong>'.__('Product ID:', 'woocommerce').'</strong> '. $_product->id;
2012-01-13 21:25:39 +00:00
echo '<br/><strong>'.__('Variation ID:', 'woocommerce').'</strong> '; if (isset($_product->variation_id) && $_product->variation_id) echo $_product->variation_id; else echo '-';
2012-01-05 11:31:22 +00:00
echo '<br/><strong>'.__('Product SKU:', 'woocommerce').'</strong> '; if ($_product->sku) echo $_product->sku; else echo '-';
2012-04-12 17:14:46 +00:00
?>"><?php echo $_product->get_image(); ?></a>
</td>
2012-04-12 17:14:46 +00:00
<td class="sku" width="1%">
2011-12-31 19:03:41 +00:00
<?php if ($_product->sku) echo $_product->sku; else echo '-'; ?>
<input type="hidden" class="item_id" name="item_id[<?php echo $index; ?>]" value="<?php echo esc_attr( $_product->id ); ?>" />
2011-12-31 19:03:41 +00:00
<input type="hidden" name="item_name[<?php echo $index; ?>]" value="<?php echo esc_attr( $_product->get_title() ); ?>" />
<input type="hidden" name="item_variation[<?php echo $index; ?>]" value="<?php if (isset($_product->variation_id)) echo $_product->variation_id; ?>" />
</td>
2011-12-31 19:03:41 +00:00
<td class="name">
2012-08-01 12:43:46 +00:00
2011-12-31 19:03:41 +00:00
<div class="row-actions">
2012-01-05 11:31:22 +00:00
<span class="trash"><a class="remove_row" href="#"><?php _e('Delete item', 'woocommerce'); ?></a> | </span>
<span class="view"><a href="<?php echo esc_url( admin_url('post.php?post='. $_product->id .'&action=edit') ); ?>"><?php _e('View product', 'woocommerce'); ?></a>
2011-12-31 19:03:41 +00:00
</div>
2012-08-01 12:43:46 +00:00
2011-12-31 19:03:41 +00:00
<?php echo $_product->get_title(); ?>
<?php if (isset($_product->variation_data)) echo '<br/>' . woocommerce_get_formatted_variation( $_product->variation_data, true ); ?>
<table class="meta" cellspacing="0">
<tfoot>
<tr>
2012-01-05 11:31:22 +00:00
<td colspan="3"><button class="add_meta button"><?php _e('Add&nbsp;meta', 'woocommerce'); ?></button></td>
</tr>
</tfoot>
<tbody class="meta_items"></tbody>
</table>
</td>
2012-08-01 12:43:46 +00:00
<?php do_action('woocommerce_admin_order_item_values', $_product); ?>
2012-08-01 12:43:46 +00:00
<td class="tax_class" width="1%">
<select class="tax_class" name="item_tax_class[<?php echo $loop; ?>]">
2012-08-01 12:43:46 +00:00
<?php
2011-12-31 19:03:41 +00:00
$tax_classes = array_filter(array_map('trim', explode("\n", get_option('woocommerce_tax_classes'))));
$classes_options = array();
2012-01-05 11:31:22 +00:00
$classes_options[''] = __('Standard', 'woocommerce');
2011-12-31 19:03:41 +00:00
if ($tax_classes) foreach ($tax_classes as $class) :
$classes_options[sanitize_title($class)] = $class;
endforeach;
foreach ($classes_options as $value => $name) echo '<option value="'. $value .'" '.selected( $value, $_product->get_tax_status(), false ).'>'. $name .'</option>';
?>
</select>
</td>
2012-08-01 12:43:46 +00:00
2011-12-31 19:03:41 +00:00
<td class="quantity" width="1%">
2012-03-05 10:15:19 +00:00
<input type="text" name="item_quantity[<?php echo $index; ?>]" placeholder="0" value="1" size="2" class="quantity" />
2011-12-31 19:03:41 +00:00
</td>
2012-08-01 12:43:46 +00:00
<td class="line_subtotal" width="1%">
2012-07-26 15:51:30 +00:00
<label><?php _e('Cost', 'woocommerce'); ?>: <input type="text" name="line_subtotal[<?php echo $index; ?>]" placeholder="0.00" value="<?php echo esc_attr( number_format( (double) $_product->get_price_excluding_tax(), 2, '.', '' ) ); ?>" class="line_subtotal" /></label>
2012-08-01 12:43:46 +00:00
2012-03-05 10:15:19 +00:00
<label><?php _e('Tax', 'woocommerce'); ?>: <input type="text" name="line_subtotal_tax[<?php echo $index; ?>]" placeholder="0.00" class="line_subtotal_tax" /></label>
2011-12-31 19:03:41 +00:00
</td>
2012-08-01 12:43:46 +00:00
<td class="line_total" width="1%">
2012-07-26 15:51:30 +00:00
<label><?php _e('Cost', 'woocommerce'); ?>: <input type="text" name="line_total[<?php echo $index; ?>]" placeholder="0.00" value="<?php echo esc_attr( number_format( (double) $_product->get_price_excluding_tax(), 2, '.', '' ) ); ?>" class="line_total" /></label>
2012-08-01 12:43:46 +00:00
2012-03-05 10:15:19 +00:00
<label><?php _e('Tax', 'woocommerce'); ?>: <input type="text" name="line_tax[<?php echo $index; ?>]" placeholder="0.00" class="line_tax" /></label>
2011-12-31 19:03:41 +00:00
</td>
2012-08-01 12:43:46 +00:00
</tr>
<?php
2012-08-01 12:43:46 +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_add_order_item', 'woocommerce_add_order_item');
2012-01-13 21:25:39 +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() {
global $woocommerce;
check_ajax_referer( 'calc-totals', 'security' );
2012-08-01 12:43:46 +00:00
2012-01-27 16:38:39 +00:00
$tax = new WC_Tax();
2012-08-01 12:43:46 +00:00
$base_tax_amount = 0;
$line_tax_amount = 0;
2012-08-01 12:43:46 +00:00
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-08-01 12:43:46 +00:00
2012-06-21 12:06:48 +00:00
$line_subtotal = esc_attr( $_POST['line_subtotal'] );
$line_total = esc_attr( $_POST['line_total'] );
2012-08-01 12:43:46 +00:00
2012-06-21 12:06:48 +00:00
$item_id = esc_attr( $_POST['item_id'] );
$tax_class = esc_attr( $_POST['tax_class'] );
2012-08-01 12:43:46 +00:00
2012-06-21 12:06:48 +00:00
if ( ! $item_id ) return;
2012-08-01 12:43:46 +00:00
// Get product details
2012-06-21 12:06:48 +00:00
$_product = new WC_Product( $item_id );
$item_tax_status = $_product->get_tax_status();
2012-08-01 12:43:46 +00:00
2012-06-21 12:06:48 +00:00
if ( $item_tax_status == 'taxable' ) {
2012-08-01 12:43:46 +00:00
$tax_rates = $tax->find_rates( $country, $state, $postcode, $tax_class );
2012-08-01 12:43:46 +00:00
2012-06-21 12:06:48 +00:00
$line_subtotal_tax_amount = rtrim( rtrim( number_format( array_sum( $tax->calc_tax( $line_subtotal, $tax_rates, false ) ), 4, '.', '' ), '0' ), '.' );
$line_tax_amount = rtrim( rtrim( number_format( array_sum( $tax->calc_tax( $line_total, $tax_rates, false ) ), 4, '.', '' ), '0' ), '.' );
2012-08-01 12:43:46 +00:00
2012-06-21 12:06:48 +00:00
}
2012-08-01 12:43:46 +00:00
2012-06-21 12:06:48 +00:00
if ( $line_subtotal_tax_amount < 0 ) $line_subtotal_tax_amount = 0;
if ( $line_tax_amount < 0 ) $line_tax_amount = 0;
2012-08-01 12:43:46 +00:00
2012-06-21 12:06:48 +00:00
echo json_encode( array(
'line_subtotal_tax' => $line_subtotal_tax_amount,
'line_tax' => $line_tax_amount
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');
/**
* Add order note via ajax
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_add_order_note() {
2012-08-01 12:43:46 +00:00
global $woocommerce;
check_ajax_referer( 'add-order-note', 'security' );
2012-08-01 12:43:46 +00:00
$post_id = (int) $_POST['post_id'];
$note = wp_kses( trim( stripslashes( $_POST['note'] ) ), array( 'a' => array( 'href' => array(), 'title' => array() ), 'br' => array(), 'em' => array(), 'strong' => array() ) );
$note_type = $_POST['note_type'];
2012-08-01 12:43:46 +00:00
$is_customer_note = $note_type == 'customer' ? 1 : 0;
2012-08-01 12:43:46 +00:00
if ( $post_id > 0 ) {
2012-01-27 16:38:39 +00:00
$order = new WC_Order( $post_id );
$comment_id = $order->add_order_note( $note, $is_customer_note );
2012-08-01 12:43:46 +00:00
echo '<li rel="'.$comment_id.'" class="note ';
if ($is_customer_note) echo 'customer-note';
echo '"><div class="note_content">';
echo wpautop( wptexturize( $note ) );
2012-02-24 14:33:02 +00:00
echo '</div><p class="meta"><a href="#" class="delete_note">'.__('Delete note', 'woocommerce').'</a></p>';
echo '</li>';
2012-08-01 12:43:46 +00:00
}
2012-08-01 12:43:46 +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');
/**
* Delete order note via ajax
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_delete_order_note() {
2012-08-01 12:43:46 +00:00
global $woocommerce;
check_ajax_referer( 'delete-order-note', 'security' );
2012-08-01 12:43:46 +00:00
$note_id = (int) $_POST['note_id'];
2012-08-01 12:43:46 +00:00
if ($note_id>0) :
wp_delete_comment( $note_id );
endif;
2012-08-01 12:43:46 +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');
/**
* 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
*/
function woocommerce_json_search_products( $x = '', $post_types = array('product') ) {
check_ajax_referer( 'search-products', 'security' );
2012-08-01 12:43:46 +00:00
$term = (string) urldecode(stripslashes(strip_tags($_GET['term'])));
2012-08-01 12:43:46 +00:00
if (empty($term)) die();
2012-08-01 12:43:46 +00:00
if ( is_numeric( $term ) ) {
2012-08-01 12:43:46 +00:00
$args = array(
'post_type' => $post_types,
'post_status' => 'publish',
'posts_per_page' => -1,
'post__in' => array(0, $term),
'fields' => 'ids'
);
2012-08-01 12:43:46 +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
} else {
2012-08-01 12:43:46 +00:00
$args = array(
'post_type' => $post_types,
'post_status' => 'publish',
'posts_per_page' => -1,
's' => $term,
'fields' => 'ids'
);
2012-08-01 12:43:46 +00:00
$args2 = array(
'post_type' => $post_types,
'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
$posts = array_unique(array_merge( get_posts( $args ), get_posts( $args2 ) ));
2012-08-01 12:43:46 +00:00
}
2012-08-01 12:43:46 +00:00
$found_products = array();
if ($posts) foreach ($posts as $post) {
2012-08-01 12:43:46 +00:00
$SKU = get_post_meta($post, '_sku', true);
2012-08-01 12:43:46 +00:00
if (isset($SKU) && $SKU) $SKU = ' (SKU: ' . $SKU . ')';
2012-08-01 12:43:46 +00:00
$found_products[$post] = get_the_title( $post ) . ' &ndash; #' . $post . $SKU;
2012-08-01 12:43:46 +00:00
}
2012-08-01 12:43:46 +00:00
echo json_encode( $found_products );
2012-08-01 12:43:46 +00:00
die();
}
2012-08-14 16:19:22 +00:00
add_action('wp_ajax_woocommerce_json_search_products', 'woocommerce_json_search_products');
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() {
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
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
$default = isset( $_GET['default'] ) ? $_GET['default'] : __('Guest', 'woocommerce');
$found_customers = array( '' => $default );
$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
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 ) {
$found_customers[ $customer->ID ] = $customer->display_name . ' (#' . $customer->ID . ' &ndash; ' . $customer->user_email . ')';
}
}
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');
2012-07-31 17:26:25 +00:00
/**
* Search for products for upsells/crosssells
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_upsell_crosssell_search_products() {
2012-08-01 12:43:46 +00:00
check_ajax_referer( 'search-products', 'security' );
2012-08-01 12:43:46 +00:00
$search = (string) urldecode(stripslashes(strip_tags($_POST['search'])));
$name = (string) urldecode(stripslashes(strip_tags($_POST['name'])));
2012-08-01 12:43:46 +00:00
if (empty($search)) die();
2012-08-01 12:43:46 +00:00
if (is_numeric($search)) :
2012-08-01 12:43:46 +00:00
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 15,
'post__in' => array(0, $search)
);
2012-08-01 12:43:46 +00:00
else :
2012-08-01 12:43:46 +00:00
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 15,
's' => $search
);
2012-08-01 12:43:46 +00:00
endif;
2012-08-01 12:43:46 +00:00
2012-04-12 10:25:50 +00:00
$posts = apply_filters('woocommerce_upsell_crosssell_search_products', get_posts( $args ));
2012-08-01 12:43:46 +00:00
if ($posts) : foreach ($posts as $post) :
$SKU = get_post_meta($post->ID, '_sku', true);
2012-08-01 12:43:46 +00:00
2011-12-08 15:13:15 +00:00
?>
2012-01-05 11:31:22 +00:00
<li rel="<?php echo $post->ID; ?>"><button type="button" name="Add" class="button add_crosssell" title="Add"><?php _e('Cross-sell', 'woocommerce'); ?> &rarr;</button><button type="button" name="Add" class="button add_upsell" title="Add"><?php _e('Up-sell', 'woocommerce'); ?> &rarr;</button><strong><?php echo $post->post_title; ?></strong> &ndash; #<?php echo $post->ID; ?> <?php if (isset($SKU) && $SKU) echo 'SKU: '.$SKU; ?><input type="hidden" class="product_id" value="0" /></li>
2011-12-08 15:13:15 +00:00
<?php
2012-08-01 12:43:46 +00:00
endforeach; else :
?><li><?php _e('No products found', 'woocommerce'); ?></li><?php
endif;
die();
}
2012-08-14 16:19:22 +00:00
add_action('wp_ajax_woocommerce_upsell_crosssell_search_products', 'woocommerce_upsell_crosssell_search_products');
/**
* Ajax request handling for categories ordering
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_term_ordering() {
global $wpdb;
2012-08-01 12:43:46 +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
woocommerce_order_terms( $term, $next_id, $taxonomy );
2012-08-01 12:43:46 +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) ) {
echo 'children';
2012-08-01 12:43:46 +00:00
die;
2011-12-08 15:13:15 +00:00
}
}
2012-08-14 16:19:22 +00:00
add_action('wp_ajax_woocommerce-term-ordering', 'woocommerce_term_ordering');
/**
* Ajax request handling for product ordering
*
* Based on Simple Page Ordering by 10up (http://wordpress.org/extend/plugins/simple-page-ordering/)
2012-08-14 16:19:22 +00:00
*
* @access public
* @return void
*/
function woocommerce_product_ordering() {
2012-05-25 11:34:33 +00:00
global $wpdb;
2012-08-01 12:43:46 +00:00
// check permissions again and make sure we have what we need
if ( ! current_user_can('edit_others_pages') || empty( $_POST['id'] ) || ( ! isset( $_POST['previd'] ) && ! isset( $_POST['nextid'] ) ) )
die(-1);
2012-08-01 12:43:46 +00:00
// real post?
if ( ! $post = get_post( $_POST['id'] ) )
die(-1);
2012-08-01 12:43:46 +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-05-25 11:34:33 +00:00
$siblings = $wpdb->get_results("
SELECT ID, menu_order FROM {$wpdb->posts} AS posts
WHERE posts.post_type = 'product'
AND posts.post_status IN ( 'publish', 'pending', 'draft', 'future', 'private' )
AND posts.ID NOT IN ( {$post->ID} )
ORDER BY posts.menu_order ASC, posts.post_title ASC
");
2012-08-01 12:43:46 +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
// 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;
$menu_order++;
}
2012-08-01 12:43:46 +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 )
break;
2012-08-01 12:43:46 +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;
$menu_order++;
2012-08-01 12:43:46 +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
);
$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-08-14 16:19:22 +00:00
add_action( 'wp_ajax_woocommerce_product_ordering', 'woocommerce_product_ordering' );