![](/img/trans.png)
[英]Custom cart item price calculation based on dimentions custom fields in Woocommerce
[英]Custom cart item price based on user input in Woocommerce
在我們的Woocommerce商店,我們有任何產品的最低價格。 在每個產品內頁中都有兩個字段,客戶可以在其中鍵入產品的寬度,高度。 然后他們可以將此產品添加到購物車,然后根據給定的寬度和高度更改價格。
例如,如果產品的最低價格是50 。 並且客戶添加寬度= 2,高度= 3 ,那么該產品的價格將達到50 * 2 * 3 = 300
所以為了安排這個,我們將這個代碼添加到function.php
add_filter( 'woocommerce_add_cart_item', 'add_custom_cart_item_data', 10, 2 );
function add_custom_cart_item_data( $cart_item_data, $cart_item_key ) {
$result=$_POST['width']*$_POST['height']*$cart_item_data['data']->price;
$cart_item_data['new-price'] =$result;
$cart_item_data['data']->set_price($result);
return $cart_item_data;
}
所以這是正常的。
但問題是,一旦它到了結賬頁面然后產品價格顯示為50,但實際上是300。
所以為了克服這一點,我使用以下代碼
add_filter( 'woocommerce_cart_item_subtotal', 'update_with_custom_details', 10, 3 );
function update_with_custom_details( $subtotal, $cart_item, $cart_item_key ) {
$subtotal =" £".$cart_item['line_total'];
return $subtotal;
}
現在它顯示300英鎊,但我知道這個代碼是錯誤的。
代碼是錯誤的,因為當客戶為此產品應用50%的優惠券代碼時,折扣將變為25,因為它基於50*.5=25;
但實際上產品新價格是300,所以折扣應該是300*5=150;
那我怎么能在購物車頁面,結賬頁面,迷你購物車等更新產品價格?
請幫忙 。
更新2:正確的方法需要分兩步完成:
woocommerce_before_calculate_totals
鈎子中設置了該計算價格 代碼:
// Save custom field value in cart item as custom data
add_filter( 'woocommerce_add_cart_item_data', 'calculate_custom_cart_item_prices', 30, 3 );
function calculate_custom_cart_item_prices( $cart_item_data, $product_id, $variation_id ) {
if ( isset($_POST['width']) && isset($_POST['height']) ) {
// Get the correct Id to be used (compatible with product variations)
$the_id = $variation_id > 0 ? $variation_id : $product_id;
$product = wc_get_product( $the_id ); // Get the WC_Product object
$product_price = (float) $product->get_price(); // Get the product price
// Get the posted data
$width = (float) sanitize_text_field( $_POST['width'] );
$height = (float) sanitize_text_field( $_POST['height'] );
$new_price = $width * $height * $product_price; // Calculated price
$cart_item_data['calculated-price'] = $new_price; // Save this price as custom data
}
return $cart_item_data;
}
// Set custom calculated price in cart item price
add_action( 'woocommerce_before_calculate_totals', 'set_calculated_cart_item_price', 20, 1 );
function set_calculated_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ){
if( ! empty( $cart_item['calculated-price'] ) ){
// Set the calculated item price (if there is one)
$cart_item['data']->set_price( $cart_item['calculated-price'] );
}
}
}
代碼位於活動子主題(或主題)的function.php文件中。 經過測試和工作。
出於測試目的 ,我使用以下代碼在單個產品頁面上輸出2個自定義字段:
// The hidden product custom field
add_action( 'woocommerce_before_add_to_cart_button', 'add_gift_wrap_field' );
function add_gift_wrap_field() {
global $product;
// The fake calculated price
?>
<div>
<label class="product-custom-text-label" for="servicetime"><?php _e( 'Custom text:', 'woocommerce'); ?><br>
<input type="text" id="user-width" name="width" value=""><br>
<input type="text" id="user-height" name="height" value="">
</label>
</div><br>
<?php
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.