2

我正在使用带有 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 );
 
}

4

2 回答 2

0

希望这会有所帮助, $price 仅获得数字值。

$amount = floatval( preg_replace( '#[^\d.]#', '', $price ) );
于 2020-07-26T13:20:22.837 回答
0

您没有使用正确的方法来获取显示的原始产品价格(浮点数)。现在在您提供的代码中,$amt_raised_for_cause没有定义...</p>

请尝试以下重新访问的代码:

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 );
}

测试和工作

于 2020-07-26T16:16:10.987 回答