簡體   English   中英

在 Woocommerce mini-cart / Cart 中設置自定義計算的商品價格

[英]Set a custom calculated item price in Woocommerce mini-cart / Cart

目前我有一些基於不同情況的產品價格的自定義計算。 當客戶將產品添加到購物cart_item_data['my-price'] ,自定義價格在會話數據中設置, cart_item_data['my-price']並且我使用add_filter( 'woocommerce_add_cart_item')函數實現,現在一切似乎都在工作。

現在查看購物車頁面中的價格,結賬頁面與我的cart_item_data['my-price'].是正確的cart_item_data['my-price'].

但我面臨的唯一問題是菜單中出現的 woocommerce 迷你購物車中的價格沒有更新,我該如何更改?

在此處輸入圖片說明

當我谷歌我看到一個過濾器

add_filter('woocommerce_cart_item_price');

但我不明白如何使用它,我執行以下操作

    add_filter('woocommerce_cart_item_price','modify_cart_product_price',10,3);

function modify_cart_product_price( $price, $cart_item, $cart_item_key){
  if($cart_item['my-price']!==0){
      $price =$cart_item['my-price'];
    }
    return $price;
    //exit;
}

這里individual price is getting correct ,但total price is wrong

更新(2021 年 10 月)

為了成功測試(並且我不知道您如何進行計算),我在產品添加到購物車表單中添加了一個自定義隱藏字段,如下所示:

// 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
    ?>
        <input type="hidden" id="my-price" name="my-price" value="115">
    <?php
}

將產品添加到購物車時,也會提交(發布)此my-price自定義字段。 要在購物車對象中設置此值,我使用以下函數:

add_filter( 'woocommerce_add_cart_item', 'custom_cart_item_prices', 20, 2 );
function custom_cart_item_prices( $cart_item_data, $cart_item_key ) {
    // Get and set your price calculation
    if( isset( $_POST['my-price'] ) ){
        $cart_item_data['my-price'] = $_POST['my-price'];

        // Every add to cart action is set as a unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }

    return $cart_item_data;
}

現在將新計算的價格my-price應用(設置)到購物車項目,我使用最后一個函數:

// For mini cart *(cart item displayed price)*
add_action( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 2 );
function filter_cart_item_price( $price, $cart_item ) {
    if ( ! is_checkout() && isset($cart_item['my-price']) ) {
        $args = array( 'price' => floatval( $cart_item['my-price'] ) );

        if ( WC()->cart->display_prices_including_tax() ) {
            $product_price = wc_get_price_including_tax( $cart_item['data'], $args );
        } else {
            $product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
        }
        return wc_price( $product_price );
    }
    return $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( isset( $cart_item['my-price'] ) && ! empty( $cart_item['my-price'] ) || $cart_item['my-price'] != 0 ){
            // Set the calculated item price (if there is one)
            $cart_item['data']->set_price( $cart_item['my-price'] );
        }
    }
}

所有代碼都在活動子主題(或活動主題)的 function.php 文件中。

測試和工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM