Skip to content

Commit a2db00e

Browse files
committed
Merge branch 'WP-167' into 'main'
feat: add jewel uin to delivery order See merge request ecommerce_modules/cms/wordpress/wordpress!82
2 parents 3d5602c + fec631f commit a2db00e

10 files changed

Lines changed: 419 additions & 111 deletions

File tree

src/Actions/OrderCreateAction.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Cdek\Helpers\Logger;
2525
use Cdek\Helpers\StringHelper;
2626
use Cdek\Helpers\WeightConverter;
27+
use Cdek\MetaKeys;
2728
use Cdek\Model\Order;
2829
use Cdek\Model\Service;
2930
use Cdek\Model\ShippingItem;
@@ -377,7 +378,7 @@ private function buildItemData(
377378
}
378379
}
379380

380-
return [
381+
$orderItem = [
381382
'ware_key' => $product->get_sku() ?: $product->get_id(),
382383
'payment' => $payment,
383384
'name' => $item->get_name(),
@@ -386,6 +387,14 @@ private function buildItemData(
386387
'weight' => $w,
387388
'weight_gross' => $w + 1,
388389
];
390+
391+
$jewelUin = $item->get_meta(MetaKeys::JEWEL_UIN);
392+
393+
if(!empty($jewelUin)){
394+
$orderItem['jewel_uin'] = $jewelUin;
395+
}
396+
397+
return $orderItem;
389398
}
390399

391400
private function convertCurrencyToRub(float $cost, string $currency): float
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace {
6+
7+
defined('ABSPATH') or exit;
8+
}
9+
10+
namespace Cdek\Controllers {
11+
12+
use Cdek\Config;
13+
use Cdek\Helpers\Logger;
14+
use Cdek\MetaKeys;
15+
use JsonException;
16+
17+
class OrderItemController
18+
{
19+
public function __invoke(): void
20+
{
21+
if (!wp_doing_ajax()) {
22+
return;
23+
}
24+
25+
$prefix = Config::DELIVERY_NAME;
26+
27+
add_action("wp_ajax_$prefix-jewel-uin", [__CLASS__, 'jewelUin']);
28+
}
29+
30+
public static function jewelUin(): void
31+
{
32+
check_ajax_referer(Config::DELIVERY_NAME);
33+
34+
if (!current_user_can('edit_posts')) {
35+
wp_die(-2, 403);
36+
}
37+
38+
try {
39+
$body = json_decode(file_get_contents('php://input'), true, 512, JSON_THROW_ON_ERROR);
40+
} catch (JsonException $e) {
41+
wp_send_json_error(
42+
[
43+
'message' => __('Invalid request data.', 'cdekdelivery'),
44+
]
45+
);
46+
}
47+
48+
if (!isset($body['jewel_uin'], $body['item_id'])) {
49+
wp_send_json_error(
50+
[
51+
'message' => __('Invalid request data.', 'cdekdelivery'),
52+
]
53+
);
54+
}
55+
56+
$item_id = (int)$body['item_id'];
57+
$jewel_uin = sanitize_text_field($body['jewel_uin']);
58+
59+
try {
60+
if (wc_update_order_item_meta($item_id, MetaKeys::JEWEL_UIN, $jewel_uin)) {
61+
wp_send_json_success(['message' => __('UIN saved successfully.', 'cdekdelivery')]);
62+
}
63+
64+
Logger::debug(
65+
sprintf(
66+
'Failed to save UIN %s for item %d',
67+
$jewel_uin,
68+
$item_id,
69+
)
70+
);
71+
} catch (\Exception $e) {
72+
Logger::warning(
73+
sprintf(
74+
'Failed to save UIN for item %d: %s',
75+
$item_id,
76+
$e->getMessage()
77+
)
78+
);
79+
}
80+
81+
wp_send_json_error(
82+
[
83+
'message' => __('Failed to save UIN.', 'cdekdelivery'),
84+
'meta' => MetaKeys::JEWEL_UIN,
85+
'item_id' => $item_id,
86+
'jewel_uin' => $jewel_uin,
87+
],
88+
);
89+
}
90+
}
91+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
import {addQueryArgs} from '@wordpress/url';
3+
import {__} from '@wordpress/i18n';
4+
import './styles/main.scss';
5+
import $ from 'jquery';
6+
import apiFetch from '@wordpress/api-fetch';
7+
8+
$(document).ready(() => {
9+
$(`.${window.cdek.prefix}-show_uin`).on('click', function (event) {
10+
event.preventDefault();
11+
const $container = $(this).next('.hidden');
12+
13+
if ($container.length > 0) {
14+
$(this).addClass('hidden');
15+
$container.removeClass('hidden');
16+
}
17+
});
18+
19+
$(`.${window.cdek.prefix}-save_uin`).on('click', function (e) {
20+
e.preventDefault();
21+
e.stopPropagation();
22+
23+
const $container = $(this).parent();
24+
const $input = $container.find(`.${window.cdek.prefix}-jewel-uin`);
25+
26+
if ($input.length === 0) {
27+
console.error('Input not found');
28+
return;
29+
}
30+
31+
$container.find(`.${window.cdek.prefix}-notice`).remove();
32+
33+
const $notice = $('<div></div>').addClass(`${window.cdek.prefix}-notice`);
34+
35+
apiFetch(
36+
{
37+
method: 'POST',
38+
url: addQueryArgs(ajaxurl, {
39+
action: `${window.cdek.prefix}-jewel-uin`,
40+
_wpnonce: window.cdek.nonce,
41+
}),
42+
data: {
43+
item_id: $container.data('id'),
44+
jewel_uin: $input.val(),
45+
},
46+
},
47+
).then(
48+
resp => {
49+
console.debug('[CDEK-ORDER-ITEM] Save UIN response', resp);
50+
51+
if (resp.success) {
52+
$notice.addClass('notice-success');
53+
} else {
54+
$notice.addClass('notice-error');
55+
}
56+
57+
$input.before($notice.text(resp.data.message));
58+
},
59+
).catch(error => {
60+
console.error('Error catch:', error);
61+
62+
$input.before(
63+
$notice
64+
.addClass('notice-error')
65+
.text(__('Error saving UIN', 'cdekdelivery')),
66+
);
67+
}).finally(() => setTimeout(() => $notice.remove(), 5000));
68+
});
69+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.official_cdek-uin-input-container {
2+
position: relative;
3+
4+
.official_cdek-notice {
5+
position: absolute;
6+
box-sizing: border-box;
7+
padding: 10px;
8+
bottom: 0;
9+
right: 10px;
10+
display: flex;
11+
align-items: center;
12+
justify-content: center;
13+
border: none;
14+
color: #ffffff;
15+
}
16+
17+
.notice-success {
18+
background-color: #2ea44f;
19+
}
20+
21+
.notice-error {
22+
background-color: #d9534f;
23+
}
24+
}

src/Loader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
use Cdek\Controllers\CallbackController;
2323
use Cdek\Controllers\IntakeController;
2424
use Cdek\Controllers\OrderController;
25+
use Cdek\Controllers\OrderItemController;
2526
use Cdek\Controllers\SettingsController;
2627
use Cdek\Helpers\CheckoutHelper;
2728
use Cdek\Helpers\DataCleaner;
2829
use Cdek\Traits\CanBeCreated;
2930
use Cdek\UI\Admin;
3031
use Cdek\UI\AdminNotices;
32+
use Cdek\UI\AdminOrderProductFields;
3133
use Cdek\UI\AdminShippingFields;
3234
use Cdek\UI\CdekWidget;
3335
use Cdek\UI\CheckoutMap;
@@ -216,6 +218,7 @@ public function __invoke(string $pluginMainFile): void
216218
add_action('admin_init', new IntakeController);
217219
add_action('admin_init', new OrderController);
218220
add_action('admin_init', new SettingsController);
221+
add_action('admin_init', new OrderItemController);
219222

220223
add_action('wc_ajax_' . Config::DELIVERY_NAME . '_save-office', new SaveOfficeToSessionAction);
221224

@@ -262,6 +265,7 @@ public function __invoke(string $pluginMainFile): void
262265
);
263266

264267
add_action('woocommerce_before_order_itemmeta', new AdminShippingFields, 10, 2);
268+
add_action('woocommerce_after_order_itemmeta', new AdminOrderProductFields, 20, 3);
265269

266270
add_action(Config::ORDER_AUTOMATION_HOOK_NAME, OrderCreateAction::new(), 10, 2);
267271
add_action(Config::TASK_MANAGER_HOOK_NAME, new TaskManager, 20);

src/MetaKeys.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ class MetaKeys
1111
public const LENGTH = '_official_cdek_length';
1212
public const WIDTH = '_official_cdek_width';
1313
public const HEIGHT = '_official_cdek_height';
14-
1514
public const TARIFF_CODE = '_official_cdek_tariff_code';
1615
public const TARIFF_MODE = '_official_cdek_tariff_mode';
17-
1816
public const OFFICE_CODE = '_official_cdek_office_code';
17+
public const JEWEL_UIN = '_official_cdek_jewel_uin';
1918
}

src/ShippingMethod.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Cdek {
1111

1212
use Cdek\Actions\CalculateDeliveryAction;
13-
use Cdek\Actions\FlushTokenCacheAction;
1413
use Cdek\Contracts\ExceptionContract;
1514
use Cdek\Migrators\MigrateCityCodeFromMap;
1615
use Cdek\Traits\SettingsFields;

src/UI/AdminOrderProductFields.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cdek\UI {
6+
7+
use Cdek\Config;
8+
use Cdek\Helpers\Logger;
9+
use Cdek\Helpers\UI;
10+
use Cdek\MetaKeys;
11+
use WC_Order_Item;
12+
use WC_Product;
13+
use WC_Order;
14+
15+
class AdminOrderProductFields
16+
{
17+
public function __invoke(int $itemId, ?WC_Order_Item $item, ?WC_Product $product): void
18+
{
19+
if ($item === null || $product === null) {
20+
return;
21+
}
22+
23+
$order = $item->get_order();
24+
25+
if (!$order instanceof WC_Order) {
26+
return;
27+
}
28+
29+
if (!$this->checkShipping($order->get_shipping_methods())) {
30+
return;
31+
}
32+
33+
try {
34+
$jewel_uin_value = wc_get_order_item_meta($itemId, MetaKeys::JEWEL_UIN);
35+
} catch (\Exception $e) {
36+
Logger::warning(
37+
sprintf(
38+
'Failed to get UIN for item %d: %s',
39+
$itemId,
40+
$e->getMessage()
41+
)
42+
);
43+
$jewel_uin_value = null;
44+
}
45+
46+
echo '<br/>';
47+
48+
if (empty($jewel_uin_value)){
49+
echo wc_render_action_buttons(
50+
[
51+
[
52+
'action' => Config::DELIVERY_NAME . "-show_uin",
53+
'url' => '#',
54+
'name' => __('Add jewel UIN', 'cdekdelivery'),
55+
]
56+
]
57+
);
58+
}
59+
60+
echo sprintf(
61+
'<div class="%s-uin-input-container%s" data-id="%s">',
62+
Config::DELIVERY_NAME,
63+
empty($jewel_uin_value) ? ' hidden' : '',
64+
$itemId,
65+
);
66+
67+
woocommerce_wp_text_input(
68+
[
69+
'id' => Config::DELIVERY_NAME . "_jewel_uin_$itemId",
70+
'class' => Config::DELIVERY_NAME . '-jewel-uin',
71+
'label' => __('UIN: ', 'cdekdelivery'),
72+
'value' => $jewel_uin_value,
73+
]
74+
);
75+
76+
echo wc_render_action_buttons(
77+
[
78+
[
79+
'action' => Config::DELIVERY_NAME . '-save_uin',
80+
'url' => '#',
81+
'name' => __('Save', 'cdekdelivery'),
82+
]
83+
]
84+
);
85+
echo '</div>';
86+
87+
$this->enqueueScript();
88+
}
89+
90+
private function checkShipping(array $shipping_methods): bool
91+
{
92+
foreach ($shipping_methods as $shipping_method) {
93+
if (strpos($shipping_method->get_method_id(), Config::DELIVERY_NAME) !== false) {
94+
return true;
95+
}
96+
}
97+
98+
return false;
99+
}
100+
101+
private function enqueueScript(): void
102+
{
103+
UI::enqueueScript('cdek-order-item', 'cdek-order-item', true, false, true);
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)