Search code examples
phpwoocommercemethodscrudorders

Display WooCommerce order metadata compatible with HPOS


I wish happy new year to all of you :)

I was using regular WooCommerce and send customer email when I change cargo status by REST API. Below code was working great. But after I enabled HPOS, data doesn't come with same code. How should I change it? (I already tried few alternatives) get_post_meta part is the issue. I sent mail but data is missing.

// Targets custom order status "shipping-progress"
// Uses 'woocommerce_order_status_' hook

add_action( 'woocommerce_order_status_shipping-progress', 'bbloomer_status_custom_notification', 20, 2 );

function bbloomer_status_custom_notification( $order_id, $order ) {

    $heading = 'Your order is shipped';
    $subject = 'Your order is shipped';

    // Get WooCommerce email objects
    $mailer = WC()->mailer()->get_emails();

    $mailer['WC_Email_Customer_Completed_Order']->heading = $heading;
    $mailer['WC_Email_Customer_Completed_Order']->settings['heading'] = $heading;
    $mailer['WC_Email_Customer_Completed_Order']->subject = $subject;
    $mailer['WC_Email_Customer_Completed_Order']->settings['subject'] = $subject;

    // Send the email with custom heading & subject
    $mailer['WC_Email_Customer_Completed_Order']->trigger( $order_id );
}

add_action( 'woocommerce_email_before_order_table', 'bbloomer_add_content_specific_email', 20, 4 );
function bbloomer_add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
    $orderstatus = $order->get_status();
    if ( $email->id == 'customer_completed_order' ) {
        if ( $orderstatus == 'shipping-progress' ) {
            $cargo_company = get_post_meta( $order->get_id(), 'shipping_company', true );
            $cargo_shipping_no = get_post_meta( $order->get_id(), 'shipping_number', true );
            echo '<p>Cargo Company: <strong>'.cargo_company.'</strong></p>';
            echo '<p>Tracking number: <strong>'.cargo_shipping_no.'</strong></p>';

        }
    }
}

Solution

  • According to provided code, to get order metadata, when woocommerce High-Performance Order Storage (HPOS) is enabled, the following code should be used.

    $order->get_meta( 'shipping_company', true );
    $order->get_meta( 'shipping_number', true );