Skip to content

Commit

Permalink
fix: order creation
Browse files Browse the repository at this point in the history
  • Loading branch information
vermorag committed Nov 27, 2024
1 parent eb6d420 commit 683da25
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: cdekit, caelan
Tags: ecommerce, shipping, delivery, woocommerce
Requires at least: 6.0
Requires PHP: 7.4
Tested up to: 6.5
Tested up to: 6.7
Stable tag: dev
License: GPLv3

Expand Down
3 changes: 1 addition & 2 deletions cdek.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
* Author: CDEKIT
* Author URI: https://cdek.ru
* WC requires at least: 6.9
* WC tested up to: 8.0
* WC tested up to: 9.4
* License: GPLv3
*/


defined('ABSPATH') or exit;

if (file_exists(__DIR__ . '/vendor/autoload.php')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/OrderCreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private function buildRequestData(): array
],
],
'from_location' => [
'city' => $deliveryMethod->city_code,
'code' => $deliveryMethod->city_code,
'address' => $deliveryMethod->address,
],
'developer_key' => Config::DEV_KEY,
Expand Down
4 changes: 3 additions & 1 deletion src/Blocks/CheckoutMapBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ public static function extend_cart_data(): array

try {
$city = $api->cityCodeGet($cityInput, $postcodeInput);
$points = $city !== null ? $api->officeListRaw($city) : '[]';
} catch (ExceptionContract $e) {
$city = null;
$points = '[]';
}

return [
Expand All @@ -67,7 +69,7 @@ public static function extend_cart_data(): array
'postcode' => $postcodeInput,
],
'city' => $city,
'points' => $city !== null ? $api->officeListRaw($city) : '[]',
'points' => $points,
];
}

Expand Down
37 changes: 16 additions & 21 deletions src/Helpers/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@
namespace Cdek\Helpers {

use Cdek\Exceptions\CacheException;
use Cdek\Loader;

class Cache
{
private const CACHE_FILE_NAME = '.cache.php';
private const CACHE_FILE_NAME = '.cdekdelivery.php';
private static ?array $store = null;

public static function clear(): void
{
unlink(WP_CONTENT_DIR.DIRECTORY_SEPARATOR.self::CACHE_FILE_NAME);
self::$store = null;
}

/**
* @param string $key
* @param callable $callback
Expand Down Expand Up @@ -50,11 +55,11 @@ public static function has(string $key): bool

private static function loadCache(): bool
{
if (!file_exists(Loader::getPluginPath(self::CACHE_FILE_NAME))) {
if (!file_exists(WP_CONTENT_DIR.DIRECTORY_SEPARATOR.self::CACHE_FILE_NAME)) {
return false;
}

self::$store = require(Loader::getPluginPath(self::CACHE_FILE_NAME));
self::$store = require(WP_CONTENT_DIR.DIRECTORY_SEPARATOR.self::CACHE_FILE_NAME);

return true;
}
Expand Down Expand Up @@ -91,28 +96,18 @@ public static function put($key, $value = null): void
self::$store[$key] = $value;
}

if (file_exists(Loader::getPluginPath(self::CACHE_FILE_NAME))) {
if (!is_writable(Loader::getPluginPath(self::CACHE_FILE_NAME))) {
throw new CacheException(
Loader::getPluginPath(self::CACHE_FILE_NAME),
);
if (file_exists(WP_CONTENT_DIR.DIRECTORY_SEPARATOR.self::CACHE_FILE_NAME)) {
if (!is_writable(WP_CONTENT_DIR.DIRECTORY_SEPARATOR.self::CACHE_FILE_NAME)) {
throw new CacheException(WP_CONTENT_DIR.DIRECTORY_SEPARATOR.self::CACHE_FILE_NAME);
}
} elseif (!is_writable(Loader::getPluginPath())) {
throw new CacheException(
Loader::getPluginPath(),
);
} elseif (!is_writable(WP_CONTENT_DIR)) {
throw new CacheException(WP_CONTENT_DIR);
}

$fp = fopen(Loader::getPluginPath(self::CACHE_FILE_NAME), 'wb');
$fp = fopen(WP_CONTENT_DIR.DIRECTORY_SEPARATOR.self::CACHE_FILE_NAME, 'wb');

fwrite($fp, '<?php return '.var_export(self::$store, true).';'.PHP_EOL);
fwrite($fp, '<?php defined("ABSPATH") or exit; return '.var_export(self::$store, true).';'.PHP_EOL);
fclose($fp);
}

public static function clear(): void
{
unlink(Loader::getPluginPath(self::CACHE_FILE_NAME));
self::$store = null;
}
}
}
2 changes: 1 addition & 1 deletion src/ShippingMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ final public function admin_options(): void
{
$error = (new CdekApi)->authGetError();
if ($error !== null) {
if ($error === 'invalid_client') {
if ($error === 'invalid_client' || $error === 'unauthorized') {
WC_Admin_Settings::add_error(
esc_html__(
'Error receiving token from CDEK API. Make sure the integration keys are correct',
Expand Down

0 comments on commit 683da25

Please sign in to comment.