Empty order after guest register
-
Hi, for discount by the role I use the code. All fine, but sometime, I get empty order…. I setup logger and looks like situation next: guest add to cart some products, go to the checkout and see that he can get discount after first order but need to be logined….. He register on shop, go to the cart and cart empty. And I receive empty order – without email, name and product…..
my code:
//DISCOUNT
add_filter('woocommerce_add_cart_item_data', 'add_items_default_price_as_custom_data', 20, 3 );
function add_items_default_price_as_custom_data( $cart_item_data, $product_id, $variation_id ){
if ( !is_user_logged_in() ) { return;}
$product_id = $variation_id > 0 ? $variation_id : $product_id;
//get discount from role
$user = wp_get_current_user();
$user_role = $user->roles[0];
$page = jet_engine()->options_pages->registered_pages['shop-opt'];
// give a discount depending on the role
switch ($user_role) {
case 'customer':
$discount_percentage = 0;
break;
case 'discount1':
$discount_percentage = $page->get( 'diskont-riven-1' );
break;
case 'discount2':
$discount_percentage = $page->get( 'diskont-riven-2' );
break;
case 'discount3':
$discount_percentage = $page->get( 'diskont-riven-3' );
break;
}
// The WC_Product Object
$product = wc_get_product($product_id);
$price = (float) $product->get_price();
// Set the Product default base price as custom cart item data
$cart_item_data['base_price'] = $price;
// Only for non on sale products
if( ! $product->is_on_sale() ){
// Set the Product discounted price as custom cart item data
$cart_item_data['new_price'] = $price * (100 - $discount_percentage) / 100;
// Set the percentage as custom cart item data
$cart_item_data['percentage'] = $discount_percentage;
}
if( $product->is_on_sale() ){
// Set the Product discounted price as custom cart item data
$cart_item_data['new_price'] = $price;
}
return $cart_item_data;
}
// Display the product name with the discount percentage
add_filter( 'woocommerce_cart_item_name', 'append_percetage_to_item_name', 20, 3 );
function append_percetage_to_item_name( $product_name, $cart_item, $cart_item_key ){
if ( !is_user_logged_in() ) { return;}
if( isset($cart_item['percentage']) && isset($cart_item['base_price']) ) {
if( $cart_item['data']->get_price() != $cart_item['base_price'] )
$product_name .= ' <em>(' . $cart_item['percentage'] . '% discount)</em>';
}
return $product_name;
}
add_action( 'woocommerce_before_calculate_totals', 'custom_discounted_cart_item_price', 20, 1 );
function custom_discounted_cart_item_price( $cart ) {
if ( !is_user_logged_in() ) { return;}
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
## ----- YOUR SETTING ----- ##
$targeted_qty = 2; // Targeted quantity
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Set cart item discounted price
$cart_item['data']->set_price($cart_item['new_price']);
}
}
// CHECK/ADD ROLE after buuing
add_action( 'woocommerce_order_status_completed', 'order_completed', 1);
function order_completed($order_id) {
$order = wc_get_order( $order_id );
$user_id = $order->get_customer_id();
if ( $user_id == ''){return ;}
$user = new WP_User( $user_id);
$user_role = $user->roles[0];
switch ($user_role) {
case 'customer':
$user->set_role( 'discount1' );
break;
case 'discount1':
$user->set_role( 'discount2' );
break;
case 'discount2':
$user->set_role( 'discount3' );
break;
}
}
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
The topic ‘Empty order after guest register’ is closed to new replies.