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

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

问题描述

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

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.

这是我的实际代码:

// 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数据.

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

但是,我去查看购物车,但该订单项的值仍为0.

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

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

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.

我还应该补充一点,我将产品后端中的regular_price设置为0.当我擦除此字符(并将其保留为空白)时,我得到了错误提示:

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:

警告:在 C:\ xampp \ htdocs \ my-transfer-source \ wp-content \ plugins \ woocommerce \ includes \ class-wc-discounts.php在第85行

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

我想知道我是否在这里错过了什么,是否有人可以对此有所帮助?谢谢

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

推荐答案

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

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
}

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

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

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

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

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

相关: Woocommerce set_quantity在以下情况下使网站崩溃将产品添加到购物车

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

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