问题描述
我们有一个出售计算机零件的网站,该网站中有一个名为组装您的PC的部分,允许用户选择零件(主板-cpu-ram等)并支付价格并获得他们的组装产品,一切正常,但需要选择向购物车添加组装费。
仅当用户经过组装您的PC向导过程时,才应添加此费用。
我正在使用此代码,但它会将其添加到所有购物车中,我的意思是它不是有条件的。
We have a website that sells computer parts, There's a section in this website called "Assemble your PC" that allows users to choose parts( mainboard - cpu - ram ,etc. ) and pay the price and get their assembled product, everything works just fine, but it needs an option to add an "assembly fee" to cart.This fee should be added only if user goes trough the "Assemble your PC" wizard process.I'm using this code but it adds it to all carts , i mean it's not conditional.
public function calculate_fees( $fees ){
global $woocommerce;
$cart_subtotal = $woocommerce->cart->subtotal;
$additional_fee = 0; // Additional fee
$additional_fee_text = __('Assembling fee : ', 'dornaweb');
if( $_POST['assembly_fee'] === 'flat_assembly_rate' ){
$additional_fee = intval( dw_option('flat_assembly_rate') ) ?: 0; // flat assembly fee
$additional_fee_text .= __(' ( assembly, no warranty )', 'dornaweb');
} elseif( $_POST['assembly_fee'] === 'percentage_assembly_fee' ) {
$additional_fee = dw_option('percentage_assembly_fee') ? ( ( intval( dw_option('percentage_assembly_fee') ) / 100 ) * $cart_subtotal ) : 0; // percentage assembly fee
$additional_fee_text .= __(' ( assembly + one year warranty )', 'dornaweb');
}
$woocommerce->cart->add_fee( $additional_fee_text, intval( $additional_fee ), 1 );
}
和构造函数
:
add_action( 'woocommerce_cart_calculate_fees', array( $this, 'calculate_fees' ) );
让我解释一下有关此向导的更多信息(组装PC):
本节中的3个步骤,
Let me explain a little more about this wizard ( Assemble your PC ) :We have 3 steps in this section,
- 第1步:用户从现有的
产品中选择主板,Ram,Cpu等。 - 第2步:查看列表并可以更改某些产品的数量(如Ram那样的
) - 第3步:用户看到最终的带有一些选项的列表,可以在页面底部选择
来收取组装费(1.不组装,
只是购买此清单2.享有一年保修3.组装
没有保修),并且当他们点击提交时,所有选择的产品
都会添加到购物车中,这是我们需要添加该费用的位置,因此只有在客户使用此费用时才应添加此费用
进度。
我还尝试了在向购物车中添加产品时增加费用,但是这种方式在我进入购物车时不起作用页面,这种方式无需支付额外费用
I also tried adding fees while i add products to cart but it doesn't work this way, when i go to cart page , there's no additional fee in this way
/* User chooses list of products ( mainboard, ram, ... ) and can choose quantity of each product
and also can choose whether he/she wants to get an assembled computer
or just wants to buy the products individually,
then when he/she hits submit products get added to cart all together
in this step assembling fee should be added and calculated in cart total */
if( !empty( $_POST['action'] ) && $_POST['action'] == 'add' ){
/**
* Add each product to cart based on user choice
*/
foreach( $_POST['products'] as $product_id => $data ) {
WC()->cart->add_to_cart( $product_id, $data['qty'] );
}
/**
* Add additional fees
*/
WC()->cart->add_fee( 'Assembling fee', 10 ); // 10 is an example , the fee differs depending on user choice ( no assemble:0, assemlby+warranty:5% of the cart subtotal, assembly+no warranty: a flat fee eg. 20$ )
WC()->cart->calculate_fees();
/**
* Redirect to cart
*/
wp_safe_redirect( WC()->cart->get_cart_url() );
}
推荐答案
假设有人遇到了同样的问题:
当我将商品添加到购物车时,我设置了一些会话,当购物车被付款或清空时,我未设置这些会话。
This worked for me, in case some one has the same issue : I set some sessions when products are added to cart, and when the cart gets paid or emptied, I unset those sessions.
if( !empty( $_POST['action'] ) && $_POST['action'] == 'add' ){
/**
* Add each product to cart based on user choice
*/
foreach( $_POST['products'] as $product_id => $data ) {
WC()->cart->add_to_cart( $product_id, $data['qty'] );
}
/**
* We set sessions after products added to cart
*/
WC()->session->set( 'MY_SESSION', 'My_value' );
/**
* Redirect to cart
*/
wp_safe_redirect( WC()->cart->get_cart_url() );
}
现在,当客户重定向到另一个页面时(在这种情况下,购物车页面),我们可以根据该会话确定购物车的额外费用
Now when the customer is redirected to another page( in this case , cart page ) , We can determine his/her cart's additional fee based on that session
function dw_calculate_fees( $cart_object ){
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if( ! WC()->session->MY_SESSION ) return;
$cart_subtotal = $woocommerce->cart->subtotal;
$additional_fee = null;
if( WC()->session->MY_SESSION === 'example_a' ){
$additional_fee = 1;
} elseif( WC()->session->MY_SESSION === 'example_b' ) {
$additional_fee = 2;
} else{
$additional_fee = 0;
}
if( ! is_null( $additional_fee ) ) {
$woocommerce->cart->add_fee( 'Additional fee ', intval( $additional_fee ), 1 );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'dw_calculate_fees' );
,我们需要在购物车付款或清空后取消设置这些会话:
and we need to unset those sessions when the cart is paid or gets emptied :
add_action( 'woocommerce_cart_emptied', 'dw_unset_fee_session' );
function dw_unset_fee_session(){
unset( WC()->session->MY_SESSION );
}
这对我有用,如果有问题,请与我分享
This worked for me, please share with me, if something is wrong about it.
这篇关于有条件的增加购物车费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!