From d423b2ad6472329fb16b48981c384cd6794e57a6 Mon Sep 17 00:00:00 2001 From: gaminikhil Date: Thu, 4 Jul 2024 12:45:26 +0530 Subject: [PATCH] Fix #237 fees not showing on block checkout Fix #237 fees not showing on block checkout page --- build/checkout-block-frontend.js | 33 ----------- build/checkout-fees-for-woocommerce.asset.php | 2 +- build/checkout-fees-for-woocommerce.js | 2 +- src/frontend.js | 59 ++++--------------- 4 files changed, 15 insertions(+), 81 deletions(-) delete mode 100644 build/checkout-block-frontend.js diff --git a/build/checkout-block-frontend.js b/build/checkout-block-frontend.js deleted file mode 100644 index 6fca995..0000000 --- a/build/checkout-block-frontend.js +++ /dev/null @@ -1,33 +0,0 @@ -(()=>{ - "use strict"; - const e = window.wp.element - , t = JSON.parse('{"$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"checkout-fees-for-woocommerce/add-fees","version":"1.0.0","title":"Payment Gateway Based Fees and Discounts","category":"woocommerce","parent":["woocommerce/checkout-shipping-address-block"],"attributes":{"lock":{"type":"object","default":{"remove":true,"move":true}}},"textdomain":"checkout-fees-for-woocommerce","editorScript":"file:./build/index.js"}') - , o = window.wc.blocksCheckout - , {registerCheckoutBlock: c} = (window.wp.i18n, - wc.blocksCheckout); - c({ - metadata: t, - component: ({children: t, checkoutExtensionData: c})=>{ - const [a,n] = (0, - e.useState)("") - , {setExtensionData: s} = c; - (0, - e.useEffect)((()=>{ - jQuery(document).on("change", 'input[name="radio-control-wc-payment-method-options"]', (function(e) { - console.log("here"), - (0, - o.extensionCartUpdate)({ - namespace: "checkout-fees-for-woocommerce", - data: { - shipping_method: document.querySelector('input[name="radio-control-0"]:checked').value, - payment_method: e.target.value - } - }) - } - )) - } - ), []); - } - }) -} -)(); diff --git a/build/checkout-fees-for-woocommerce.asset.php b/build/checkout-fees-for-woocommerce.asset.php index e1dff40..0064d0e 100644 --- a/build/checkout-fees-for-woocommerce.asset.php +++ b/build/checkout-fees-for-woocommerce.asset.php @@ -1 +1 @@ - array('wc-blocks-checkout', 'wp-element', 'wp-i18n'), 'version' => '7b2d759ead81217562b8'); + array(), 'version' => '809990fb9b3428ea2e63'); diff --git a/build/checkout-fees-for-woocommerce.js b/build/checkout-fees-for-woocommerce.js index 673dede..5f76f36 100644 --- a/build/checkout-fees-for-woocommerce.js +++ b/build/checkout-fees-for-woocommerce.js @@ -1 +1 @@ -(()=>{"use strict";const e=JSON.parse('{"$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"checkout-fees-for-woocommerce/add_fees","version":"1.0.0","title":"Payment Gateway Based Fees and Discounts","category":"woocommerce","parent":["woocommerce/checkout-shipping-address-block"],"attributes":{"lock":{"type":"object","default":{"remove":true,"move":true}}},"textdomain":"checkout-fees-for-woocommerce","editorScript":"file:./build/index.js"}'),o=window.wc.blocksCheckout,t=(window.wp.i18n,window.wp.element),{registerCheckoutBlock:c}=wc.blocksCheckout;c({metadata:e,component:({children:e,checkoutExtensionData:c})=>{const[a,s]=(0,t.useState)(""),{setExtensionData:n}=c;(0,t.useEffect)((()=>{n("checkout-fees-for-woocommerce","add_fees",a),wp.hooks.addAction("experimental__woocommerce_blocks-checkout-set-active-payment-method","checkout-block-example",(function(e){console.log(e.value),(0,o.extensionCartUpdate)({namespace:"checkout-fees-for-woocommerce",data:{payment_method:e.value}})})),wp.hooks.addAction("experimental__woocommerce_blocks-checkout-set-selected-shipping-rate","checkout-block-example",(function(e){console.log(e.storeCart.paymentMethods[0]),(0,o.extensionCartUpdate)({namespace:"checkout-fees-for-woocommerce",data:{payment_method:e.storeCart.paymentMethods[0]}})}))}),[]),(0,t.useCallback)((e=>{s(e),n("checkout-fees-for-woocommerce","add_fees",e)}),[s.setExtensionData])}})})(); \ No newline at end of file +(()=>{const{PAYMENT_STORE_KEY:e}=window.wc.wcBlocksData,{extensionCartUpdate:t}=window.wc.blocksCheckout,{subscribe:c,select:o}=wp.data;c((function(){const c=o(e).getActivePaymentMethod();t({namespace:"checkout-fees-for-woocommerce",data:{payment_method:c}})}),e)})(); \ No newline at end of file diff --git a/src/frontend.js b/src/frontend.js index a36a9d7..cabad0c 100644 --- a/src/frontend.js +++ b/src/frontend.js @@ -1,47 +1,14 @@ -import metadata from './block.json'; -import { ValidatedTextInput } from '@woocommerce/blocks-checkout'; -import { __ } from '@wordpress/i18n'; -import { useEffect, useState, useCallback } from '@wordpress/element'; -import { extensionCartUpdate } from '@woocommerce/blocks-checkout'; - -// Global import -const { registerCheckoutBlock } = wc.blocksCheckout; - -const Block = ({ children, checkoutExtensionData }) => { - const { setExtensionData } = checkoutExtensionData; - - useEffect( () => { - - wp.hooks.addAction( 'experimental__woocommerce_blocks-checkout-set-active-payment-method', 'checkout-block-example', function( payment_method ) { - var update_cart = extensionCartUpdate( { - namespace: 'checkout-fees-for-woocommerce', - data: { - payment_method: payment_method.value, - }, - }); +const { PAYMENT_STORE_KEY } = window.wc.wcBlocksData; // "wc/store/payment" +const { extensionCartUpdate } = window.wc.blocksCheckout; +const { subscribe, select } = wp.data; +let previouslyChosenPaymentMethod = ''; + +subscribe( function () { + const chosenPaymentMethod = select( PAYMENT_STORE_KEY ).getActivePaymentMethod(); + extensionCartUpdate( { + namespace: 'checkout-fees-for-woocommerce', + data: { + payment_method: chosenPaymentMethod, + }, } ); - - wp.hooks.addAction( 'experimental__woocommerce_blocks-checkout-set-selected-shipping-rate', 'checkout-block-example', function( shipping ) { - var update_cart = extensionCartUpdate( { - namespace: 'checkout-fees-for-woocommerce', - data: { - payment_method: shipping.storeCart.paymentMethods[0], - }, - }); - } ); - - }, [] ); - - const onInputChange = useCallback( - ( value ) => { - }, - [ setGiftMessage. setExtensionData ] - ) -} - -const options = { - metadata, - component: Block -}; - -registerCheckoutBlock( options ); \ No newline at end of file +}, PAYMENT_STORE_KEY ); \ No newline at end of file