• Resolved charlyox

    (@charlyox)


    Hello,

    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.

    Is this hook really available? Some rumour say that it’s 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 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 );
    }

    I want to add order item meta at the creation of the order.

    Thanks for your help !

    Charles

Viewing 1 replies (of 1 total)
  • Thread Starter charlyox

    (@charlyox)

    SHAME ON ME!

    I had not given the last two arguments of the add_action function…

    So for those who are interested the right code for the add_action is:

    add_action('woocommerce_add_order_item_meta', array($this, 'charlie_ajoute_un_order_item_meta'), 10, 3);

    Sorry for my dumbness.

    Charles

Viewing 1 replies (of 1 total)
  • The topic ‘woocommerce woocommerce_add_order_item_meta issue’ is closed to new replies.