Add text in woocommerce_thankyou if order status is complete - wordpress

I need to add a custom text that is displayed on the thank you page, if the order has been processed and has a completed status.
I am using the following code in the "woocommerce_order_details_before_order_table" hook and it works perfectly, but when I use it in the "woocommerce_thankyou" hook it generates errors.
add_action( 'woocommerce_thankyou', 'complete_custom_text' );
function complete_custom_text( $order ) {
$status = $order->get_status();
if(($status === 'completed')){
echo '<p><b>Text (Custom) 5:</b> Perfect.</p>';
}
}
If someone could help me I would appreciate it.

You are receiving order_id in that hook, use $order = wc_get_order( $order_id ); to get the order before reading the status.

Related

Change Verbiage on WooCommerce Subscriptions Customer Facing

I'm looking to change the verbiage for the word "Suspend" to the word "Pause" on the customer-facing side of WooCommerce Subscriptions. I have the filter that pulls up the action buttons but I'm unsure of how to pull the specific action of "Suspend" and change it to the word "Pause".
Here is the filter.
apply_filters( 'wcs_view_subscription_actions', $actions, $subscription );
Any help would be much appreciated. If you need more information I'm happy to provide what I can.
You can use the wcs_view_subscription_actions filter hook. this way. check below code. code will go in your active theme functions.php file.
add_filter( 'wcs_view_subscription_actions', 'change_action_buttons_label', 10, 2 );
function change_action_buttons_label( $actions, $subscription ){
if( isset( $actions['suspend'] ) ){
$actions['suspend']['name'] = __( 'Pause', 'woocommerce-subscriptions' );
}
return $actions;
}

Automatically Update Order Status to Complete When Shipment Tracking Number Input - WooCommerce

I'm trying to figure out the best way to automatically update the order status in WooCommerce when the tracking information is inputted via the official WooCommerce Shipment Tracking plugin. I found the doc from Woo on how to automatically complete orders, but I only want this to execute when the tracking number is added. Any help would be greatly appreciated!
Below is the code from Woo:
/**
* Auto Complete all WooCommerce orders.
*/
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$order->update_status( 'completed' );
}
And here are the meta references from the plugin:
The Shipping Tracking plugin stores the tracking information in the order meta with the meta key _wc_shipment_tracking_items. It’s an array with the following structure:
tracking_provider — String of predefined provider
custom_tracking_provider — String of custom provider
custom_tracking_link — String of custom tracking URL tracking_number
String of tracking number date_shipped — Timestamp of shipment date
As you said you are using the "WooCommerce Shipment Tracking plugin." but in that plugin, I didn't find any filters or hooks that will help to update status when the tracking number is added. but I found that they use update_post_meta() to update the tracking code so you can use the update_postmeta action hook to update order status.
Try the below code. code will go in your active theme functions.php file.
function update_order_status_when_shipment_tracking_input( $meta_id, $object_id, $meta_key, $meta_value ){
if( $meta_key == '_wc_shipment_tracking_items' ){
error_log('update_order_status_when_shipment_tracking_input');
$order = wc_get_order( $object_id );
if( $order ){
$order->update_status( 'completed' );
}
}
}
add_action( 'update_postmeta', 'update_order_status_when_shipment_tracking_input', 10, 4 );

what would be the hook called for bulk order status (woocommerce)?

i want to call my custom function on Bulk order status changed, to add some additional functionality.
for that i tried following hooks but nothing work..
add_action( 'admin_action_woocommerce_order_status', 'bulk_order_my_function' );
add_action( 'admin_action_woocommerce_order_status_processing', 'bulk_order_my_function' );
add_action( 'admin_action_wc_processing', 'bulk_order_my_function' );
add_action( 'admin_action_woocommerce_processing', 'bulk_order_my_function' );
i am more interested in general order status hook for bulk order update instead of creating separate function for each status..
i.e: so in my one function bases on IF/ELSE condition i can put my logic.
Would be grateful if anyone can help?
So i downloaded the whole woocommerce package and find the string bulk in the woocommerce folder and certainly i found the hook in the below mentioned file:
\woocommerce\includes\admin\list-tables\abstract-class-wc-admin-list-table.php
hook we can use is
add_filter( 'handle_bulk_actions-edit-shop_order', 'bulk_order_stock_update', 10, 3 );
function will be some thing like following
function bulk_order_stock_update($redirect_to, $action, $ids) {
if ( false !== strpos( $action, 'mark_' ) ) {
$new_status = substr( $action, 5 ); // Get the status name from action.
$report_action = 'marked_' . $new_status;
//place your some condition here....
}
}

Using update_meta_data on WooCommerce thank you page

I am trying to limit access to the WooCommerce thank you page so the user can only view it once (At the moment you can paste the URL in another browser and see it still.)
I was thinking of creating/attaching a custom order meta to the order once the thank you page has loaded and then wrapping the whole page template in an if statement that checks if this meta exists. Thus when they paste it into another browser/window the template sees that this metas exists and shows them a different message.
Is this the right way to do it? This is what i have done so far but it doesnt work!
//functions.php
add_action( 'wp_footer', 'hasLoadedPlayPage', 20 );
function hasLoadedPlayPage( $order ){
if( !is_wc_endpoint_url( 'order-received' ) ) return;
$order->update_meta_data('hasLoadedPlayPage', 'Yes');
$order->save();
}
//thankyou.php
$hasAnswered = get_post_meta($order->ID, 'hasLoadedPlayPage', true);
if(! $hasAnswered){
echo "NOT SEEN";
} else {
echo "SEEN";
}
Any guidance anyone could give me would be greatly appreciated!
Thanks
James
Looks good to me except that you need to use add_action('woocommerce_thankyou'... instead of wp_footer. But as woocommerce_thankyou only receives the order id as an argument, you'll need to use $order = wc_get_order($order_id) to get the WC_Order object. Something like:
//functions.php
add_action( 'woocommerce_thankyou', 'hasLoadedPlayPage', 20, 1);
function hasLoadedPlayPage( $order_id ){
$order = wc_get_order($order_id);
$order->update_meta_data('hasLoadedPlayPage', 'Yes');
$order->save();
}
//thankyou.php
$hasAnswered = get_post_meta($order->ID, 'hasLoadedPlayPage', true);
if(! $hasAnswered){
echo "NOT SEEN";
} else {
echo "SEEN";
}

Set every new order with woocommerce on hold

For a specific woocommerce project, I need to set every new order on hold.
It needs to go through that before processing payment.
Do you guys know a hook to do that? I tried many different things, that didn't work.
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
global $woocommerce;
if ( !$order_id )
return;
$order = new WC_Order( $order_id );
$order->update_status( 'on-hold' );
}
This is the standard way of doing it. Not sure if it still works with 2.2, but you didn't specify your WooCommerce version.

Resources