我知道有点晚了,但也许我的anwser对其他人有帮助。我在版本4.9.7中使用Wordpress,在版本3.4.3中使用WooCommerce
要扩展按钮,可以使用woocommerce操作“woocommerce_order_item_add_action_buttons“。您可以在文件中找到该操作的声明。”"woocommerce/includes/admin/meta-boxes/views/html-order-items.php"
以下代码使用此操作并向现有按钮添加新按钮(“添加特殊产品”)。
function my_function_to_add_the_button($order)
{
// Add the "Special Product" button
// 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="my_special_button" type="button" class="button add-special-item" data-order_id="\'. esc_attr($order->get_id()) .\'" data-product_id="798" >Add Special Product</button>\';
}
/**
* hook to add the button
*/
add_action(\'woocommerce_order_item_add_action_buttons\', \'my_function_to_add_the_button\');
然后可以将相同的JavaScript代码绑定到按钮,并将order\\u id和product\\u id发送到服务器。
下面的代码告诉WordPress在页脚中加载一个新的JavaScript文件。
/**
* Add javascript
*/
function my_function_to_add_the_js_file()
{
wp_enqueue_script( \'my_button_script\', plugin_dir_url(__FILE__) ."/js/my-button-script.js", array(\'jquery\'), NULL, true );
// send the admin ajax url to the script
wp_localize_script( \'my_button_script\', \'mb_script_var\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );
}
/**
* hook to add the javascript file
*/
add_action( \'admin_enqueue_scripts\', \'my_function_to_add_the_js_file\' );
下一段代码显示该文件的内容。
(function( $ ) {
\'use strict\';
$(\'.inside\').on(\'click\',\'#my_special_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: \'add_my_product_to_order\',
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 );
JavaScript代码将click函数绑定到新按钮。如果有人单击该按钮,JavaScript代码将从标记中获取product\\u id和order\\u id,并通过ajax将它们发送到服务器。
在服务器上,产品将添加到订单中。
下面的代码显示了JavaScript将调用的“ajax”函数。
/**
* Ajax callback
*/
function my_function_to_add_the_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->save();
$back_data[\'msg\'] = \'The procuct was added\';
}
else
{
$back_data[\'error\'] = 1;
$back_data[\'msg\'] = \'No product was added\';
}
wp_send_json( $msg );
}
/**
* hook to add the ajax callback
*/
add_action( \'wp_ajax_add_my_product_to_order\', \'my_function_to_add_the_product\' );