【问题标题】:Set cart item price from a hidden input field custom price in Woocommerce 3从 Woocommerce 3 中的隐藏输入字段自定义价格设置购物车项目价格
【发布时间】:2019-02-17 14:31:20
【问题描述】:

在 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。

我将var 设置为等于WC()-&gt;session-&gt;set( 'custom_data', $data );,然后设置var_dump 来检查它,但这会返回NULL,这可能就是它返回的内容,我不完全确定,因为我从未使用过它。

我还应该补充一点,我将产品后端中的 regular_price 设置为 0。当我删除它(并将其留空)时,我会返回错误:

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

我想知道我是否在这里遗漏了什么,是否有人可以对此有所了解?谢谢

【问题讨论】:

  • 您好,感谢您的回复,但除了我的隐藏字段之外,这是我的完整代码,所以我不确定您还想要什么其他代码。
  • 期望的行为是将从隐藏字段检索到的新价格传递给 Woocommerce 购物车价格。那有意义吗?我将把它添加到问题中。

标签: php wordpress woocommerce cart price


【解决方案1】:

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 文件中。经过测试并且可以工作。

【讨论】:

  • 做到了。为什么不需要设置 WC()->session?
  • 我做了一个简单的更新...不需要 WC_Session 数据,因为我们有必要的数据作为自定义购物车项目数据...如果您尝试:print_r($cart_item); 在购物车项目 foreach 循环中,您将看到什么我是说。
  • @logos_164 你在this very recent answer 中看到几乎相同的东西...... WC_Session 也没有被使用。例如,当您需要在购物车项目之外设置一个值时,WC_session 主要用于能够在 Ajax 驱动的代码上获取它。例如,Woocommerce 使用 WC_Session 来存储客户选择的选项。
  • 这个例子在你的评论上面,自动改变价格。我想在循环和购物车页面上使用文本输入字段更改价格。可能吗 。谢谢
  • @musabaltaci 您可以使用stackoverflow.com/a/48128915/3730754 之类的内容进行一些更改,您将在其中将隐藏的输入字段替换为文本输入字段...如果您需要帮助,请自己尝试并提出一个新问题...
猜你喜欢
  • 2018-05-14
  • 1970-01-01
  • 1970-01-01
  • 2018-08-29
  • 1970-01-01
  • 2019-12-21
  • 2015-11-28
  • 1970-01-01
相关资源
最近更新 更多