簡體   English   中英

從 Woocommerce 3 中的隱藏輸入字段自定義價格設置購物車項目價格

[英]Set cart item price from a hidden input field custom price in Woocommerce 3

在 Woocommerce 中,我使用jQuery計算單個產品頁面的自定義價格,現在需要將此值傳遞給購物車。

期望的行為是將從隱藏字段檢索到的新價格傳遞給購物車項目價格。

這是我的實際代碼:

// Hidden input field in single product page
add_action( 'woocommerce_before_add_to_cart_button', 'custom_hidden_product_field', 11, 0 );
function custom_hidden_product_field() {
    echo '<input type="hidden" id="hidden_field" name="custom_price" class="custom_price" value="">';
}


// The code to pass this data to the cart:
add_action( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {

    if( ! empty( $_REQUEST['custom_price'] ) ) {
        // Set the custom data in the cart item
        $cart_item_data['custom_data']['custom_price'] = $_REQUEST['custom_price'];
        $data = array( 'custom_price' => $_REQUEST['custom_price'] );
        
        // below statement make sure every add to cart action as unique line item
        $cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
        WC()->session->set( 'custom_data', $data );
    }
    return $cart_item_data;
}

並檢查$data$cart_item_data以查看它們都返回在頁面上計算的custom_price數據。

但是,我去查看購物車,訂單項的值仍然是 0。

我設置了一個等於WC()->session->set( 'custom_data', $data );var 然后var_dump對其進行檢查,但這會返回NULL ,這可能就是它返回的內容,我不完全確定,因為我從未使用過它。

我還應該補充一點,我將產品后端中的regular_price設置為 0。當我刪除它(並將其留空)時,我得到了錯誤:

警告:第 85 行 C:\xampp\htdocs\my-transfer-source\wp-content\plugins\woocommerce\includes\class-wc-discounts.php 中遇到的非數字值

我想知道我是否在這里遺漏了什么,是否有人可以對此有所了解? 謝謝

2021 年更新- 處理迷你購物車中的自定義價格項目

首先出於測試目的,我們在隱藏的輸入字段中添加一個價格,因為您沒有提供計算價格的代碼:

// Add a hidden input field (With a value of 20 for testing purpose)
add_action( 'woocommerce_before_add_to_cart_button', 'custom_hidden_product_field', 11 );
function custom_hidden_product_field() {
    echo '<input type="hidden" id="hidden_field" name="custom_price" class="custom_price" value="20">'; // Price is 20 for testing
}

然后您將使用以下內容更改購物車商品價格 WC_Session

// Save custom calculated price as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {

    if( isset( $_POST['custom_price'] ) && ! empty( $_POST['custom_price'] )  ) {
        // Set the custom data in the cart item
        $cart_item_data['custom_price'] = (float) sanitize_text_field( $_POST['custom_price'] );

        // Make each item as a unique separated cart item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}

// For mini cart
add_action( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 2 );
function filter_cart_item_price( $price, $cart_item ) {
    if ( isset($cart_item['custom_price']) ) {
        $args = array( 'price' => floatval( $cart_item['custom_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;
}

// Updating cart item price
add_action( 'woocommerce_before_calculate_totals', 'change_cart_item_price', 30, 1 );
function change_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 ) {
        // Set the new price
        if( isset($cart_item['custom_price']) ){
            $cart_item['data']->set_price($cart_item['custom_price']);
        }
    }
}

代碼在您的活動子主題(或活動主題)的functions.php 文件中。 測試和工作。

暫無
暫無

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

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