Reputation: 44
I am using WC v. 2.3.9.
woocommerce_add_order_item_meta gets fired but my callback function only gets one parameter, three expected.
I want to add order item meta at the creation of the order.
Is this hook really available? Some rumour say that it's deprecated.
Edit : after a few research it's the function woocommerce_add_order_item_meta() that is deprecated.
Anyway I can find it in woocommerce/includes/class-wc-checkout.php. And the "do_action()" on this hook seems straightforward:
// Store the line items to the new/resumed order
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$item_id = $order->add_product(
$values['data'],
$values['quantity'],
array(
'variation' => $values['variation'],
'totals' => array(
'subtotal' => $values['line_subtotal'],
'subtotal_tax' => $values['line_subtotal_tax'],
'total' => $values['line_total'],
'tax' => $values['line_tax'],
'tax_data' => $values['line_tax_data'] // Since 2.2
)
)
);
if ( ! $item_id ) {
throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce' ), 402 ) );
}
// Allow plugins to add order item meta
do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );
}
my custom plugin code:
public function __construct(){
add_action('woocommerce_add_order_item_meta', array($this, 'charlie_ajoute_un_order_item_meta'));
}
public function charlie_ajoute_un_order_item_meta($item_id, $values, $cart_item_key){
if (isset($values['cred_meta']['cred_post_id'])){
$annonce_id = $values['cred_meta']['cred_post_id'];
}
elseif(isset($values['annonce_concernee'])){
$annonce_id = $values['annonce_concernee'];
}
else{
$annonce_id = 'turlututu';
/* as $values as well as $$cart_item_key are not set, my order item meta is always "turlututu".*/
}
wc_add_order_item_meta( $item_id, 'annonce_concernee', $annonce_id, true );
}
Thanks for your help !
Charles
Upvotes: 0
Views: 3071
Reputation: 44
Shame on me!I had not found necessary to add the last two arguments of the add_action() function. So the default number of argument being one, only one was passed to my callback.
so the right code for the add_action() function is:
add_action('woocommerce_add_order_item_meta', array($this, 'charlie_ajoute_un_order_item_meta'), 10, 3);
Hope it may help someone.
Charles
Upvotes: 1