You've already forked wc-licensed-product
Fix license generation and checkout domain field bugs
- Add WooCommerce Checkout Blocks support for domain field - Create CheckoutBlocksIntegration for block-based checkout - Create StoreApiExtension for Store API domain handling - Add checkout-blocks.js for frontend domain field in blocks - Fix LicenseManager product type check in generateLicense() - Add multiple order status hooks for reliable license generation Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
127
src/Checkout/CheckoutBlocksIntegration.php
Normal file
127
src/Checkout/CheckoutBlocksIntegration.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
/**
|
||||
* WooCommerce Checkout Blocks Integration
|
||||
*
|
||||
* @package Jeremias\WcLicensedProduct\Checkout
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jeremias\WcLicensedProduct\Checkout;
|
||||
|
||||
use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;
|
||||
|
||||
/**
|
||||
* Integration with WooCommerce Checkout Blocks
|
||||
*/
|
||||
final class CheckoutBlocksIntegration implements IntegrationInterface
|
||||
{
|
||||
/**
|
||||
* The name of the integration
|
||||
*/
|
||||
public function get_name(): string
|
||||
{
|
||||
return 'wc-licensed-product';
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the integration
|
||||
*/
|
||||
public function initialize(): void
|
||||
{
|
||||
$this->registerScripts();
|
||||
$this->registerBlockExtensionData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register scripts for the checkout block
|
||||
*/
|
||||
private function registerScripts(): void
|
||||
{
|
||||
$scriptPath = WC_LICENSED_PRODUCT_PLUGIN_DIR . 'assets/js/checkout-blocks.js';
|
||||
$scriptUrl = WC_LICENSED_PRODUCT_PLUGIN_URL . 'assets/js/checkout-blocks.js';
|
||||
|
||||
if (file_exists($scriptPath)) {
|
||||
wp_register_script(
|
||||
'wc-licensed-product-checkout-blocks',
|
||||
$scriptUrl,
|
||||
['wc-blocks-checkout', 'wp-element', 'wp-components', 'wp-i18n'],
|
||||
WC_LICENSED_PRODUCT_VERSION,
|
||||
true
|
||||
);
|
||||
|
||||
wp_set_script_translations(
|
||||
'wc-licensed-product-checkout-blocks',
|
||||
'wc-licensed-product',
|
||||
WC_LICENSED_PRODUCT_PLUGIN_DIR . 'languages'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register block extension data
|
||||
*/
|
||||
private function registerBlockExtensionData(): void
|
||||
{
|
||||
// Pass data to the checkout block script
|
||||
add_filter(
|
||||
'woocommerce_blocks_checkout_block_registration_data',
|
||||
function (array $data): array {
|
||||
$data['wc-licensed-product'] = [
|
||||
'hasLicensedProducts' => $this->cartHasLicensedProducts(),
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of script handles to enqueue in the frontend context
|
||||
*/
|
||||
public function get_script_handles(): array
|
||||
{
|
||||
return ['wc-licensed-product-checkout-blocks'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of script handles to enqueue in the editor context
|
||||
*/
|
||||
public function get_editor_script_handles(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns script data to pass to the frontend scripts
|
||||
*/
|
||||
public function get_script_data(): array
|
||||
{
|
||||
return [
|
||||
'hasLicensedProducts' => $this->cartHasLicensedProducts(),
|
||||
'fieldLabel' => __('Domain for License Activation', 'wc-licensed-product'),
|
||||
'fieldPlaceholder' => __('example.com', 'wc-licensed-product'),
|
||||
'fieldDescription' => __('Enter the domain where you will use this license (without http:// or www).', 'wc-licensed-product'),
|
||||
'sectionTitle' => __('License Domain', 'wc-licensed-product'),
|
||||
'validationError' => __('Please enter a valid domain for your license activation.', 'wc-licensed-product'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if cart contains licensed products
|
||||
*/
|
||||
private function cartHasLicensedProducts(): bool
|
||||
{
|
||||
if (!WC()->cart) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (WC()->cart->get_cart() as $cartItem) {
|
||||
$product = $cartItem['data'];
|
||||
if ($product && $product->is_type('licensed')) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user