Add ShipStation integration for tracking

This commit is contained in:
Narek Zakarian 2023-10-17 16:30:38 +04:00
parent 4bfde190ec
commit e6d43f1110
No known key found for this signature in database
GPG key ID: 07AFD7E7A9C164A7
3 changed files with 124 additions and 3 deletions

View file

@ -65,6 +65,9 @@ return array(
'compat.ywot.is_supported_plugin_version_active' => function (): bool {
return function_exists( 'yith_ywot_init' );
},
'compat.shipstation.is_supported_plugin_version_active' => function (): bool {
return function_exists( 'woocommerce_shipstation_init' );
},
'compat.module.url' => static function ( ContainerInterface $container ): string {
/**

View file

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\OrderTracking;
use WooCommerce\PayPalCommerce\OrderTracking\Integration\GermanizedShipmentIntegration;
use WooCommerce\PayPalCommerce\OrderTracking\Integration\ShipmentTrackingIntegration;
use WooCommerce\PayPalCommerce\OrderTracking\Integration\ShipStationIntegration;
use WooCommerce\PayPalCommerce\OrderTracking\Integration\YithShipmentIntegration;
use WooCommerce\PayPalCommerce\OrderTracking\Shipment\ShipmentFactoryInterface;
use WooCommerce\PayPalCommerce\OrderTracking\Shipment\ShipmentFactory;
@ -96,9 +97,10 @@ return array(
$logger = $container->get( 'woocommerce.logger.woocommerce' );
$endpoint = $container->get( 'order-tracking.endpoint.controller' );
$is_gzd_active = $container->get( 'compat.gzd.is_supported_plugin_version_active' );
$is_wc_shipment_active = $container->get( 'compat.wc_shipment_tracking.is_supported_plugin_version_active' );
$is_yith_ywot_active = $container->get( 'compat.ywot.is_supported_plugin_version_active' );
$is_gzd_active = $container->get( 'compat.gzd.is_supported_plugin_version_active' );
$is_wc_shipment_active = $container->get( 'compat.wc_shipment_tracking.is_supported_plugin_version_active' );
$is_yith_ywot_active = $container->get( 'compat.ywot.is_supported_plugin_version_active' );
$is_ship_station_active = $container->get( 'compat.shipstation.is_supported_plugin_version_active' );
$integrations = array();
@ -114,6 +116,10 @@ return array(
$integrations[] = new YithShipmentIntegration( $shipment_factory, $logger, $endpoint );
}
if ( $is_ship_station_active ) {
$integrations[] = new ShipStationIntegration( $shipment_factory, $logger, $endpoint );
}
return $integrations;
},
);

View file

@ -0,0 +1,112 @@
<?php
/**
* The Shipment integration for ShipStation plugin.
*
* @package WooCommerce\PayPalCommerce\OrderTracking\Shipment
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\OrderTracking\Integration;
use Exception;
use Psr\Log\LoggerInterface;
use WC_Order;
use WooCommerce\PayPalCommerce\Compat\Integration;
use WooCommerce\PayPalCommerce\OrderTracking\Endpoint\OrderTrackingEndpoint;
use WooCommerce\PayPalCommerce\OrderTracking\Shipment\ShipmentFactoryInterface;
/**
* Class ShipStationIntegration.
*/
class ShipStationIntegration implements Integration {
/**
* The shipment factory.
*
* @var ShipmentFactoryInterface
*/
protected $shipment_factory;
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/**
* The order tracking endpoint.
*
* @var OrderTrackingEndpoint
*/
protected $endpoint;
/**
* The ShipStationIntegration constructor.
*
* @param ShipmentFactoryInterface $shipment_factory The shipment factory.
* @param LoggerInterface $logger The logger.
* @param OrderTrackingEndpoint $endpoint The order tracking endpoint.
*/
public function __construct(
ShipmentFactoryInterface $shipment_factory,
LoggerInterface $logger,
OrderTrackingEndpoint $endpoint
) {
$this->shipment_factory = $shipment_factory;
$this->logger = $logger;
$this->endpoint = $endpoint;
}
/**
* {@inheritDoc}
*/
public function integrate(): void {
add_action(
'woocommerce_shipstation_shipnotify',
function( $wc_order, array $data ) {
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_ship_station_tracking', true ) ) {
return;
}
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return;
}
$order_id = $wc_order->get_id();
$transaction_id = $wc_order->get_transaction_id();
$tracking_number = $data['tracking_number'] ?? '';
$carrier = $data['carrier'] ?? '';
if ( ! $tracking_number || ! is_string( $tracking_number ) || ! $carrier || ! is_string( $carrier ) || ! $transaction_id ) {
return;
}
try {
$ppcp_shipment = $this->shipment_factory->create_shipment(
$order_id,
$transaction_id,
$tracking_number,
'SHIPPED',
'OTHER',
$carrier,
array()
);
$tracking_information = $this->endpoint->get_tracking_information( $order_id, $tracking_number );
$tracking_information
? $this->endpoint->update_tracking_information( $ppcp_shipment, $order_id )
: $this->endpoint->add_tracking_information( $ppcp_shipment, $order_id );
} catch ( Exception $exception ) {
$this->logger->error( "Couldn't sync tracking information: " . $exception->getMessage() );
}
},
500,
1
);
}
}