| 1 |
/** |
| 2 |
* Block registration. |
| 3 |
* |
| 4 |
* @package Packetery |
| 5 |
*/ |
| 6 |
|
| 7 |
import { registerBlockType } from '@wordpress/blocks'; |
| 8 |
import { registerCheckoutBlock } from '@woocommerce/blocks-checkout'; |
| 9 |
|
| 10 |
import metadata from './block.json'; |
| 11 |
import { Edit } from './Edit'; |
| 12 |
import { View } from './View'; |
| 13 |
import { __ } from '@wordpress/i18n'; |
| 14 |
import { saveShippingAndPaymentMethods } from './saveShippingAndPaymentMethods'; |
| 15 |
|
| 16 |
registerBlockType( metadata, { |
| 17 |
title: __( 'title', 'packeta' ), |
| 18 |
description: __( 'description', 'packeta' ), |
| 19 |
edit: Edit, |
| 20 |
} ); |
| 21 |
|
| 22 |
registerCheckoutBlock( { |
| 23 |
metadata, |
| 24 |
component: View, |
| 25 |
} ); |
| 26 |
|
| 27 |
const { extensionCartUpdate } = wc.blocksCheckout; |
| 28 |
|
| 29 |
wp.hooks.addAction( 'experimental__woocommerce_blocks-checkout-set-selected-shipping-rate', 'packetery-js-hooks', function ( shipping ) { |
| 30 |
saveShippingAndPaymentMethods( shipping, null ); |
| 31 |
} ); |
| 32 |
|
| 33 |
wp.hooks.addAction( 'experimental__woocommerce_blocks-checkout-set-active-payment-method', 'packetery-js-hooks', function ( payment ) { |
| 34 |
saveShippingAndPaymentMethods( null, payment ); |
| 35 |
} ); |
| 36 |
|
| 37 |
// Used to clear values upon checkout page reload. It is needed to save something. |
| 38 |
wp.hooks.addAction( 'experimental__woocommerce_blocks-checkout-render-checkout-form', 'packetery-js-hooks', function ( form ) { |
| 39 |
extensionCartUpdate( { |
| 40 |
namespace: 'packetery-js-hooks', |
| 41 |
data: { |
| 42 |
shipping_method: 'n/a', |
| 43 |
payment_method: 'n/a', |
| 44 |
}, |
| 45 |
} ); |
| 46 |
} ); |
| 47 |
|
| 48 |
wp.hooks.addAction( 'packetery_save_shipping_and_payment_methods', 'packetery-js-hooks', function ( shippingRateId, paymentId ) { |
| 49 |
const shippingObject = shippingRateId ? { shippingRateId: shippingRateId } : null; |
| 50 |
const paymentObject = paymentId !== '' ? { value: paymentId } : null; |
| 51 |
saveShippingAndPaymentMethods( shippingObject, paymentObject ); |
| 52 |
} ); |
| 53 |
|