It works perfectly fine. However, there is only one issue: if the customer registers on the checkout page, the product is not added to the cart. That is why I am looking for a plugin (yours, of course) that can handle this situation.
Your feedback is appreciated, and with kind regards,
First of all I'm afraid B2BKing doesn't have any features for this,
But I looked into that and over your code,
For logged out users I guess the problem is that we can't really check their account history as they haven't logged in yet - and it's very difficult programmatically to check right in that split second between checkout and registration.
I feel like the most realistic solution might be to just modify that code to apply it generally for all logged out users.
Or alternatively maybe just add some kind of column to the Orders page backend that lets you know when this is the customer's first order, so you can add the free item via backend? Since the item is would be added to cart during checkout it's not like the customer can see it in cart anyway?
Hello,
Can a free product be added when the user completes the registration form (either the B2B one or the default one)?
In case yes: how do I do that? I have searched the documentation but did not find it there.
Your answer is appreciated and with kind regards,
TheStingPilot
Another option is a free product on first order.
Hello Stefan,
I have the following code:
function add_free_gift_for_first_order() {
// Define the free product ID
$my_current_lang = apply_filters( 'wpml_current_language', NULL );
if ($my_current_lang == 'de') {$free_product_id = 15262;}
elseif($my_current_lang == 'el') {$free_product_id = 15268;}
elseif($my_current_lang == 'en') {$free_product_id = 15263;}
elseif($my_current_lang == 'es') {$free_product_id = 15266;}
elseif($my_current_lang == 'fr') {$free_product_id = 15264;}
elseif($my_current_lang == 'id') {$free_product_id = 15265;}
elseif($my_current_lang == 'et') {$free_product_id = 15267;}
else {$free_product_id = 15261;}
// Get the current user ID
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
// Check if the user has made previous orders
$customer_orders = wc_get_orders(array(
'customer_id' => $user_id,
'limit' => -1,
'status' => array('completed', 'processing', 'on-hold')
));
// If no previous orders, and user is logged in, add the free product to the cart
if (empty($customer_orders) && is_user_logged_in()) {
$cart = WC()->cart;
$free_product_exists = false;
// Check if the free product is already in the cart
foreach ($cart->get_cart() as $cart_item) {
if ($cart_item['product_id'] == $free_product_id) {
$free_product_exists = true;
break;
}
}
// Add the free product to the cart if it's not already added
if (!$free_product_exists) {
$cart->add_to_cart($free_product_id, 1);
}
}
}
// Hook the function to WooCommerce before calculating totals
add_action('woocommerce_before_calculate_totals', 'add_free_gift_for_first_order');
add_filter('woocommerce_quantity_input_args', 'ts_set_limit_free_product_jasmineregular', 10, 2);
function ts_set_limit_free_product_jasmineregular($args, $product) {
$my_current_lang = apply_filters( 'wpml_current_language', NULL );
if ($my_current_lang == 'de') {$free_product_id = 15262;}
elseif($my_current_lang == 'el') {$free_product_id = 15268;}
elseif($my_current_lang == 'en') {$free_product_id = 15263;}
elseif($my_current_lang == 'es') {$free_product_id = 15266;}
elseif($my_current_lang == 'fr') {$free_product_id = 15264;}
elseif($my_current_lang == 'id') {$free_product_id = 15265;}
elseif($my_current_lang == 'et') {$free_product_id = 15267;}
else {$free_product_id = 15261;}
if (!is_cart()) {
if ($product->get_id() === $free_product_id) {
$args['max_value'] = 1; // Maximum quantity (default = -1)
}
} else {
if ($product->get_id() === $free_product_id) {
// Cart's 'min_value' is 0
$args['max_value'] = 1;
}
}
return $args;
}
Source: https://webappick.com/woocommerce-add-free-product-to-cart/
It works perfectly fine. However, there is only one issue: if the customer registers on the checkout page, the product is not added to the cart. That is why I am looking for a plugin (yours, of course) that can handle this situation.
Your feedback is appreciated, and with kind regards,
TheStingPIlot
Hello Willem-Jan,
First of all I'm afraid B2BKing doesn't have any features for this,
But I looked into that and over your code,
For logged out users I guess the problem is that we can't really check their account history as they haven't logged in yet - and it's very difficult programmatically to check right in that split second between checkout and registration.
I feel like the most realistic solution might be to just modify that code to apply it generally for all logged out users.
Or alternatively maybe just add some kind of column to the Orders page backend that lets you know when this is the customer's first order, so you can add the free item via backend? Since the item is would be added to cart during checkout it's not like the customer can see it in cart anyway?
Kind regards,
Stefan