woocommerce/packages/js/extend-cart-checkout-block/$slug.php.mustache

138 lines
3.4 KiB
Plaintext
Raw Normal View History

Add `@woocommerce/extend-cart-checkout-block` package (#32323) * initial commit * 1.1.0 * Add additional files * 1.2.0 * Add sass file and comment to webpack.config.js * 1.3.0 * Use pascal case for class name * 1.4.0 * Add package lock * Add gitignore * Add plugin header * 1.5.0 * Add actions to initialise class * 1.6.0 * Fix issue with __FILE__ and version constant * 1.7.0 * Add scripts to package.lock.json * 1.8.0 * Move scripts to src and remove package.json template * 1.9.0 * Add JS script in root of src folder * 1.10.0 * Add correct directory to JS script * 1.11.0 * Add correct path to sass loader webpack * 1.12.0 * 1.13.0 * Remove automattic colour import * 1.14.0 * update src of scripts * 1.15.0 * update example data * 1.16.0 * update example with filters * 1.17.0 * Add filter for cod payment method * 1.18.0 * Create README.md * Update package.json (https://github.com/woocommerce/blocks-template/pull/1) * Add new file to house the integration class and prevent a fatal error (https://github.com/woocommerce/blocks-template/pull/2) * 1.19.0 * Update integration to use correct hooks * 1.20.0 * Include blocks integration file * 1.21.0 * Remove package lock and update repo references * Remove gitignore and update README * Add project.json * Move package to extend-cart-checkout-block * Update pnpm lock for new package name * Add new package name to dependency extraction webpack plugin * Remove unnecessary config options from webpack.config.js.mustache * Include npmDevDependencies in defaultValues * Add changelog for dependency extraction plugin * Add composer.json for changeglog generation * Add postinstall script to package.json * Remove test script from package.json * Reset package version to 1.0.0 Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2022-05-25 11:44:31 +00:00
<?php
/**
* Plugin Name: {{title}}
{{#description}}
* Description: {{description}}
{{/description}}
* Version: {{version}}
{{#author}}
* Author: {{author}}
{{/author}}
{{#license}}
* License: {{license}}
{{/license}}
{{#licenseURI}}
* License URI: {{{licenseURI}}}
{{/licenseURI}}
* Text Domain: {{textdomain}}
*
* @package {{namespace}}
*/
add_action( 'woocommerce_blocks_loaded',
function () {
register_block_type_from_metadata( __DIR__ . '/build/js/checkout-newsletter-subscription-block' );
require_once __DIR__ . '/{{slug}}-blocks-integration.php';
add_action(
'woocommerce_blocks_cart_block_registration',
function ( $integration_registry ) {
$integration_registry->register( new {{slugPascalCase}}_Blocks_Integration() );
}
);
add_action(
'woocommerce_blocks_checkout_block_registration',
function ( $integration_registry ) {
$integration_registry->register( new {{slugPascalCase}}_Blocks_Integration() );
}
);
}
);
/**
* Registers the slug as a block category with WordPress.
*/
function register_{{slugPascalCase}}_block_category( $categories ) {
return array_merge(
$categories,
[
[
'slug' => '{{slug}}',
'title' => __( '{{slugPascalCase}} Blocks', '{{slug}}' ),
],
]
);
}
add_action( 'block_categories_all', 'register_{{slugPascalCase}}_block_category', 10, 2 );
add_action( 'woocommerce_init', '{{slugPascalCase}}_register_custom_checkout_fields' );
/**
* Registers custom checkout fields for the WooCommerce checkout form.
*
* @return void
* @throws Exception If there is an error during the registration of the checkout fields.
*/
function {{slugPascalCase}}_register_custom_checkout_fields() {
if ( ! function_exists( 'woocommerce_register_additional_checkout_field' ) ) {
return;
}
woocommerce_register_additional_checkout_field(
array(
'id' => '{{slug}}/custom-checkbox',
'label' => 'Check this box to see a custom field on the order.',
'location' => 'contact',
'type' => 'checkbox',
)
);
woocommerce_register_additional_checkout_field(
array(
'id' => '{{slug}}/custom-text-input',
'label' => "{{slugPascalCase}}'s example text input",
'location' => 'address',
'type' => 'text',
)
);
/**
* Sanitizes the value of the custom text input field. For demo purposes we will just turn it to all caps.
*/
add_action(
'woocommerce_sanitize_additional_field',
function ( $value, $key, $group ) {
if ( '{{slug}}/custom-text-input' === $key ) {
return strtoupper( $value );
}
return $value;
},
10,
3
);
/**
* Validates the custom text input field. For demo purposes we will not accept the string 'INVALID'.
*/
add_action(
'woocommerce_blocks_validate_location_address_fields',
function ( \WP_Error $errors, $fields, $group ) {
if ( 'INVALID' === $fields['{{slug}}/custom-text-input'] ) {
$errors->add( 'invalid_text_detected', 'Please ensure your custom text input is not "INVALID".' );
}
},
10,
3
);
woocommerce_register_additional_checkout_field(
array(
'id' => '{{slug}}/custom-select-input',
'label' => "{{slugPascalCase}}'s example select input",
'location' => 'order',
'type' => 'select',
'options' => [
[
'label' => 'Option 1',
'value' => 'option1',
],
[
'label' => 'Option 2',
'value' => 'option2',
],
],
)
);
}