I have a piece of code for creating video passes after someone buys a product with the ID 1136. For some reason, the order_status_completed is not doing anything. I tried hooking the function to woocommerce_thankyou and woocommerce_payment_complete. I am at a loss. I ran the code without hooking it to an action and it works fine. No bugs in my error log.. nothing.
add_action('woocommerce_order_status_completed','create_video_passes',10,1);
function create_video_passes($orderId){
$order = wc_get_order($orderId);
$orderData = $order->get_data();
foreach( $order->get_items() as $item ):
$item = $item->get_data();
if($item['id'] == 1136){
foreach(range(1,$item['quantity']) as $index) {
$passArray = array(
'post_title' => generateRandomString(),
'post_type' => 'videopass',
'post_status' => 'publish',
'post_author' => 1
);
// Insert the post into the database
$passId = wp_insert_post( $passArray );
update_post_meta( $passId, 'gebruikt','nee');
update_post_meta( $passId, 'email',$orderData['billing']['email']);
}
}
endforeach;
}