Skip to content

Commit

Permalink
Merge pull request #8 from Kalpa-Services/1-payment-option-not-visibl…
Browse files Browse the repository at this point in the history
…e-on-frontend

1 payment option not visible on frontend
  • Loading branch information
carlHandy authored Jul 18, 2024
2 parents caa1529 + 543bfff commit 1212933
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
8 changes: 8 additions & 0 deletions includes/class-mmg-checkout-payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public function enqueue_scripts() {
wp_localize_script('mmg-checkout', 'mmg_checkout_params', array(
'ajax_url' => admin_url('admin-ajax.php'),
));

// For blocks support
$gateway_settings = get_option('woocommerce_mmg_checkout_settings', array());
wp_localize_script('wc-mmg-payments-blocks', 'mmgCheckoutData', array(
'title' => isset($gateway_settings['title']) ? $gateway_settings['title'] : 'MMG Checkout',
'description' => isset($gateway_settings['description']) ? $gateway_settings['description'] : 'Pay with MMG Checkout',
'supports' => array('products', 'refunds'),
));
}

public function generate_checkout_url() {
Expand Down
8 changes: 7 additions & 1 deletion includes/class-wc-mmg-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public function __construct() {
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');

$this->supports = array(
'products',
'refunds',
'checkout_block_support',
);

add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
}
Expand Down Expand Up @@ -71,4 +77,4 @@ public function receipt_page($order_id) {
</script>
<?php
}
}
}
32 changes: 32 additions & 0 deletions includes/class-wc-mmg-payments-blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;

class WC_MMG_Payments_Blocks extends AbstractPaymentMethodType {
protected $name = 'mmg_checkout';

public function initialize() {
$this->settings = get_option('woocommerce_mmg_checkout_settings', array());
}

public function is_active() {
return ! empty($this->settings['enabled']) && 'yes' === $this->settings['enabled'];
}

public function get_payment_method_script_handles() {
wp_register_script(
'wc-mmg-payments-blocks',
plugins_url('js/mmg-checkout-blocks.js', dirname(__FILE__)),
array('wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n'),
filemtime(plugin_dir_path(dirname(__FILE__)) . 'js/mmg-checkout-blocks.js'),
true
);
return array('wc-mmg-payments-blocks');
}

public function get_payment_method_data() {
return array(
'title' => $this->settings['title'],
'description' => $this->settings['description'],
);
}
}
23 changes: 23 additions & 0 deletions js/mmg-checkout-blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { registerPaymentMethod } = window.wc.wcBlocksRegistry;
const { createElement } = window.wp.element;
const { decodeEntities } = window.wp.htmlEntities;

const MMGCheckoutLabel = ({ title }) => {
return createElement('span', {}, decodeEntities(title));
};

const MMGCheckoutContent = ({ description }) => {
return createElement('p', {}, decodeEntities(description));
};

registerPaymentMethod({
name: 'mmg_checkout',
label: createElement(MMGCheckoutLabel, { title: mmgCheckoutData.title }),
content: createElement(MMGCheckoutContent, { description: mmgCheckoutData.description }),
edit: createElement(MMGCheckoutContent, { description: mmgCheckoutData.description }),
canMakePayment: () => true,
ariaLabel: decodeEntities(mmgCheckoutData.title),
supports: {
features: mmgCheckoutData.supports,
},
});
14 changes: 14 additions & 0 deletions main.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,18 @@ function mmg_checkout_init() {
new MMG_Checkout_Payment();
}
add_action('plugins_loaded', 'mmg_checkout_init');
}

add_action('woocommerce_blocks_loaded', 'mmg_checkout_register_block_support');

function mmg_checkout_register_block_support() {
if (class_exists('Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType')) {
require_once plugin_dir_path(__FILE__) . 'includes/class-wc-mmg-payments-blocks.php';
add_action(
'woocommerce_blocks_payment_method_type_registration',
function(Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry) {
$payment_method_registry->register(new WC_MMG_Payments_Blocks());
}
);
}
}

0 comments on commit 1212933

Please sign in to comment.