-2

When i add this code to the thankyou.php of woocommerce everything is working fine (without the hook of course). When i add it to my function.php on my child theme its not working at all.

function lwb_test() {
foreach ( $order->get_items() as $item_id => $item ) {
    $product_id = $item->get_product_id();
    $order_id = $order->get_id();
    $new_product = new WC_Product( $product_id );  // create an object of WC_Product class

    $product_attribut = $new_product->get_attribute( 'pa_lieferfrequenz' );  // call get_attribute method
    //$product_attribut = '1week';
    $date = date('d-m-Y', strtotime("+ $product_attribut "));
    add_post_meta( $order_id, 'lwb_pickup_time_email_notification', $date );
}
}
add_action('woocommerce_thankyou', 'lwb_test');
LovinQuaQua
  • 111
  • 2
  • 12

1 Answers1

2

You can inside add_action use your function, like so.

add_action( 'woocommerce_thankyou', function( $order_id ){
    $order = new WC_Order( $order_id );
});

You can use it with additional code but I thought it would be easier for you with only add_action

add_action('woocommerce_thankyou', 'example', 10, 1);

function example($order_id) {
    $order = new WC_Order( $order_id );
}
Pipo
  • 488
  • 2
  • 10
  • can you tell me the difference, why this is working when the function is inside the add_action? – LovinQuaQua Jul 13 '20 at 21:45
  • Alterred my answer, for another example with function outside. It can work both ways, you choose what you want. In your code you're creating a callback function, but you were missing $order_id in that function, that's why It didn't work. – Pipo Jul 13 '20 at 21:52