Get the product raw price for display (float) on WooCommerce cart page

Viewed 2013

I am using WooCommerce with Dokan plugin. I need to convert the string to a number so that I can use it for calculations (this has to be easier than I am making it).

I need to use the line subtotal rather than the product price because the product price is pulling back the lowest variable product price instead of the the option they selected.

I have tried ltrim() and substring(), the number is correct but it's returning it formatted and I can't figure out how to get rid of the currency symbol ($) and make it as a float number.


foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
   $product = $cart_item['data'];
   $product_id = $cart_item['product_id'];
    /*?>Product ID:  <?php  echo $product_id,"<br>"; */
   $quantity=1;
   
    $quantity = $cart_item['quantity'];
    ?>Quantity:  <?php  echo $quantity,"<br>"; 
    
/*  $price = WC()->cart->get_product_price( $product );
    ?>Total Price 1:  <?php  echo $price,"<br>"; */
    
    //$price = get_post_meta($cart_item['product_id'] , '_price', true);
    
    $price = WC()->cart->get_product_price( $product );
    ?>Price:  <?php  echo $price,"<br>"; 
    
    $res = ltrim($price,12);

    ?>Price:  <?php  echo $res,"<br>"; 
    
    
    
    $item_total = $price * $quantity;    
    ?>Item Total:  <?php  echo $item_total,"<br>";
    

    
    $vendor_id = get_post($product_id); 
    /*?>Vendor ID: <?php echo $vendor_id->post_author,"<br>"; */
    $admin_commission  = get_user_meta( $vendor_id->post_author, 'dokan_admin_percentage', true );
   ?>Admin Commission: <?php echo $admin_commission, "<br>";
    $commission_amount = number_format(($price) * ( (get_user_meta( $vendor_id->post_author, 'dokan_admin_percentage', true ))/100),2);
    $commission_total = ($commission_amount/2);
    ?>Commission_Total: <?php echo $commission_total,"<br><br>";
    $amt_raised_for_cause= $amt_raised_for_cause + $commission_total ;
   // Anything related to $product, check $product tutorial
   //$meta = wc_get_formatted_cart_item_data( $cart_item );
 
}

2 Answers

Hope this will helpful, $price get only number value.

$amount = floatval( preg_replace( '#[^\d.]#', '', $price ) );

You are not using correct way to get the raw product price for display (float number). Now in your provided code, $amt_raised_for_cause is not defined…

Try the following revisited code instead:

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $product      = $cart_item['data']; // The WC_Product Object
    $product_id   = $cart_item['product_id']; // The product ID (Or the variable product ID)
    $variation_id = $cart_item['variation_id']; // The variation product ID

    /*?>
    Product ID:  <?php  echo $product_id,"<br>";
    Variation ID:  <?php  echo $product_id,"<br>";
    */

    $quantity = $cart_item['quantity']; // The cart item quantity
    ?>Quantity:  <?php  echo $quantity,"<br>";

    // Display the formatted price html (for cart item)
    $price_html = WC()->cart->get_product_price( $product );
    ?>Price:  <?php  echo $price_html,"<br>";

    // Get the raw product price for display (including taxes if it's enabled)
    if ( WC()->cart->display_prices_including_tax() ) {
        $price = wc_get_price_including_tax( $product );
    } else {
        $price = wc_get_price_excluding_tax( $product );
    }

    // Display the raw product price
    /* ?>Price (raw):  <?php  echo $price,"<br>"; */

    $item_total   = $price * $quantity;
    ?>Item Total:  <?php  echo $item_total,"<br>";

    $vendor_id    = get_post($product_id); // The dokan vendor ID
    /*?>Vendor ID: <?php echo $vendor_id->post_author,"<br>"; */

    $admin_commission  = get_user_meta( $vendor_id->post_author, 'dokan_admin_percentage', true );
   ?>Admin Commission: <?php echo $admin_commission, "<br>";

    $commission_amount = number_format( $price * ( $admin_commission / 100 ), 2 );
    $commission_total  = $commission_amount / 2;
    
    ?>Commission_Total: <?php echo $commission_total,"<br><br>";
    $amt_raised_for_cause = $amt_raised_for_cause + $commission_total; // <=== $amt_raised_for_cause NOT DEFINED
    
   // Anything related to $product, check $product tutorial
   //$meta = wc_get_formatted_cart_item_data( $cart_item );
}

Tested and works

Related