Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One Click Checkout #2173

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,67 @@
* Note: Do not add any custom code here. Please use a custom plugin so that your customizations aren't lost during updates.
* https://github.com/woocommerce/theme-customisations
*/

// Add a new setting field for One-Click Checkout in WooCommerce settings
add_filter('woocommerce_general_settings', 'add_one_click_checkout_setting');
function add_one_click_checkout_setting($settings) {
$settings[] = array(
'name' => __('One-Click Checkout', 'your-text-domain'),
'type' => 'title',
'desc' => __('Enable One-Click Checkout for returning customers.', 'your-text-domain'),
'id' => 'one_click_checkout_title'
);

$settings[] = array(
'name' => __('Enable One-Click Checkout', 'your-text-domain'),
'id' => 'enable_one_click_checkout',
'type' => 'checkbox',
'desc' => __('Enable the One-Click Checkout feature.', 'your-text-domain'),
'default' => 'no',
);

$settings[] = array('type' => 'sectionend', 'id' => 'one_click_checkout_title');

return $settings;
}

// Redirect users to the checkout page if One-Click Checkout is enabled and they are logged in
add_filter('woocommerce_add_to_cart_redirect', 'one_click_checkout_redirect');
function one_click_checkout_redirect($url) {
// Check if One-Click Checkout is enabled
if ('yes' === get_option('enable_one_click_checkout') && is_user_logged_in()) {
// Skip cart page and go directly to the checkout page
$checkout_url = wc_get_checkout_url();
return $checkout_url;
}

return $url;
}

// Pre-fill checkout form for returning customers if One-Click Checkout is enabled
add_filter('woocommerce_checkout_fields', 'prefill_checkout_fields_for_returning_customers');
function prefill_checkout_fields_for_returning_customers($fields) {
if ('yes' === get_option('enable_one_click_checkout') && is_user_logged_in()) {
$current_user = wp_get_current_user();

// Pre-fill billing fields with user data
$fields['billing']['billing_first_name']['default'] = $current_user->billing_first_name;
$fields['billing']['billing_last_name']['default'] = $current_user->billing_last_name;
$fields['billing']['billing_email']['default'] = $current_user->user_email;
$fields['billing']['billing_phone']['default'] = $current_user->billing_phone;
$fields['billing']['billing_address_1']['default'] = $current_user->billing_address_1;
$fields['billing']['billing_city']['default'] = $current_user->billing_city;
$fields['billing']['billing_postcode']['default'] = $current_user->billing_postcode;
$fields['billing']['billing_country']['default'] = $current_user->billing_country;

// Pre-fill shipping fields if needed (optional)
// $fields['shipping']['shipping_first_name']['default'] = $current_user->shipping_first_name;
// $fields['shipping']['shipping_last_name']['default'] = $current_user->shipping_last_name;
// $fields['shipping']['shipping_address_1']['default'] = $current_user->shipping_address_1;
// $fields['shipping']['shipping_city']['default'] = $current_user->shipping_city;
// $fields['shipping']['shipping_postcode']['default'] = $current_user->shipping_postcode;
// $fields['shipping']['shipping_country']['default'] = $current_user->shipping_country;
}

return $fields;
}