GLS – Get Order – URL functions

0.0/5
<?php
//Requires enabled WooCommerce Abandoned Cart Pro because of Twillio

function custom_meta_box_markup()
{
global $post;

//if ($order->get_meta('_track_url') != null) {
    //return false;
//}

$request = wp_remote_get( 'http://194.228.216.10:5491/mailstage/orderStatus/get?login=RECLAR&pswd=Zzfe4qusgzdp&order_id=' . $post->ID );

if( is_wp_error( $request ) ) {
    return false; // Bail early
}

$body = wp_remote_retrieve_body($request);
$data = json_decode($body);

$track_url = $data[0]->track_url;

if ($track_url != null ) {
    update_post_meta($post->ID, '_track_url', $track_url);
	echo '<a href="' . $track_url . '">' . 'Sledovat zásilku' . '</a>';
} else {
	echo "Zásilka ještě nemá Tracking URL";
}

}

function add_custom_meta_box()
{
    add_meta_box("demo-meta-box", "Order Delivery", "custom_meta_box_markup", "shop_order", "side", "high", null);
}

add_action("add_meta_boxes", "add_custom_meta_box");

use Twilio\Rest\Client;

add_action("woocommerce_order_status_changed", "wpd_order_tracking_email");

function wpd_order_tracking_email($order_id, $checkout=null) {
   global $woocommerce;
   $order = new WC_Order( $order_id );
   if($order->status === 'handedcourier' ) {
	  if ($order->get_meta('_track_url') == null) {
	
		$request = wp_remote_get( 'http://194.228.216.10:5491/mailstage/orderStatus/get?login=RECLAR&pswd=Zzfe4qusgzdp&order_id=' . $order->get_id() );
		
		if( is_wp_error( $request ) ) {
    		return false; 
		}

		$body = wp_remote_retrieve_body($request);
		$data = json_decode($body);

		$track_url = $data[0]->track_url;
		if ($track_url != null ) {
			update_post_meta($order->get_id(), '_track_url', $track_url);
		}
	  } else {
		  $track_url = $order->get_meta('_track_url');
	  }
      // Create a mailer
      $mailer = $woocommerce->mailer();

      $message_body = __( '<a href="' . $track_url . '">' . __('Tracking URL', 'code-snippets') . '</a>' );

      $message = $mailer->wrap_message(
        // Message head and message body.
      	sprintf( __( 'Order %s tracking URL', 'code-snippets' ), $order->get_order_number() ), $message_body );


        // Cliente email, email subject and message.
     	$mailer->send( $order->billing_email, sprintf( __( 'Order %s tracking url', 'code-snippets' ), $order->get_order_number() ), $message );
	   
		if( ! class_exists( 'SplClassLoader' ) ) {
    		require_once( WP_PLUGIN_DIR . '/woocommerce-abandon-cart-pro/includes/libraries/twilio-php/Twilio/autoload.php' ); // Loads the library
		
		}
		
	    //$to_phone = "+420776095445";
	    $to_phone = $order->get_billing_phone();
		
		$sid = "AC18aac4fa96e38bc318a60062a2fcc454";
		$token = "8b8cf904eb48ca90449eef68431c9e0f";
	
		$client = new Client($sid, $token);
		$from_phone = "+12565989551";
		
	
		$msg_body = "Reclar - " . sprintf( __( 'Your order %s was sent', 'code-snippets' ), $order->get_order_number() ) . __('Tracking URL', 'code-snippets') . " " . $track_url . " " . $order_id;
		$message = $client->messages->create(
                                $to_phone,
                                array(
                                    'from' => $from_phone,
                                    'body' => $msg_body,
                            )
              );

	}

}

function wpd_add_my_account_order_tracking( $actions, $order ) {
	
	if ( $order->get_meta('_track_url') != null) {
		$track_url = $order->get_meta('_track_url');
		$message   = __( "Track URL", 'code-snippets');
	} else {
		$track_url = '';
		$message   = __( "Tracking URL Not Available", 'code-snippets');
	}
    
	
	$actions['track'] = array(
        // adjust URL as needed
        'url'  => $track_url,
        'name' =>  $message,
    );

    return $actions;
}
add_filter( 'woocommerce_my_account_my_orders_actions', 'wpd_add_my_account_order_tracking', 10, 2 );

Comments and reviews

{{ reviewsTotal }} Review
{{ reviewsTotal }} Reviews
{{ options.labels.newReviewButton }}
{{ userData.canReview.message }}

Description

This snippet was fetched automatically from WPDistro.cz projects.

Author

Tags

This snippet is untagged