0

I've been using this code to show coupon codes in WooCommerce order overview. With updating the WooCommerce database to high-performance order storage (HPOS) it doesn't work anymore and I don't know how to change.

// Additional custom column
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
function custom_shop_order_column( $columns ) {
    $reordered_columns = array();

    foreach( $columns as $key => $column){
        $reordered_columns[$key] = $column;
        if( $key ==  'order_status' ){
            $reordered_columns['coupons'] = __( 'Coupons', 'woocommerce');
        }
    }
    return $reordered_columns;
}

// Custom column content
add_action( 'manage_shop_order_posts_custom_column', 'custom_shop_order_column_used_coupons' );
function custom_shop_order_column_used_coupons( $column ) {
    global $post, $the_order;

    if ( ! is_a( $the_order, 'WC_Order' ) ) {
        $the_order = wc_get_order( $post->ID );
    }

    if ( 'coupons' === $column ) {
        $coupon_codes = $the_order->get_coupon_codes();
        
        if ( ! empty($coupon_codes) ) {
            echo implode(', ', $coupon_codes);
        }
    }
}

The above code is originally from here: https://stackoverflow.com/a/65520971/26742945above

Can someone please point out what to change in this code? Thank you in advance!

1
  • 1
    WooCommerce is offtopic for this Stack.
    – Chris Cox
    Commented Aug 12 at 11:04

2 Answers 2

0

You are correct that using WC()->session->set('jne_custom_shipping_cost', $shipping_cost); only stores the shipping cost in the session without actually modifying the order data. To include the custom shipping cost into the actual order total when the order is placed, you need to use specific WooCommerce hooks :

//Function 1 : Add the custom shipping cost to the cart total
add_action('woocommerce_cart_calculate_fees', 'add_custom_shipping_cost', 10, 1);
function add_custom_shipping_cost($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    if (did_action('woocommerce_cart_calculate_fees') >= 2) {
        return;
    }

    $shipping_cost = WC()->session->get('jne_custom_shipping_cost');

    if (!empty($shipping_cost)) {
        $cart->add_fee(__('Custom Shipping Cost', 'your-text-domain'), $shipping_cost);
    }
}

//Function 2 : Save the custom shipping cost into the order meta
add_action('woocommerce_checkout_create_order', 'save_custom_shipping_cost_order_meta', 20, 1);
function save_custom_shipping_cost_order_meta($order) {
    $shipping_cost = WC()->session->get('jne_custom_shipping_cost');

    if (!empty($shipping_cost)) {
        $order->set_shipping_total($shipping_cost);
    }
}

//Function 3 : Display the custom shipping cost in the admin order screen
add_action('woocommerce_admin_order_data_after_shipping_address', 'display_custom_shipping_cost_in_admin', 10, 1);
function display_custom_shipping_cost_in_admin($order){
    $shipping_cost = $order->get_shipping_total();
    if (!empty($shipping_cost)) {
        echo '<p><strong>'.__('Custom Shipping Cost', 'your-text-domain').':</strong> ' . wc_price($shipping_cost) . '</p>';
    }
}

WooCommerce provides specific hooks to manage the addition of custom fees and their persistence through the cart and order lifecycle. By using these hooks, such as woocommerce_cart_calculate_fees for adding fees to the cart and woocommerce_checkout_create_order for saving them in the order, you ensure that custom shipping costs are accurately calculated and retained.

Unlike cart items, cart fees are non-persistent, this means third-party plugins which add fees need to be constantly hooked into the cart calculations, adding their fees each time the cart totals are calculated. To do that WooCommerce provides the woocommerce_cart_calculate_fees hook, which third parties can use to add their fees.

Source : https://woocommerce.com/document/subscriptions/develop/recurring-cart-fees/#section-1

0

To adapt your WooCommerce custom column code for coupon codes to the High-Performance Order Storage (HPOS) system, you will need to ensure that you’re correctly accessing the order data using the new storage format. The main change involves how you retrieve the order details, as HPOS uses a slightly different method.

Here’s the modified code to accommodate HPOS:

    // Additional custom column

     add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 
     20 );
     function custom_shop_order_column( $columns ) {
    $reordered_columns = array();

    foreach( $columns as $key => $column ) {
        $reordered_columns[$key] = $column;
        if( $key ==  'order_status' ) {
            $reordered_columns['coupons'] = __( 'Coupons', 'woocommerce' );
        }
    }
    return $reordered_columns;
    }

      // Custom column content

     add_action( 'manage_shop_order_posts_custom_column', 'custom_shop_order_column_used_coupons' );
     function custom_shop_order_column_used_coupons( $column ) {
    global $post;

       // Get the order object using new method for HPOS compatibility
    $the_order = WC_Order_Factory::get_order( $post->ID );

    if ( 'coupons' === $column ) {
        $coupon_codes = $the_order->get_coupon_codes();
        
        if ( ! empty( $coupon_codes ) ) {
            echo implode( ', ', $coupon_codes );
        } else {
            echo __( 'No coupons used', 'woocommerce' );
        }
      }
     }

Changes Explained:

Order Retrieval: The updated code snippet uses WC_Order_Factory::get_order( $post->ID ); instead of wc_get_order( $post->ID );. This is designed to seamlessly work with HPOS, ensuring that you get the order data correctly.

No Coupons Message:

I added a line to output a message when no coupons have been used. This is optional but helps provide clarity to users viewing this column.

Testing:

Ensure to test this modified code in a staging environment before deploying it on a live website. Check for any conflicts with other plugins, especially those that might also modify the WooCommerce admin interface. With these changes, your coupon codes should accurately display in the WooCommerce order overview when using HPOS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.