Untitled
webwizards
plain_text
18 days ago
4.6 kB
7
Indexable
// Configuration: Map group names to billing profile emails
function get_group_billing_profiles() {
return array(
'B2B GBP' => 'testagent@test.comagent',
'ABC Company' => 'billing@abccompany.com',
'XYZ Corp' => 'accounts@xyzcorp.com',
// Add more group => email associations here
);
}
// Get the billing profile user for the current user's group
function get_billing_profile_for_user($user_id) {
$group_profiles = get_group_billing_profiles();
// Get user's group
$group_id = get_user_meta($user_id, 'b2bking_customergroup', true);
if (empty($group_id)) {
return false;
}
$group_name = get_the_title($group_id);
// Check if this group has a billing profile
if (isset($group_profiles[$group_name])) {
$billing_email = $group_profiles[$group_name];
$billing_user = get_user_by('email', $billing_email);
if ($billing_user) {
return $billing_user;
}
}
return false;
}
// Prefill checkout fields with billing profile data
add_filter('woocommerce_checkout_get_value', 'prefill_checkout_with_billing_profile', 10, 2);
function prefill_checkout_with_billing_profile($value, $input) {
if (is_admin()) {
return $value;
}
$user_id = get_current_user_id();
if (!$user_id) {
return $value;
}
$billing_user = get_billing_profile_for_user($user_id);
if (!$billing_user) {
return $value;
}
// Map of checkout fields to user meta keys
$billing_fields = array(
'billing_first_name' => 'billing_first_name',
'billing_last_name' => 'billing_last_name',
'billing_company' => 'billing_company',
'billing_address_1' => 'billing_address_1',
'billing_address_2' => 'billing_address_2',
'billing_city' => 'billing_city',
'billing_state' => 'billing_state',
'billing_postcode' => 'billing_postcode',
'billing_country' => 'billing_country',
'billing_email' => 'billing_email',
'billing_phone' => 'billing_phone',
);
if (isset($billing_fields[$input])) {
$meta_key = $billing_fields[$input];
$billing_value = get_user_meta($billing_user->ID, $meta_key, true);
if (!empty($billing_value)) {
return $billing_value;
}
}
return $value;
}
// Make billing fields readonly for users with billing profiles
add_filter('woocommerce_checkout_fields', 'lock_billing_fields_for_group_members');
function lock_billing_fields_for_group_members($fields) {
$user_id = get_current_user_id();
if (!$user_id) {
return $fields;
}
$billing_user = get_billing_profile_for_user($user_id);
if (!$billing_user) {
return $fields;
}
// Lock all billing fields
$billing_field_keys = array(
'billing_first_name',
'billing_last_name',
'billing_company',
'billing_address_1',
'billing_address_2',
'billing_city',
'billing_state',
'billing_postcode',
'billing_country',
'billing_email',
'billing_phone',
);
foreach ($billing_field_keys as $field_key) {
if (isset($fields['billing'][$field_key])) {
$fields['billing'][$field_key]['custom_attributes'] = array(
'readonly' => 'readonly',
'disabled' => 'disabled',
);
$fields['billing'][$field_key]['class'][] = 'locked-field';
}
}
return $fields;
}
// Re-enable disabled fields before form submission so values are processed
add_action('wp_footer', 'enable_locked_fields_on_submit');
function enable_locked_fields_on_submit() {
if (is_checkout() && !is_wc_endpoint_url()) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('form.checkout').on('checkout_place_order', function() {
$('.locked-field input, .locked-field select').removeAttr('disabled');
});
});
</script>
<?php
}
}
// Add CSS to visually indicate locked fields
add_action('wp_head', 'locked_fields_styling');
function locked_fields_styling() {
if (is_checkout()) {
?>
<style type="text/css">
.locked-field input[readonly],
.locked-field select[readonly] {
background-color: #f5f5f5 !important;
cursor: not-allowed !important;
opacity: 0.5;
}
</style>
<?php
}
}
Editor is loading...
Leave a Comment