I have added custom field named 'pin_type' to all products.
The field has string value "single" or "multi" etc.. Im using 'woocommerce_order_status_completed' hook in function.php to do some stuff.
I just cant get the custom 'pin_type' value to show anything. I used phpMyadmin to search for pin_item in wp database and every products pin_item seems to be there in wp_postmeta table under 'meta_key' column and the values are under 'meta_value' column.
How do I get the value based on the product in the order?
add_action( 'woocommerce_order_status_completed', 'pincodeactions', 10, 1);
function pincodeactions( $order_id ) {
$order = wc_get_order( $order_id );
$order_id = $order->get_id();
$price = $order->get_total();
$billing_email = $order->get_billing_email();
$paid_date = date('Y-m-d h:m:s', strtotime($order->get_date_paid()));
//$pin_type = $order->get_meta('pin_type'); //not working: Returns empty string
$pin_type = get_post_meta( $this->order->get_id(), 'pin_type', true ); //ERROR: Using $this when not in object context
echo pin_type
I found the answer: Get a custom field value saved in Order items meta in a hooked function
My working code:
add_action( 'woocommerce_order_status_completed', 'pincodeactions', 10, 1);
function pincodeactions( $order_id ) {
$order = wc_get_order( $order_id );
$order_id = $order->get_id();
$price = $order->get_total();
$billing_email = $order->get_billing_email();
$paid_date = date('Y-m-d h:m:s', strtotime($order->get_date_paid()));
//Get pin type
$order = wc_get_order($order_id);
$items = $order->get_items();
foreach ($items as $item) {
$product_id = $item['product_id'];
$pin_type = get_post_meta($product_id, 'pin_type', true);
if (empty($pin_type)){
$pin_type = 'test';
}
}