(Theme: Woodmart)
I have one problem, I would like to have a custom meta field in the database of registered users, where I can set his debt and not let him make an order while he has an outstanding debt to the company. The field in the database where the debt will be saved will be pulled from the accounting program and placed in those fields based on the user ID.
I tried to do something but it doesn't work, I just added a meta table, created it manually through SQL, I don't know if I need any other code to make that meta field work, the next code I tried was:
<?php add_action( 'woocommerce_checkout_process', 'cssigniter_prevent_checkout_if_user_have_debt' );
function cssigniter_prevent_checkout_if_user_have_debt() {
// We check if there is a customer in the database
$customer = wp_get_current_user();
$debt = get_user_meta( $current_user->ID, 'wp_woocommerce_user_debt' , true );
if ( ! empty( $customer ) ) {
$args = array(
'customer_id' => $customer->ID,
'wp_woocommerce_user_debt' => $current_user->ID, 'wp_woocommerce_user_debt',
);
// Get the data from the column "wp_woocommerce_user_debt"
$pending_debt = get_user_meta( $user_id, 'wp_woocommerce_user_debt', true );
// It is checked whether the customer's debt is greater than zero, if so, data is pulled from the table "wp_woocommerce_user_debt"
if ( count( $pending_debts ) > 0 ) {
foreach ( $pending_debts as $pending_debt ) {
array_push( $pending_debt, '<a href="' . $pending_debt->get_meta_data() . '" </a>' );
}
// Printing a message to the customer
$message = sprintf(
__( 'Kupovina nije uspjela. Zamolili bi smo Vas da izmirite dug u iznosu od %2$s KM prema kompaniji ... kako bi ste mogli nastaviti kupovati.', 'your-text-domain' )
);
wc_add_notice( $message, 'error' );
}
}
} ?>
Replace your function with this
add_action( 'woocommerce_checkout_process', 'cssigniter_prevent_checkout_if_user_have_debt' );
function cssigniter_prevent_checkout_if_user_have_debt() {
//Check if current visitor is logged in
if(!is_user_logged_in()) return;
// Get User ID
$user_id = get_current_user_id();
$debt = get_user_meta( $user_id, 'wp_woocommerce_user_debt' , true );
// Check if user have debt
if(empty($debt)) return;
//Print msg if user have debt
wc_add_notice( sprintf( 'Kupovina nije uspjela. Zamolili bi smo Vas da izmirite dug u iznosu od %2$s KM prema kompaniji %s kako bi ste mogli nastaviti kupovati.','your-text-domain',$debt),'error' );
}