|
| 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 | +} |
0 commit comments