从 Woocommerce 3 中的隐藏输入字段自定义价格设置购物车项目价格 [英] Set cart item price from a hidden input field custom price in Woocommerce 3

查看:27
本文介绍了从 Woocommerce 3 中的隐藏输入字段自定义价格设置购物车项目价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Woocommerce 中,我使用 jQuery 在单个产品页面上计算自定义价格,现在需要将此值传递给购物车.

所需的行为是将从隐藏字段检索到的新价格传递给购物车商品价格.

这是我的实际代码:

//单品页面隐藏输入框add_action('woocommerce_before_add_to_cart_button', 'custom_hidden_​​product_field', 11, 0);函数 custom_hidden_​​product_field() {echo '<输入类型=隐藏"id="hidden_​​field";name="custom_price";class="custom_price";值=">";}//将此数据传递给购物车的代码:add_action('woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2);函数 save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {如果(!空($_REQUEST['custom_price'])){//设置购物车项目中的自定义数据$cart_item_data['custom_data']['custom_price'] = $_REQUEST['custom_price'];$data = array( 'custom_price' => $_REQUEST['custom_price'] );//下面的语句确保每个添加到购物车的操作都是唯一的订单项$cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );WC()->session->set('custom_data', $data);}返回 $cart_item_data;}

并检查 $data$cart_item_data 以查看它们都返回在页面上计算的 custom_price 数据.>

然而,我去查看购物车,该行项目的值仍然为0.

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

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

<块引用>

警告:遇到非数字值C:xampphtdocsmy-transfer-sourcewp-contentpluginswoocommerceincludesclass-wc-discounts.php 第 85 行

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

解决方案

2021 年更新 - 处理迷你购物车中的自定义价格项目

首先出于测试目的,我们在隐藏输入字段中添加一个价格,因为您没有提供计算价格的代码:

//添加一个隐藏的输入字段(值为 20 用于测试目的)add_action('woocommerce_before_add_to_cart_button', 'custom_hidden_​​product_field', 11);函数 custom_hidden_​​product_field() {echo '<输入类型=隐藏"id="hidden_​​field";name="custom_price";class="custom_price";值=20">';//测试价格为 20}

然后您将使用以下内容更改购物车商品价格(WC_Session 不需要):

//将自定义计算的价格保存为自定义购物车项目数据add_filter('woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2);函数 save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {if( isset( $_POST['custom_price'] ) && ! empty( $_POST['custom_price'] ) ) {//设置购物车项目中的自定义数据$cart_item_data['custom_price'] = (float) sanitize_text_field( $_POST['custom_price'] );//将每个项目作为一个独特的分离购物车项目$cart_item_data['unique_key'] = md5( microtime().rand() );}返回 $cart_item_data;}//对于迷你车add_action('woocommerce_cart_item_price', 'filter_cart_item_price', 10, 2);函数 filter_cart_item_price( $price, $cart_item ) {如果( isset($cart_item['custom_price'])){$args = array( 'price' => floatval( $cart_item['custom_price'] ) );如果(WC()->购物车->display_prices_include_tax()){$product_price = wc_get_price_including_tax( $cart_item['data'], $args);} 别的 {$product_price = wc_get_price_without_tax( $cart_item['data'], $args);}返回 wc_price( $product_price );}返回 $price;}//更新购物车商品价格add_action('woocommerce_before_calculate_totals', 'change_cart_item_price', 30, 1);函数 change_cart_item_price( $cart ) {if ( ( is_admin() && !defined( 'DOING_AJAX' ) ) )返回;if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )返回;//遍历购物车项目foreach ( $cart->get_cart() as $cart_item ) {//设置新价格if(isset($cart_item['custom_price'])){$cart_item['data']->set_price($cart_item['custom_price']);}}}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

In Woocommerce, I used jQuery to calculate a custom price on a single product pages, and now need to pass this value to the cart.

The desired behavior is to pass the new price retrieved from the hidden field to the cart item price.

Here is my actual code:

// 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;
}

And check both $data and $cart_item_data to see that they both return the custom_price data that is calculated on the page.

However, I go to view cart, and the value of the line item is still 0.

I set a var equal to the WC()->session->set( 'custom_data', $data ); and then var_dump to check it, but this returns NULL which might just be what it returns, I'm not entirely sure because I've never used it.

I should also add that I have the regular_price in the product backend set to 0. When I erase this (and leave it blank) I get back the error:

Warning: A non-numeric value encountered in C:xampphtdocsmy-transfer-sourcewp-contentpluginswoocommerceincludesclass-wc-discounts.php on line 85

I'm wondering if I've missed something here, and if someone could lend some light onto this? Thanks

解决方案

Update 2021 - Handling custom price item in mini cart

First for testing purpose we add a price in the hidden input field as you don't give the code that calculate the price:

// 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
}

Then you will use the following to change the cart item price (WC_Session is not needed):

// 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']);
        }
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

这篇关于从 Woocommerce 3 中的隐藏输入字段自定义价格设置购物车项目价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆