Par*_*kor 3 php wordpress cart woocommerce woocommerce-subscriptions
我正在创建一个会员网站并为每个会员计划完全创建静态页面(只有 3 个计划)。但是,我为每个计划添加了产品,当我点击 SELECT PLAN 按钮时,我重定向到一些自定义表单,在那里我向用户询问我们将用来完成计划的信息范围(与sneakertub.com 相同)。
我已将代码写入 PHP 页面,该页面将处理表单的提交操作。这个 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 )
Run Code Online (Sandbox Code Playgroud)
但是,我认为上面的代码不起作用。因为我没有找到任何上述技术的会话值。我用过wp_session,WC()->session和$_SESSION但是没办法工作。
我试图以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);
}
}
Run Code Online (Sandbox Code Playgroud)
现在忽略for循环。主要的是
var_dump($session_data);
var_dump($session_data2);
Run Code Online (Sandbox Code Playgroud)
仅两个转储 NULL。
我的主要目标是将上述所有字段添加到 Woocommerce 结帐和订单页面中。
请让我知道这里有什么问题。我知道我可能正在采用非常糟糕的方法,但我希望计划选择与sneakertub.com 相同的结帐过程。请让我知道是否有任何关于此的教程或执行此操作的正确方法。我更喜欢在没有插件的情况下执行此操作,但我也准备使用插件。
我感谢您的关注。
更新- 您应该使用WC_Cart add_to_cart()method 中最后一个可用参数,而不是使用会话,这将允许您添加任何自定义购物车项目数据。
对于基于计算的购物车项目价格变化,最好在之前进行新的价格计算并将其设置在该自定义购物车项目数据中。
请尝试以下操作:
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 );
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或活动主题)的 function.php 文件中。测试和工作。
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'] );
}
}
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或活动主题)的 function.php 文件中。测试和工作。
所有其他自定义购物车项目数据都可以在购物车项目键下
'custom_data'作为索引数组使用......因此您将能够轻松地从购物车对象中获取该数据,并将其保存在订单中。
| 归档时间: |
|
| 查看次数: |
9087 次 |
| 最近记录: |