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