How to get WooCommerce checkout page to wait while showing loading screen? - php

I'm new and I'm trying to code a crypto payment gateway for WooCommerce by integrating with Metamask. When I press "Place Order" it could trigger Metamask Chrome Ext to pop but then I get "Error processing checkout. Please try again" on the checkout page. WC status log shows nothing. Debug logs nothing too as well as no error in browser console.
Anyway, how do I add a "loading" spinner on checkout page while the payment is processing?
add_filter( 'woocommerce_payment_gateways', 'add_custom_gateway_class' );
function add_custom_gateway_class( $gateways ) {
$gateways[] = 'WC_Crypto_Gateway';
return $gateways;
}
add_action( 'plugins_loaded', 'initialize_gateway_class' );
function initialize_gateway_class() {
class WC_Crypto_Gateway extends WC_Payment_Gateway {
public function __construct() {
$this->id = 'crypto'; // payment gateway ID
$this->icon = ''; // payment gateway icon
$this->has_fields = true; // for custom credit card form
$this->title = __( 'Crypto Payment Gateway', 'text-domain' ); // vertical tab title
$this->method_title = __( 'Crypto Payment Gateway', 'text-domain' ); // payment method name
$this->method_description = __( 'To allow users to pay using cryptocurrencies such as ETH', 'text-domain' ); // payment method description
// load backend options fields
$this->init_form_fields();
// load the settings.
$this->init_settings();
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->enabled = $this->get_option( 'enabled' );
// Action hook to saves the settings
if(is_admin()) {
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
}
// Action hook to load custom JavaScript
add_action( 'wp_enqueue_scripts', array( $this, 'payment_gateway_scripts' ) );
}
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'text-domain' ),
'label' => __( 'Enable Crypto Payment Gateway', 'text-domain' ),
'type' => 'checkbox',
'description' => __( 'This enable the gateway which allow to accept payment via cryptocurrency.', 'text-domain' ),
'default' => 'no',
'desc_tip' => true
),
'description' => array(
'title' => __( 'Description', 'text-domain' ),
'type' => 'textarea',
'description' => __( 'This controls the description which the user sees during checkout.', 'text-domain' ),
'default' => __( 'Pay with your cryptos such as ETH, BTC.', 'text-domain' ),
)
);
}
public function payment_fields() {
}
public function payment_gateway_scripts() {
global $woocommerce;
// process a token only on cart/checkout pages
if ( ! is_cart() && ! is_checkout() ) {
return;
}
// stop enqueue JS if payment gateway is disabled
if ( 'no' === $this->enabled ) {
return;
}
// stop enqueue JS if site without SSL
if ( ! is_ssl() ) {
return;
}
?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ethers/5.5.3/ethers.umd.min.js"></script>
<?php
wp_register_script( 'woocommerce_pay_crypto', plugins_url( 'web3-connector.js', __FILE__ ) );
wp_enqueue_script( 'woocommerce_pay_crypto' );
}
public function process_payment( $order_id ) {
global $woocommerce;
// get order detailes
$order = wc_get_order( $order_id );
}
}
}
In my js, I basically call the below and disable the order button.
$("form.woocommerce-checkout").on("submit", async function (e) {
e.preventDefault();
//Get the payment total
const tokenAmount = document.getElementById("total-amount");
$("#place_order").html(
'<span class="woocommerce-checkout.processing blockUI.blockOverlay"></span>'
);
$("#place_order").attr("disabled", true);

Related

Issue with action button in WooCommerce admin order list for custom order status

I have created custom order status "Ready to dispatch" and custom action button as well.
The problem and question is: when I have clicked on the custom action button, order status has changed, but default action button "Complete" disappeared.
How can I make that after click of custom action button, "Complete" action button retain?
Any advice would be appreciated
My current code:
// Register new status
function register_awaiting_shipment_order_status() {
register_post_status( 'wc-ready-to-dispatch', array(
'label' => 'Ready to dispatch',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Ready to dispatch (%s)', 'Ready to dispatch (%s)' )
) );
}
add_action( 'init', 'register_awaiting_shipment_order_status' );
// Add your custom order status action button (for orders with "processing" status)
add_filter( 'woocommerce_admin_order_actions', 'add_custom_order_status_actions_button', 100, 2 );
function add_custom_order_status_actions_button( $actions, $order ) {
// Display the button for all orders that have a 'processing' status
if ( $order->has_status( array( 'processing' ) ) ) {
$action_slug = 'ready-to-dispatch';
// Get Order ID (compatibility all WC versions)
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
// Set the action button
$actions['ready-to-dispatch'] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=ready-to-dispatch&order_id=' . $order_id ), 'woocommerce-mark-order-status' ),
'name' => __( 'Ready to dispatch', 'woocommerce' ),
'action' => 'ready-to-dispatch', // keep "view" class for a clean button CSS
);
}
return $actions;
}
// Set Here the WooCommerce icon for your action button
add_action( 'admin_head', 'add_custom_order_status_actions_button_css' );
function add_custom_order_status_actions_button_css() {
$action_slug = 'ready-to-dispatch';
echo '<style>.wc-action-button-'.$action_slug.'::after { font-family: woocommerce !important; content: "\f344" !important; }</style>';
}
// Adding custom status 'awaiting-delivery' to order edit pages dropdown
add_filter( 'wc_order_statuses', 'custom_wc_order_statuses' );
function custom_wc_order_statuses( $order_statuses ) {
$new_order_statuses = array();
// add new order status after processing
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_statuses['wc-ready-to-dispatch'] = 'Ready to dispatch';
}
}
return $new_order_statuses;
}
// Adding custom status 'awaiting-delivery' to admin order list bulk dropdown
add_filter( 'bulk_actions-edit-shop_order', 'custom_dropdown_bulk_actions_shop_order', 1, 1 );
function custom_dropdown_bulk_actions_shop_order( $actions ) {
$actions['mark_ready-to-dispatch'] = __( 'Ready to dispatch', 'woocommerce' );
return $actions;
}
That's because in the woocommerce_admin_order_actions hook an if condition is missing, to display the button if the order contains the status ready-to-dispatch.
I have rewritten your code with this updated code. For example, the init hook has been replaced with woocommerce_register_shop_order_post_statuses to register custom order statuses.
So you get:
/**
* Register order status
*/
function filter_woocommerce_register_shop_order_post_statuses( $order_statuses ) {
// Status must start with "wc-"
$order_statuses['wc-ready-to-dispatch'] = array(
'label' => _x( 'Ready to dispatch', 'Order status', 'woocommerce' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
/* translators: %s: number of orders */
'label_count' => _n_noop( 'Ready to dispatch <span class="count">(%s)</span>', 'Abonnement <span class="count">(%s)</span>', 'woocommerce' ),
);
return $order_statuses;
}
add_filter( 'woocommerce_register_shop_order_post_statuses', 'filter_woocommerce_register_shop_order_post_statuses', 10, 1 );
/**
* Show order status in the dropdown # single order
*/
function filter_wc_order_statuses( $order_statuses ) {
$new_order_statuses = array();
// add new order status after processing
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
// Status must start with "wc-"
$new_order_statuses['wc-ready-to-dispatch'] = _x( 'Ready to dispatch', 'Order status', 'woocommerce' );
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'filter_wc_order_statuses', 10, 1 );
/**
* Show order status in the dropdown # bulk actions
*/
function filter_bulk_actions_edit_shop_order( $bulk_actions ) {
// Note: "mark_" must be there instead of "wc"
$bulk_actions['mark_ready-to-dispatch'] = __( 'Ready to dispatch', 'woocommerce' );
return $bulk_actions;
}
add_filter( 'bulk_actions-edit-shop_order', 'filter_bulk_actions_edit_shop_order', 10, 1 );
/**
* Add quick action button # admin orders list
*/
function filter_order_actions( $actions, $order ) {
// Get Order ID (compatibility all WC versions)
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
// Display the button for all orders that have a 'processing' status
if ( $order->has_status( array( 'processing' ) ) ) {
$action_slug = 'ready-to-dispatch';
// Set the action button
$actions['ready-to-dispatch'] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=ready-to-dispatch&order_id=' . $order_id ), 'woocommerce-mark-order-status' ),
'name' => __( 'Ready to dispatch', 'woocommerce' ),
'action' => $action_slug, // keep "view" class for a clean button CSS
);
}
// Display the button for all orders that have a 'ready-to-dispatch' status
if ( $order->has_status( array( 'ready-to-dispatch' ) ) ) {
$action_slug = 'complete';
// Set the action button
$actions['complete'] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=completed&order_id=' . $order_id ), 'woocommerce-mark-order-status' ),
'name' => __( 'Complete', 'woocommerce' ),
'action' => $action_slug,
);
}
return $actions;
}
add_filter( 'woocommerce_admin_order_actions', 'filter_order_actions', 10, 2 );
/**
* Add WooCommerce icon for your action button # admin orders list
*/
function action_admin_head() {
$action_slug = 'ready-to-dispatch';
echo '<style>.wc-action-button-' . $action_slug . '::after { content: "\f344" !important; }</style>';
}
add_action( 'admin_head', 'action_admin_head' );

How to reorder custom tab in woocommerce my account page?

I created a custom tab (My Favorites) for my Woocommerce my account page and placed the below code in my child theme's functions.php file. When I did that, the custom tab is ordered at the bottom of the rest of the tabs.
So I was wondering, is a way to reorder it further up the list of tabs?
Specifically I would like to reorder my custom tab (My Favorites) above the "Account Details" tab.
image link
function add_myfavorites_endpoint() {
add_rewrite_endpoint( 'myfavorites', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'add_myfavorites_endpoint' );
function myfavorites_query_vars( $vars ) {
$vars[] = 'myfavorites';
return $vars;
}
add_filter( 'query_vars', 'myfavorites_query_vars', 0 );
function add_myfavorites_link_my_account( $items ) {
$items['myfavorites'] = 'My Favorites';
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'add_myfavorites_link_my_account' );
function myfavorites_content() {
echo '<h3>My Favorites</h3><p>';
echo do_shortcode( ' [my_content_shortcode] ' );
}
add_action( 'woocommerce_account_myfavorites_endpoint', 'myfavorites_content' );
// Rename, re-order my account menu items
function fwuk_reorder_my_account_menu() {
$neworder = array(
'dashboard' => __( 'Dashboard', 'woocommerce' ),
'orders' => __( 'Previous Orders', 'woocommerce' ),
'wishlist-link' => __( 'Wishlist', 'woocommerce' ),
'edit-address' => __( 'Addresses', 'woocommerce' ),
'edit-account' => __( 'Account Details', 'woocommerce' ),
'customer-logout' => __( 'Logout', 'woocommerce' ),
);
return $neworder;
}
add_filter ( 'woocommerce_account_menu_items', 'fwuk_reorder_my_account_menu' );

Email recipient based on a custom field value in WooCommerce

I've added a custom field to the checkout page on my Woocommerce store. The field is 'Restaurant Location'. Upon a customer placing an order, my goal is to use the 'Restaurant Location' field to determine which email to send the order confirmation to.
Here's how I defined the custom field.
/////// Hook custom field in ///////
add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields' );
function custom_checkout_fields( $fields ) {
$fields['order']['restaurant_location'] = array(
'label' => __('Food options', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => true,
'clear' => false,
'type' => 'select',
'options' => array(
'no' => __('New Orleans', 'woocommerce' ),
'br' => __('Baton Rouge', 'woocommerce' )
)
);
return $fields;
}
Here's my attempt at the email filter.
add_filter( 'woocommerce_email_recipient_new_order', 'gon_conditional_email_recipient', 10, 2 );
function gon_conditional_email_recipient( $recipient, $order ) {
$gon_order_data = $order->get_data();
$gon_restaurant_location = $gon_order_data['order']['restaurant_location'];
if ( $gon_restaurant_location == 'New Orleans' ) {
$recipient = 'test1#gmail.com';
return $recipient;
}
else if ( $gon_restaurant_location == 'Baton Rouge' ) {
$recipient = 'test2#gmail.com';
return $recipient;
}
return $recipient;
}
The email filter is working, i.e. I can get an email to go to either address, but I can't seem to pull in the '$gon_restaurant_location' variable properly. Any ideas?
Thanks,
pS
Your custom field value is not saved in database, so that's why is not working. Try this complete solution instead:
// Add the custom checkout field
add_filter( 'woocommerce_after_order_notes', 'restaurant_location_checkout_field' );
function restaurant_location_checkout_field( $checkout ) {
woocommerce_form_field( 'restaurant_location', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Food options', 'woocommerce'),
'required' => true,
'options' => array(
'' => __('Please select an option', 'woocommerce' ),
'New Orleans' => __('New Orleans', 'woocommerce' ),
'Baton Rouge' => __('Baton Rouge', 'woocommerce' )
)
), $checkout->get_value( 'restaurant_location' ));
}
// Process the checkout (checking)
add_action('woocommerce_checkout_process', 'restaurant_location_field_process');
function restaurant_location_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['restaurant_location'] )
wc_add_notice( __( 'Please select a food option .' ), 'error' );
}
// Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'restaurant_location_field_update_order_meta' );
function restaurant_location_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['restaurant_location'] ) ) {
update_post_meta( $order_id, '_restaurant_location', sanitize_text_field( $_POST['restaurant_location'] ) );
}
}
// Display field value on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Food options', 'woocommerce').':</strong> ' . get_post_meta( $order->get_id(), '_restaurant_location', true ) . '</p>';
}
// Conditional Email recipient filter based on restaurant location
add_filter( 'woocommerce_email_recipient_new_order', 'conditional_email_recipient', 10, 2 );
function conditional_email_recipient( $recipient, $order ) {
if( is_admin() ) return $recipient;
$location = get_post_meta( $order->get_id(), '_restaurant_location', true );
$recipient = $location == 'New Orleans' ? ',test1#example.com' : ',test1#example.com';
return $recipient;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on Woocommerce 3+ and works
Based on official developer documentation: Adding a custom special field

woocommerce shipping method with 3.0 api

Hi there, I'm trying to build a very simple plugin for woocommerce to show some information about delivery without taxes. The diference for the other methods is the extra HTML field.
I read a lot of the documentation but I think there is something miss on the configuration. The admin it seems working. But the method didn't appear on checkout screen. The code is below:
<?php
if ( ! defined( 'WPINC' ) ) {
die('security by preventing any direct access to your plugin file');
}
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
function shipping_delivery_info() {
if (!class_exists('shipping_delivery_info')) {
class shipping_delivery_info extends WC_Shipping_Method {
public function __construct( $instance_id = 0) {
$this->id = 'shipping_delivery_info';
$this->instance_id = absint( $instance_id );
$this->method_title = __('Shipping Delivery Info', 'shipping_delivery_info');
$this->method_description = __('A Woocommerce custom shipping method plugin, that shows ' .
'some shipping information to costumer, like free shipping but with HTML field.',
'shipping_delivery_info');
$this->supports = array(
'shipping-zones',
'instance-settings',
'instance-settings-modal',
);
$this->init();
}
/**
* Load the settings API
*/
function init() {
// Load the settings
$this->init_form_fields();
$this->init_settings();
$this->enabled = $this->get_option( 'enabled' );
$this->title = $this->get_option( 'title' );
$this->info = $this->get_option( 'info' );
add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
}
function init_form_fields() {
$this->instance_form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'shipping_delivery_info'),
'type' => 'checkbox',
'label' => __( 'Enable this shipping method', 'shipping_delivery_info'),
'default' => 'yes',
),
'title' => array(
'title' => __('Title', 'shipping_delivery_info'),
'type' => 'text',
'description' => __( 'The title to be displayed during checkout.', 'shipping_delivery_info' ),
'default' => __('Shipping Information', 'shipping_delivery_info'),
),
'info' => array(
'title' => __('Information', 'shipping_delivery_info'),
'type' => 'text',
'description' => __( 'Information about delivery and its taxes.', 'shipping_delivery_info' ),
'default' => __('Insert here some HTML.'),
),
);
}
}
}
}
add_action('woocommerce_shipping_init', 'shipping_delivery_info');
function add_shipping_delivery_info($methods)
{
$methods['shipping_delivery_info'] = 'shipping_delivery_info';
return $methods;
}
add_filter('woocommerce_shipping_methods', 'add_shipping_delivery_info');
function shipping_delivery_info_message($posted)
{
$packages = WC()->shipping->get_packages();
$chosen_methods = WC()->session->get('chosen_shipping_methods');
if (is_array($chosen_methods) && in_array('shipping_delivery_info', $chosen_methods)) {
foreach ($packages as $i => $package) {
if ($chosen_methods[$i] != "shipping_delivery_info") {
continue;
}
$shipping_delivery_info = new shipping_delivery_info();
$message = $shipping_delivery_info->settings['info'];
return $message;
/*$messageType = "info";
wc_add_notice($message, $messageType);*/
}
}
}
add_action('woocommerce_review_order_before_cart_contents', 'shipping_delivery_info_message', 10);
add_action('woocommerce_after_checkout_validation', 'shipping_delivery_info_message', 10);
}
You need to add the calculate_shipping function to your class
/**
* function calculate_shipping.
*
* #access public
* #param mixed $package
* #return void
*/
public function calculate_shipping( $package = array() ) {
$rate = array(
'id' => 'My Method id',
'label' => 'New method',
'cost' => 0,
'calc_tax' => 'per_item'
);
$this->add_rate( $rate );
}
}
Anyway, i think you don't need to add a new shipping method just to show some info at the cart, instead you can use these actions:
add_action( 'woocommerce_review_order_before_submit', 'add_tracking_notification', 12 );
add_action('woocommerce_proceed_to_checkout', 'add_tracking_notification');
function add_tracking_notification() {
echo '<h5 style="margin-bottom:10px">This is a custom message</h5>';
}

WordPress Plugin WooCommerce, Custom Payment Gateway Settings Not Saving

I'm working on a custom payment gateway for the WordPress plugin WooCommerce. I cannot seem to save the settings for the payment gateway. When I enter information into the fields and then click save, the page refreshes with all of the fields blank. What am I doing wrong?
Here is my code.
<?php
/**
* Plugin Name: Bitcoin WooCommerce Integration Made Easy
* Description: A Bitcoin processing plugin that integrates into WooCommerce made specifically for Bitcoin Publish.
* Version: 0.01
* Author: Cammy_the_block
*/
add_action( 'plugins_loaded', 'init_your_gateway_class' );
function init_your_gateway_class() {
class WC_Gateway_Your_Gateway extends WC_Payment_Gateway {
function __construct() {
$this->id = "Bitcoin WooCommerce Integration Gateway";
$this->method_title = "Bitcoin with BWCIME";
$this->method_description = "More later";
$this->init_form_fields();
$this->init_settings();
if ( version_compare( WOOCOMMERCE_VERSION, '2.0.0', '>=' ) ) {
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( &$this, 'process_admin_options' ) );
}
else {
add_action( 'woocommerce_update_options_payment_gateways', array( &$this, 'process_admin_options' ) );
}
}
function init_form_fields(){
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable Cheque Payment', 'woocommerce' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'Cheque Payment', 'woocommerce' ),
'desc_tip' => true,
),
'description' => array(
'title' => __( 'Customer Message', 'woocommerce' ),
'type' => 'textarea',
'default' => ''
)
);
}
}
function process_payment( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
$productArray = array();
$x = 0;
foreach( $order->get_items() as $item_id => $item ) {
$productArray[x] = $order->get_product_from_item( $item );
$x++;
}
// Mark as on-hold (we're awaiting the cheque)
$order->update_status('on-hold',
__( 'Awaiting cheque payment. there are ' + $productArray.length + 'items', 'woocommerce' )
);
// Remove cart
$woocommerce->cart->empty_cart();
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
}
}
function add_your_gateway_class ($methods ) {
$methods[] = 'WC_Gateway_Your_Gateway';
return $methods;
}
add_filter( 'woocommerce_payment_gateways', 'add_your_gateway_class' );
?>
EDIT:
The add filter code runs add_your_gateway_class, which in turn causes it to run WC_Gateway_Your_Gateway.
you have to call them on the constructor after the init_settings();
$this->init_settings();
// Define user set variables
$this->access_key = $this->get_option( 'access_key' );
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
edit:
you also need another action/hook, just at the end of the constructor, no need to create that new function that you came up with:
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
edit 2:
sorry you already have it, my bad :p, dont realy know what happened there, glad its solved
The actual problem is your this->id="example_gateway": id is always case sensitive and it should be without space and lowercase.
Example:
function __construct() {
$this->id = "bitcoin_woocommerce_integration_gateway";
$this->method_title =( "Bitcoin with BWCIME", 'bitcoin_woocommerce_integration_gateway' );
$this->title = __( "Bitcoin with BWCIME", 'bitcoin_woocommerce_integration_gateway' );
$this->method_description = "More later";
//further as same as your code.....
}
I'm not completely sure how I fixed it, but I believe it had to do with adding the function admin_options().
public function admin_options() {
?>
<h3><?php _e('Bitcoin Payment', 'woothemes'); ?></h3>
<p><?php _e('Message!.', 'woothemes'); ?></p>
<table class="form-table">
<?php
// Generate the HTML For the settings form.
$this->generate_settings_html();
?>
</table>
<?php
}
EDIT: I'm not sure what causes it but you have to clear the plugin's settings by interfacing with the database. The easy way to do this is to change the ID of the plugin. I'm not actually sure where the settings are stored in the database.

Categories