我正在使用带有 Dokan 插件的 WooCommerce。我需要将字符串转换为数字,以便可以将其用于计算(这比我做的要容易)。
我需要使用行小计而不是产品价格,因为产品价格正在拉回最低的可变产品价格,而不是他们选择的选项。
我试过ltrim()
and substring()
,数字是正确的,但它返回的格式是它,我不知道如何去掉货币符号 ($) 并将其设为浮点数。
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 );
}