Adding an item takes eight clicks, in the WC admin order page, so I thought I would add custom buttons for some of my most commonly-added products to do it all at once. I found several hints that get me most of the way there -- the item is indeed added to the order, but it doesn't appear until you reload the page, or hit Recalculate. Calling $('.calculate-action').trigger('click'); at that point does work but that requires yet another click ("OK"), an annoyance I am trying to avoid. The regular "Add Item(s)" and then "Add products" ... "Add" (with all the faffing about to select the product) just gets on with it at this point. How?
Is there a different approach, or maybe one more call I need to add?
scna_orderadmin_add_product.js:
(function( $ ) {
'use strict';
$('.inside').on('click','#scna_item_button', function(){
// get the order_id from the button tag
var order_id = $(this).data('order_id');
// get the product_id from the button tag
var product_id = $(this).data('product_id');
// send the data via ajax to the sever
$.ajax({
type: 'POST',
url: mb_script_var.ajaxurl,
dataType: 'json',
data: {
action: 'scna_orderadmin_add_product',
order_id: order_id,
product_id: product_id
},
success: function (data, textStatus, XMLHttpRequest) {
// if(data.error == 0)
// {
// trigger the "Recalculate" button to recalculate the order price
// and to show the new product in the item list
// $('.calculate-action').trigger('click');
// }
// show the control message
alert(data.msg);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
});
})( jQuery );
functions.php:
/** Add a custom button to the admin order page to add SCNA Membership.
*
* To save clicks when creating a new order from a paper form.
*/
function scna_orderadmin_add_product_buttons($order)
{
// Add a button that adds SCNA 1- and 2-year memberships, to save steps.
// the button tag gets as data attributes your product id and the order id
// later we will grab this data and send them to the server
echo '<button id="scna_item_button" type="button" class="button add-special-item" data-order_id="'. esc_attr($order->get_id()) .'" data-product_id="112" >Add SCNA 1-year</button>';
echo '<button id="scna_item_button" type="button" class="button add-special-item" data-order_id="'. esc_attr($order->get_id()) .'" data-product_id="113" >Add SCNA 2-year</button>';
}
/**
* hook to add the button
*/
add_action('woocommerce_order_item_add_action_buttons', 'scna_orderadmin_add_product_buttons');
/**
* Add javascript
*/
function scna_orderadmin_add_product_js_file()
{
wp_enqueue_script( 'scna_orderadmin_add_product_js', get_stylesheet_directory_uri() ."/js/scna_orderadmin_add_product.js", array('jquery'), NULL, true );
// send the admin ajax url to the script
wp_localize_script( 'scna_orderadmin_add_product_js', 'mb_script_var', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
/**
* hook to add the javascript file
*/
add_action( 'admin_enqueue_scripts', 'scna_orderadmin_add_product_js_file' );
/**
* Ajax callback
*/
function scna_orderadmin_add_product()
{
/////////////////////////////////////////////////////////////////////////////
/// Attention, to keep the example simple we will not check any ajax data. //
/////////////////////////////////////////////////////////////////////////////
//
// the data from the ajax call
$order_id = intval($_POST['order_id']);
$product_id = intval($_POST['product_id']);
//getting order Object
$order = wc_get_order($order_id);
// gettting the product
$product = wc_get_product($product_id);
$back_data = array('error' => 0, 'msg' => '');
if($order !== false AND $product !== false)
{
// Add the product to the order
$order->add_product($product, 1);
// Save the order data
$order->calculate_totals();
//$order->save();
$back_data['msg'] = 'The item was added';
}
else
{
$back_data['error'] = 1;
$back_data['msg'] = 'No item was added';
}
wp_send_json( $msg );
}
/**
* hook to add the ajax callback
*/
add_action( 'wp_ajax_scna_orderadmin_add_product', 'scna_orderadmin_add_product' );