| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Packetery\Module\Blocks; |
| 6 |
|
| 7 |
use Automattic\WooCommerce\Blocks\Integrations\IntegrationRegistry; |
| 8 |
use Packetery\Module\Blocks; |
| 9 |
use Packetery\Module\Checkout\CheckoutSettings; |
| 10 |
use Packetery\Module\Framework\WcAdapter; |
| 11 |
use Packetery\Module\Framework\WpAdapter; |
| 12 |
|
| 13 |
class BlockHooks { |
| 14 |
|
| 15 |
/** |
| 16 |
* @var CheckoutSettings |
| 17 |
*/ |
| 18 |
private $checkoutSettings; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var WpAdapter |
| 22 |
*/ |
| 23 |
private $wpAdapter; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var WcAdapter |
| 27 |
*/ |
| 28 |
private $wcAdapter; |
| 29 |
|
| 30 |
public function __construct( |
| 31 |
CheckoutSettings $checkoutSettings, |
| 32 |
WpAdapter $wpAdapter, |
| 33 |
WcAdapter $wcAdapter |
| 34 |
) { |
| 35 |
$this->checkoutSettings = $checkoutSettings; |
| 36 |
$this->wpAdapter = $wpAdapter; |
| 37 |
$this->wcAdapter = $wcAdapter; |
| 38 |
} |
| 39 |
|
| 40 |
public function registerCheckoutBlock( IntegrationRegistry $integrationRegistry ): void { |
| 41 |
$integrationRegistry->register( |
| 42 |
new Blocks\WidgetIntegration( |
| 43 |
$this->checkoutSettings->createSettings() |
| 44 |
) |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
public function register(): void { |
| 49 |
// This hook is tested. |
| 50 |
$this->wpAdapter->addFilter( |
| 51 |
'__experimental_woocommerce_blocks_add_data_attributes_to_block', |
| 52 |
function ( $allowedBlocks ) { |
| 53 |
$allowedBlocks[] = 'packeta/packeta-widget'; |
| 54 |
|
| 55 |
return $allowedBlocks; |
| 56 |
} |
| 57 |
); |
| 58 |
// This hook is expected replacement in the future. |
| 59 |
$this->wpAdapter->addFilter( |
| 60 |
'woocommerce_blocks_add_data_attributes_to_block', |
| 61 |
function ( $allowedBlocks ) { |
| 62 |
$allowedBlocks[] = 'packeta/packeta-widget'; |
| 63 |
|
| 64 |
return $allowedBlocks; |
| 65 |
} |
| 66 |
); |
| 67 |
|
| 68 |
$this->wpAdapter->addAction( |
| 69 |
'woocommerce_blocks_checkout_block_registration', |
| 70 |
[ |
| 71 |
$this, |
| 72 |
'registerCheckoutBlock', |
| 73 |
] |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
public function saveShippingAndPaymentMethodsToSession( array $orderUpdateData ): void { |
| 78 |
if ( isset( $orderUpdateData['shipping_method'] ) ) { |
| 79 |
$this->wcAdapter->sessionSet( 'packetery_checkout_shipping_method', $orderUpdateData['shipping_method'] ); |
| 80 |
} |
| 81 |
|
| 82 |
if ( isset( $orderUpdateData['payment_method'] ) ) { |
| 83 |
$this->wcAdapter->sessionSet( 'packetery_checkout_payment_method', $orderUpdateData['payment_method'] ); |
| 84 |
} |
| 85 |
|
| 86 |
$this->wcAdapter->cartCalculateTotals(); |
| 87 |
} |
| 88 |
|
| 89 |
public function orderUpdateCallback(): void { |
| 90 |
if ( ! function_exists( 'woocommerce_store_api_register_update_callback' ) ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
$this->wcAdapter->storeApiRegisterUpdateCallback( |
| 94 |
[ |
| 95 |
'namespace' => 'packetery-js-hooks', |
| 96 |
'callback' => [ $this, 'saveShippingAndPaymentMethodsToSession' ], |
| 97 |
] |
| 98 |
); |
| 99 |
} |
| 100 |
} |
| 101 |
|