| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class WcAdapter. |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Framework; |
| 11 |
|
| 12 |
use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 13 |
use WC_Blocks_Utils; |
| 14 |
use WC_Data_Store; |
| 15 |
use WC_Logger; |
| 16 |
use WC_Logger_Interface; |
| 17 |
use WC_Product; |
| 18 |
use WC_Shipping_Rate; |
| 19 |
use WC_Shipping_Zone; |
| 20 |
use WC_Shipping_Zone_Data_Store_Interface; |
| 21 |
use WC_Shipping_Zones; |
| 22 |
|
| 23 |
/** |
| 24 |
* Class WcAdapter. |
| 25 |
* |
| 26 |
* @package Packetery |
| 27 |
*/ |
| 28 |
class WcAdapter { |
| 29 |
use ActionSchedulerTrait; |
| 30 |
use WcCartTrait; |
| 31 |
use WcCustomerTrait; |
| 32 |
use WcPageTrait; |
| 33 |
use WcSessionTrait; |
| 34 |
use WcTaxTrait; |
| 35 |
|
| 36 |
/** |
| 37 |
* Converts weight from global unit to kg. |
| 38 |
* |
| 39 |
* @param int|float $weight Weight. |
| 40 |
* @param string $toUnit To unit. |
| 41 |
* |
| 42 |
* @return float |
| 43 |
*/ |
| 44 |
public function getWeight( $weight, string $toUnit ): float { |
| 45 |
return (float) wc_get_weight( $weight, $toUnit ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Gets WC product by id. |
| 50 |
* |
| 51 |
* @param mixed $productId Product ID. |
| 52 |
* |
| 53 |
* @return WC_Product|null |
| 54 |
*/ |
| 55 |
public function productFactoryGetProduct( $productId ): ?\WC_Product { |
| 56 |
$product = WC()->product_factory->get_product( $productId ); |
| 57 |
if ( $product instanceof WC_Product ) { |
| 58 |
return $product; |
| 59 |
} |
| 60 |
|
| 61 |
return null; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Gets product by post or post id. |
| 66 |
* |
| 67 |
* @param mixed $theProduct Post id or object. |
| 68 |
* |
| 69 |
* @return false|WC_Product|null |
| 70 |
*/ |
| 71 |
public function getProduct( $theProduct ) { |
| 72 |
return wc_get_product( $theProduct ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Creates new logger instance. |
| 77 |
* |
| 78 |
* @return WC_Logger |
| 79 |
*/ |
| 80 |
public function createLogger(): WC_Logger { |
| 81 |
return new WC_Logger(); |
| 82 |
} |
| 83 |
|
| 84 |
public function isCheckout(): bool { |
| 85 |
return is_checkout(); |
| 86 |
} |
| 87 |
|
| 88 |
public function featuresUtilDeclareCompatibility( string $featureId, string $pluginFile, bool $positiveCompatibility = true ): void { |
| 89 |
FeaturesUtil::declare_compatibility( $featureId, $pluginFile, $positiveCompatibility ); |
| 90 |
} |
| 91 |
|
| 92 |
public function storeApiRegisterUpdateCallback( array $args ): void { |
| 93 |
woocommerce_store_api_register_update_callback( $args ); |
| 94 |
} |
| 95 |
|
| 96 |
public function shipToBillingAddressOnly(): bool { |
| 97 |
return wc_ship_to_billing_address_only(); |
| 98 |
} |
| 99 |
|
| 100 |
public function shippingGetPackages(): array { |
| 101 |
return WC()->shipping()->get_packages(); |
| 102 |
} |
| 103 |
|
| 104 |
public function addNotice( string $message, string $noticeType ): void { |
| 105 |
wc_add_notice( $message, $noticeType ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @return WC_Logger|WC_Logger_Interface |
| 110 |
*/ |
| 111 |
public function getLogger() { |
| 112 |
return wc_get_logger(); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* @return array<string, array{ |
| 117 |
* name: string, |
| 118 |
* countries: array<int, string> |
| 119 |
* }> |
| 120 |
*/ |
| 121 |
public function countriesGetContinents(): array { |
| 122 |
return WC()->countries->get_continents(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* @param array{contents: array<string, array<string, mixed>>, contents_cost: float, applied_coupons: array, user: array{ID: int}, destination: array<string, string>, cart_subtotal: float, packetery_payment_method: mixed, rates: array<string, WC_Shipping_Rate>} $package |
| 127 |
* |
| 128 |
* @return WC_Shipping_Zone |
| 129 |
*/ |
| 130 |
public function shippingZonesGetZoneMatchingPackage( array $package ): WC_Shipping_Zone { |
| 131 |
return WC_Shipping_Zones::get_zone_matching_package( $package ); |
| 132 |
} |
| 133 |
|
| 134 |
public function hasBlockInPage( int $page, string $blockName ): bool { |
| 135 |
return WC_Blocks_Utils::has_block_in_page( $page, $blockName ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @param string $objectType |
| 140 |
* |
| 141 |
* @return WC_Data_Store|WC_Shipping_Zone_Data_Store_Interface |
| 142 |
*/ |
| 143 |
public function dataStoreLoad( string $objectType ): object { |
| 144 |
return WC_Data_Store::load( $objectType ); |
| 145 |
} |
| 146 |
} |
| 147 |
|