简体   繁体   English

在Woocommerce 3中使用WC_Cart add_to_cart()方法存储自定义数据

[英]Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

I am creating a membership site and totally created static pages for each Membership plans (have only 3 plans). 我正在创建一个会员站点,并为每个会员计划(只有3个计划)完全创建了静态页面。 However, I have added products for each plan and when I hit SELECT PLAN button I redirect to some custom form where I ask users range of info we are going to use to fulfil the plan (same as sneakertub.com). 但是,我为每个计划添加了产品,当我按“选择计划”按钮时,我重定向到一些自定义表单,在该表单中,我询问用户我们将用于实现该计划的信息范围(与sneakertub.com相同)。

I have written code into the PHP page which will handle SUBMIT action of the form. 我已经将代码写入PHP页面,该页面将处理表单的SUBMIT操作。 This PHP file, infopage.php , will process POST data I sent via POST call and stores these all data into WC session. 这个PHP文件infopage.php将处理我通过POST调用发送的POST数据,并将所有这些数据存储到WC会话中。

$customer_name = $_POST["customer_name"];
$customer_email = $_POST["customer_email"];
$customer_sex = $_POST["customer_sex"];
$customer_age = $_POST["customer_age"];
$product_id = $_POST["product_id"];

global $wp_session;
$data = array(
       'customer_name' => $customer_name,
       'customer_email'   => $customer_email,
       'customer_sex'  => $customer_sex,
       'customer_age'     => $customer_age);
$wp_session['custom_SESSION_child']=$data;

WC()->session->set('custom_data_child', $data);

//Add product to WooCommerce cart.
WC()->cart->add_to_cart( $product_id )

However, I don't think the above code works. 但是,我认为上面的代码不起作用。 As I don't find values into session with any of the above technique. 由于我找不到上述任何一种技术的价值。 I have used wp_session , WC()->session and $_SESSION but no approach is working. 我已经使用过wp_sessionWC()->session$_SESSION但是没有办法起作用。

I am trying to access these values into functions.php this way, 我正在尝试通过这种方式将这些值访问到functions.php

add_action( 'woocommerce_before_calculate_totals', 'twf_additional_price', 1, 3 );

function twf_additional_price( $cart_object ) {

    global $wpdb;

    global $wp_session;
    $session_data_2 =  $wp_session['custom_SESSION_child'];
    $session_data = WC()->session->get('custom_data_child');
    var_dump($session_data);
    var_dump($session_data2);

    foreach ( $cart_object->cart_contents as $key => $value ) {
        $extra_charge = 0;
        if(isset($value['twf_user_custom_datas'])){
            $extra_charge = 100;
        }

        $value['data']->set_price($value['data']->price + $extra_charge);
    }
}

For now ignore the for loop. 现在,请忽略for循环。 Main thing is 主要的是

var_dump($session_data);
        var_dump($session_data2);

both dumps only NULL . 两个都只转储NULL

My main goal is to add the all above fields into Woocommerce checkout and order pages. 我的主要目标是将上述所有字段添加到Woocommerce结帐和订单页面中。

Please let me know what is wrong here. 请让我知道这里出了什么问题。 I know I might be working on very bad approach but I want Plan selection to checkout process same as sneakertub.com. 我知道我可能正在采用非常糟糕的方法,但是我希望“计划选择”的结帐过程与sneakertub.com相同。 Please let me know if there is any tutorial on this or proper way to do this. 如果有关于此方法或适当方法的任何教程,请告诉我。 I prefer doing this without plugins but I am ready to use plugins as well. 我更喜欢在不使用插件的情况下进行操作,但是我也准备使用插件。

I appreciate your attention. 感谢您的关注。

Updated - Instead of using sessions, you should use the last available argument in WC_Cart add_to_cart() method , which will allow you to add any custom cart item data . 已更新 -您应该使用WC_Cart add_to_cart()方法中的最后一个可用参数,而不是使用会话,这将允许您添加任何自定义购物车数据

For cart item price change based on calculations, is better to make the new price calculation before and to set it in that custom cart item data. 对于基于计算的购物车价格变动,最好先进行新的价格计算,然后在该自定义购物车数据中进行设置。

Try the following instead: 请尝试以下操作:

1) For your code in the php page: 1)对于您在php页面中的代码:

$custom_data = array(); // Initializing

// Set the posted data as cart item custom data
if( isset($_POST['customer_name']) && ! empty($_POST['customer_name']) )
    $custom_data['custom_data']['name']  = sanitize_text_field( $_POST['customer_name'] );
if( isset($_POST['customer_email']) && ! empty($_POST['customer_email']) )
    $custom_data['custom_data']['email'] = sanitize_text_field( $_POST['customer_email'] );
if( isset($_POST['customer_sex']) && ! empty($_POST['customer_sex']) )
    $custom_data['custom_data']['sex']   = sanitize_text_field( $_POST['customer_sex'] );
if( isset($_POST['customer_age']) && ! empty($_POST['customer_age']) )
    $custom_data['custom_data']['age']   = sanitize_text_field( $_POST['customer_age'] );

// Set the calculated item price as custom cart item data
if( isset($custom_data['custom_data']) && sizeof($custom_data['custom_data']) > 0 && $product_id > 0 ) {
    // Get an instance of the WC_Product object
    $product = wc_get_product( $product_id );
    // Save the new calculated price as custom cart item data
    $custom_data['custom_data']['new_price'] = $product->get_price() + 100;
}

// Add product to cart with the custom cart item data
WC()->cart->add_to_cart( $product_id, '1', '0', array(), $custom_data );

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。


2) Your revisited function that will change the cart item price : 2)您重新访问的功能将改变购物车的价格

add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_price', 30, 1 );
function custom_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    foreach ( $cart->get_cart() as $cart_item ) {
        if( isset($cart_item['custom_data']['new_price']) )
            $cart_item['data']->set_price( $cart_item['custom_data']['new_price'] );
    }
}

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。

All other custom cart item data is available under the cart item key 'custom_data' as an indexed array… So you will be able to get that data easily from the cart object, to save it in the order. 所有其他自定义购物车项目数据都可以在购物车项目键'custom_data'作为索引数组使用……因此,您可以轻松地从购物车对象中获取该数据,并将其保存在订单中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM