Skip to content

Commit 4144cba

Browse files
authored
Merge pull request #147 from BeAPI/issue/77334
Issue/77334
2 parents 76d0746 + 83931f0 commit 4144cba

File tree

3 files changed

+228
-9
lines changed

3 files changed

+228
-9
lines changed

src/Addons/Addons.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77

88
use ShoppingFeed\ShoppingFeedWC\Addons\Inventory\Inventory;
99
use ShoppingFeed\ShoppingFeedWC\Addons\Marketplaces\Marketplaces;
10-
use ShoppingFeed\ShoppingFeedWC\Addons\Plugins\ASTPlugin\ASTPlugin;
1110
use ShoppingFeed\ShoppingFeedWC\Addons\Plugins\ChainedProductsPlugin\ChainedProducts;
12-
use ShoppingFeed\ShoppingFeedWC\Addons\Plugins\PhWoocommerceShipmentTrackingProPlugin\PhWoocommerceShipmentTrackingProPlugin;
11+
use ShoppingFeed\ShoppingFeedWC\Addons\Plugins\MondialRelayWordpressPlugin\MondialRelayWordpress;
1312
use ShoppingFeed\ShoppingFeedWC\Addons\Shipping\Shipping;
1413

1514
class Addons {
@@ -36,10 +35,16 @@ class Addons {
3635
*/
3736
private $chained_products_plugin;
3837

38+
/**
39+
* @var MondialRelayWordpress
40+
*/
41+
private $mondial_relay_plugin;
42+
3943
public function __construct() {
40-
$this->shipping = new Shipping();
41-
$this->inventory = new Inventory();
42-
$this->marketplaces = new Marketplaces();
43-
$this->chained_products_plugin = new ChainedProducts();
44+
$this->shipping = new Shipping();
45+
$this->inventory = new Inventory();
46+
$this->marketplaces = new Marketplaces();
47+
$this->chained_products_plugin = new ChainedProducts();
48+
$this->mondial_relay_plugin = new MondialRelayWordpress();
4449
}
4550
}
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
<?php
2+
3+
namespace ShoppingFeed\ShoppingFeedWC\Addons\Plugins\MondialRelayWordpressPlugin;
4+
5+
use ShoppingFeed\ShoppingFeedWC\Dependencies\ShoppingFeed\Sdk\Api\Order\OrderResource;
6+
use ShoppingFeed\ShoppingFeedWC\Orders\Order\Metas;
7+
use ShoppingFeed\ShoppingFeedWC\Orders\Order\Products;
8+
use ShoppingFeed\ShoppingFeedWC\ShoppingFeedHelper;
9+
10+
// Exit on direct access
11+
defined( 'ABSPATH' ) || exit;
12+
13+
/**
14+
* Class MondialRelayWordpress to manage the plugin mondialrelay-wordpress
15+
* @link https://mondialrelay-wp.com/en/home/
16+
* @package ShoppingFeed\ShoppingFeedWC\Addons\Plugins\MondialRelayWordpressPlugin
17+
*/
18+
class MondialRelayWordpress {
19+
20+
private const MONDIAL_RELAY_SHIPPING_METHOD = 'mondialrelay_official_shipping';
21+
22+
public function __construct() {
23+
if ( ! class_exists( \class_MRWP_main::class ) ) {
24+
return;
25+
}
26+
27+
add_action( 'sf_add_metas', [ $this, 'add_meta' ] );
28+
}
29+
30+
/**
31+
* Add metadata used by MondialRelayWordPress plugin to generate labels.
32+
*
33+
* @param Metas $metas
34+
*
35+
* @return void
36+
*/
37+
public function add_meta( $metas ): void {
38+
$carrier = $metas->sf_order->getShipment()['carrier'] ?? false;
39+
if ( ! $carrier ) {
40+
return;
41+
}
42+
43+
// Find matching shipping method name for the carrier
44+
$shipping_method_for_carrier = ShoppingFeedHelper::get_wc_shipping_from_sf_carrier( $carrier );
45+
if (
46+
! is_array( $shipping_method_for_carrier )
47+
|| empty( $shipping_method_for_carrier ) ||
48+
! isset( $shipping_method_for_carrier['method_rate_id'] )
49+
) {
50+
ShoppingFeedHelper::get_logger()->warning(
51+
sprintf(
52+
'[Mondial Relay] Order %s carrier is not associated with a Woocommerce shipping method.',
53+
$metas->sf_order->getReference()
54+
),
55+
array(
56+
'source' => 'shopping-feed',
57+
'carrier' => esc_html( $carrier ),
58+
)
59+
);
60+
61+
return;
62+
}
63+
64+
// Check if shipping method match MondialRelay
65+
if ( self::MONDIAL_RELAY_SHIPPING_METHOD !== $shipping_method_for_carrier['method_rate_id'] ) {
66+
ShoppingFeedHelper::get_logger()->warning(
67+
sprintf(
68+
'[Mondial Relay] Order %s carrier is not associated with Mondial Relay shipping method.',
69+
$metas->sf_order->getReference()
70+
),
71+
array(
72+
'source' => 'shopping-feed',
73+
'carrier' => esc_html( $carrier ),
74+
'shipping_method' => esc_html( $shipping_method_for_carrier['method_rate_id'] ),
75+
)
76+
);
77+
78+
return;
79+
}
80+
81+
ShoppingFeedHelper::get_logger()->info(
82+
sprintf(
83+
'[Mondial Relay] order %s carrier is associated with Mondial Relay shipping method.',
84+
$metas->sf_order->getReference()
85+
),
86+
array(
87+
'source' => 'shopping-feed',
88+
)
89+
);
90+
91+
// Get shipping method instance (use later to check the shipping code)
92+
$shipping_methods = \WC()->shipping()->get_shipping_methods();
93+
$method_name = (string) $shipping_method_for_carrier['method_rate_id'];
94+
$instance_id = (int) $shipping_method_for_carrier['method_id'];
95+
96+
if ( ! isset( $shipping_methods[ $method_name ] ) ) {
97+
ShoppingFeedHelper::get_logger()->error(
98+
'[Mondial Relay] Failed to retrieve shipping method instance.',
99+
array(
100+
'source' => 'shopping-feed',
101+
'shipping_method' => sprintf( '%s:%s', $method_name, $instance_id ),
102+
)
103+
);
104+
105+
return;
106+
}
107+
108+
$classname = get_class( $shipping_methods[ $method_name ] );
109+
/* @var \WC_Shipping_Method $instance */
110+
$instance = new $classname( $instance_id );
111+
112+
// Get the relay id for the order.
113+
$relay_id = $metas->sf_order->getShippingAddress()['relayId'] ?? false;
114+
if ( ! $relay_id ) {
115+
ShoppingFeedHelper::get_logger()->warning(
116+
sprintf( '[Mondial Relay] no relay id found for the order %s', $metas->sf_order->getReference() ),
117+
array(
118+
'source' => 'shopping-feed',
119+
)
120+
);
121+
122+
return;
123+
}
124+
125+
ShoppingFeedHelper::get_logger()->info(
126+
sprintf(
127+
'[Mondial Relay] found relay id %s for order %s',
128+
$relay_id,
129+
$metas->sf_order->getReference()
130+
),
131+
array(
132+
'source' => 'shopping-feed',
133+
)
134+
);
135+
136+
// Prefix relay id with the country code if available
137+
$country = $metas->sf_order->getShippingAddress()['country'] ?? false;
138+
if ( $country ) {
139+
$relay_id = sprintf( '%s-%s', strtoupper( $country ), $relay_id );
140+
}
141+
142+
$metas->add_meta( 'Mondial Relay Parcel Shop ID', $relay_id, true );
143+
144+
$formatted_address = $this->format_address( $metas->sf_order );
145+
$metas->add_meta( 'Mondial Relay Parcel Shop Address', $formatted_address, true );
146+
147+
/**
148+
* Code MED and APM are encoded as 24R
149+
*
150+
* @see \class_MRWP_main::save_mondial_relay_shipping_meta
151+
*/
152+
$shipping_code = $instance->get_instance_option( 'method', '' );
153+
if ( in_array( $shipping_code, [ 'MED', 'APM' ], true ) ) {
154+
$shipping_code = '24R';
155+
}
156+
157+
$metas->add_meta( 'Mondial Relay Shipping Code', $shipping_code, true );
158+
159+
$weight = $this->calculate_products_weight( $metas->sf_order );
160+
$metas->add_meta( 'Mondial Relay Parcel Weight', $weight, true );
161+
162+
ShoppingFeedHelper::get_logger()->info(
163+
'[Mondial Relay] added metadata for MondialRelayWordpress',
164+
array(
165+
'source' => 'shopping-feed',
166+
)
167+
);
168+
}
169+
170+
/**
171+
* Format address to match MondialRelayWP
172+
*
173+
* The plugin use `-MRWP-` to mark line return.
174+
*
175+
* @return string
176+
*/
177+
private function format_address( OrderResource $sf_order ): string {
178+
$address_parts = [];
179+
$address_parts[] = $sf_order->getShippingAddress()['company'] ?? '';
180+
$address_parts[] = $sf_order->getShippingAddress()['street'] ?? '';
181+
$address_parts[] = $sf_order->getShippingAddress()['street2'] ?? '';
182+
$address_parts[] = $sf_order->getShippingAddress()['postalCode'] ?? '';
183+
$address_parts[] = $sf_order->getShippingAddress()['city'] ?? '';
184+
$address_parts[] = $sf_order->getShippingAddress()['country'] ?? '';
185+
186+
return implode( '-MRWP-', $address_parts );
187+
}
188+
189+
/**
190+
* Return the weight in grams for the order.
191+
*
192+
* @param OrderResource $sf_order
193+
*
194+
* @return int
195+
*/
196+
private function calculate_products_weight( OrderResource $sf_order ): int {
197+
$weight = 0;
198+
$products = new Products( $sf_order );
199+
foreach ( $products->get_products() as $product ) {
200+
$product_id = 0 !== (int) $product['args']['variation_id'] ? (int) $product['args']['variation_id'] : $product['args']['product_id'];
201+
$wc_product = wc_get_product( $product_id );
202+
if ( ! $wc_product ) {
203+
continue;
204+
}
205+
206+
$weight += ( (float) $wc_product->get_weight() ) * 1000; // convert weight to grams
207+
}
208+
209+
return (int) $weight;
210+
}
211+
}

src/ShoppingFeedHelper.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,11 +479,12 @@ public static function get_zones_with_shipping_methods() {
479479
/**
480480
* Get WC Shipping from SF carrier
481481
*
482-
* @param $name
482+
* @param string $sf_carrier
483483
*
484484
* @return array
485485
*/
486486
public static function get_wc_shipping_from_sf_carrier( $sf_carrier ) {
487+
$sf_carrier = trim( $sf_carrier );
487488
$sf_carrier_id = self::get_sf_carrier_id( $sf_carrier );
488489
if ( empty( $sf_carrier_id ) && ! empty( $sf_carrier ) ) {
489490
$sf_carrier_id = self::add_sf_carrier( $sf_carrier );
@@ -506,7 +507,7 @@ public static function get_wc_shipping_from_sf_carrier( $sf_carrier ) {
506507
/**
507508
* Get SF carrier id
508509
*
509-
* @param $name
510+
* @param string $name
510511
*
511512
* @return int
512513
*/
@@ -541,7 +542,9 @@ public static function get_sf_carriers() {
541542
/**
542543
* Add SF carrier
543544
*
544-
* @param $sf_carrier
545+
* @param string $sf_carrier
546+
*
547+
* @return int
545548
*/
546549
public static function add_sf_carrier( $sf_carrier ) {
547550
$sf_carriers = self::get_sf_carriers();

0 commit comments

Comments
 (0)