php - 从 Woocommerce 3 中的隐藏输入字段自定义价格设置购物车项目价格

标签 php wordpress woocommerce cart price

在 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()->session->set( 'custom_data', $data );然后 var_dump检查它,但这会返回 NULL这可能就是它返回的内容,我不完全确定,因为我从未使用过它。
我还应该补充一点,我有 regular_price在产品后端设置为 0。当我删除它(并将其留空)时,我得到了错误:

Warning: A non-numeric value encountered in C:\xampp\htdocs\my-transfer-source\wp-content\plugins\woocommerce\includes\class-wc-discounts.php on line 85


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

最佳答案

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 文件中。测试和工作。

关于php - 从 Woocommerce 3 中的隐藏输入字段自定义价格设置购物车项目价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52303818/

相关文章:

php - Docker 撰写文件未显示来自 wp-config.php 的 PHP 错误?

php - 更改 WooCommerce 默认密码安全级别

php - Woocommerce - 将新订单详细信息复制到新表格

php - 连接和转义字符后输出不完整

php - 在 Symfony2 项目中的 "post-update-cmd"上运行命令

css - WordPress 主题 : Wrapping entry titles?

javascript - li 背景颜色重复/图案

php - WooCommerce 密码强度更改为 6 个字符

javascript - 如何使用 PHP/HTML 限制选中复选框的数量并将值传递到不同的页面

php - mt_rand + time() 生成的 key 如何准确且唯一以用作预订引用 ID?