Add a Track shipping button to WooCommerce My acccount Orders
Add a Track shipping button to WooCommerce My acccount Orders
I am trying to add Track shipping button to WooCommerce My account orders that tracks order from the order ID, using the following code in funtions.php file of my child theme.
add_filter( 'woocommerce_my_account_my_orders_actions', 'my_code_add_myaccount_order_track_button', 10, 2 );
function my_code_add_myaccount_order_track_button( $actions, $order ) {
// Retrieve tracking link based on order ID (replace this with your logic to get the tracking link)
$order_id = $order->get_id();
$tracking_link = get_post_meta( $order_id, '_tracking_link', true ); // Assuming you store the tracking link in a custom field
if ( ! empty( $tracking_link ) ) {
$actions['track'] = array(
'url' => $tracking_link,
'name' => __( 'Track', 'woocommerce' ),
);
}
return $actions;
}
But it doesn't work at all. What I am doing wrong?
As WooCommerce is migrating progressively to custom database tables, you should use CRUD methods, but not anymore WordPress post meta functions, especially if High-Performance Order Storage is enabled.
Assuming that customer orders have a tracking URL registered as custom order metadata, try the following code replacement:
add_filter( 'woocommerce_my_account_my_orders_actions', 'my_account_my_orders_tracking_button', 10, 2 );
function my_account_my_orders_tracking_button( $actions, $order ) {
// Get tracking link custom metadata
$tracking_link = $order->get_meta('_tracking_link');
if ( ! empty( $tracking_link ) ) {
$actions['track'] = array(
'url' => $tracking_link,
'name' => __( 'Track', 'woocommerce' ),
);
}
return $actions;
}
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
January 10, 2024
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand
January 10, 2024