我正在使用 WooCommerce 和 Dokan 插件。我需要将字符串转换为数字,以便我可以使用它进行计算(这必须比我做的更容易)。
我需要使用行小计而不是产品价格,因为产品价格拉回最低的可变产品价格而不是他们选择的选项。
我已经尝试过
ltrim()
和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 );
}
希望这会有所帮助,$price 只能得到数值。
$amount = floatval( preg_replace( '#[^\d.]#', '', $price ) );
您没有使用正确的方法来获取显示的原始产品价格(浮动数字)。现在在您提供的代码中,未定义
$amt_raised_for_cause
...
请尝试以下重新访问的代码:
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 );
}
已测试且有效