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.
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:
Tested and works