假设我有一些函数(具有不同数量的参数),如下所示:
def add(n1=None, n2=None):
return n1+n2
我需要通过另一个来呼叫他们,例如:call_another(function_name, arguments)
call_another(add, n1=1, n2=1)
我对** kwargs有想法:
def call_another(f, **kwargs):
# How to call f() sending the arguments?
return f(kwargs) # Obviously, don't work
我看不到将kwargs中的命名参数发送到目标函数的方法。
答案 0 :(得分:2)
您应该使用add_action( 'woocommerce_checkout_create_order_line_item', 'lottery_ticket_items', 20, 4 );
function lottery_ticket_items( $item, $cart_item_key, $values, $order )
{
$numberallocated = array();
$_p = $item->get_product();
$product_id = $_p->get_id();
$drawnumbers = get_field('draw_no', $product_id);
$qty = get_field('maximum_entries', $product_id);
$array = range(1, $qty);
$result = array_diff($array, $drawnumbers);
$key = 'meta_key_value';
foreach( $order->get_items() as $item_id => $item_product ) {
// Get the product ID
$number = array_rand($result, 1);
$numberallocated[] = $number;
$product_id = $item_product->get_product_id();
$item->add_meta_data( $key , $number , true );
}
update_field('draw_no', $numberallocated, $product_id);
}
add_action( 'woocommerce_before_single_product_summary', 'lottery_display_tickets', 5 );
function lottery_display_tickets() {
print_r(get_field('draw_no'));
}
而不是**kwargs
。人工示例:
kwargs