Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.4 kB
4
Indexable
Never
add_action( 'woocommerce_cart_totals_after_order_total', 'bbloomer_show_total_discount_cart_checkout', 9999 );
add_action( 'woocommerce_review_order_after_order_total', 'bbloomer_show_total_discount_cart_checkout', 9999 );
  
function bbloomer_show_total_discount_cart_checkout() {   
   $discount_total = 0;  
   foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {         
      $product = $values['data'];
     $regular_price = get_post_meta($product->get_id(),'_regular_price', true);
     $regular_price = b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $regular_price ) );


		// get user's group
		$user_id = get_current_user_id();
 	$user_id = b2bking()->get_top_parent_account($user_id);

		$currentusergroupidnr = apply_filters('b2bking_b2b_group_for_pricing', b2bking()->get_user_group($user_id), $user_id);

		$is_b2b_user = get_user_meta( $user_id, 'b2bking_b2buser', true );
     // GET ORIGINAL PRODUCT PRICE
     if ( $product->is_on_sale() ) {
     	$original_user_price = get_post_meta($product->get_id(),'_sale_price',true);
        	
     	if ($is_b2b_user === 'yes'){
     		// Search if there is a specific price set for the user's group
     		$b2b_price = b2bking()->tofloat(get_post_meta($product->get_id(), 'b2bking_sale_product_price_group_'.$currentusergroupidnr, true ));
     							
     		if (!empty($b2b_price)){
     			$original_user_price = $b2b_price;
     		}
     	}

     } else {
     	$original_user_price = get_post_meta($product->get_id(),'_regular_price',true);

     	if ($is_b2b_user === 'yes'){
     		// Search if there is a specific price set for the user's group
     		$b2b_price = b2bking()->tofloat(get_post_meta($product->get_id(), 'b2bking_regular_product_price_group_'.$currentusergroupidnr, true ));
     							
     		if (!empty($b2b_price)){
     			$original_user_price = $b2b_price;
     		}
     	}
     }

     // adjust price for tax
     $sale_price = b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $original_user_price ) ); // get sale price


     $discount = ( (float)$regular_price - (float)$sale_price ) * (int)$values['quantity'];
     $discount_total += $discount;
  }
           
   if ( $discount_total > 0 ) {
      echo '<tr><th>You Saved</th><td data-title="You Saved">' . wc_price( $discount_total + WC()->cart->get_discount_total() ) .'</td></tr>';
   }
}