在Woocommerce 3中使用WC_Cart add_to_cart()方法存储自定义数据 [英] Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

查看:109
本文介绍了在Woocommerce 3中使用WC_Cart add_to_cart()方法存储自定义数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个会员站点,并为每个会员计划(只有3个计划)完全创建了静态页面.但是,我为每个计划添加了产品,当我按下选择计划"按钮时,我重定向到一些自定义表单,在该表单中,我向用户询问我们将用于实现该计划的信息范围(与sneakertub.com相同).

I am creating a membership site and totally created static pages for each Membership plans (have only 3 plans). 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).

我已将代码写入PHP页面,该页面将处理表单的SUBMIT操作.这个PHP文件infopage.php将处理我通过POST调用发送的POST数据,并将所有这些数据存储到WC会话中.

I have written code into the PHP page which will handle SUBMIT action of the form. This PHP file, infopage.php, will process POST data I sent via POST call and stores these all data into WC session.

$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 )

但是,我认为上面的代码不起作用.由于我在上述任何技术中都找不到会话值.我已经使用过wp_sessionWC()->session$_SESSION,但是没有方法起作用.

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.

我正在尝试通过这种方式将这些值访问functions.php

I am trying to access these values into functions.php this way,

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循环.主要是

For now ignore the for loop. Main thing is

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

都只转储NULL.

我的主要目标是将上述所有字段添加到Woocommerce结帐和订购页面中.

My main goal is to add the all above fields into Woocommerce checkout and order pages.

请让我知道这里出了什么问题.我知道我可能正在采用非常糟糕的方法,但是我希望计划选择"的结帐过程与"sneakertub.com"相同.如果有关于此方法或适当方法的任何教程,请告诉我.我更喜欢在不使用插件的情况下进行此操作,但是我也准备使用插件.

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. 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.

感谢您的关注.

推荐答案

已更新-您应该使用

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.

对于基于计算得出的购物车价格变化,最好先进行新的价格计算并将其设置在该自定义购物车数据中.

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.

请尝试以下操作:

1)对于php页面中的代码:

1) For your code in the php page:

$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 );

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

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

2)您重新访问的功能将更改购物车价格:

2) Your revisited function that will change the cart item price:

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

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

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

所有其他自定义购物车项目数据都可以在购物车项目键'custom_data'下作为索引数组使用...因此,您将能够轻松地从购物车对象中获取该数据,并将其保存在订单中.

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.

这篇关于在Woocommerce 3中使用WC_Cart add_to_cart()方法存储自定义数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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