当前位置:  开发笔记 > 编程语言 > 正文

有条件地向购物车添加费用

如何解决《有条件地向购物车添加费用》经验,为你挑选了1个好方法。

我们有一个销售电脑零件的网站,这个网站上有一个名为"组装你的电脑"的部分,允许用户选择零件(主板 - cpu - ram等)并支付价格并获得他们的组装产品,一切正常很好,但它需要一个选项来添加"装配费"到购物车.仅当用户通过"组装您的PC"向导过程时才应添加此费用.我正在使用此代码,但它将其添加到所有购物车,我的意思是它不是有条件的.

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

并在constructor:

add_action( 'woocommerce_cart_calculate_fees', array( $this, 'calculate_fees' ) );

让我解释一下这个向导(组装你的PC)的更多信息:我们在本节中有3个步骤,

步骤1:用户从现有产品中选择主板,Ram,Cpu等.

第2步:查看列表并可以更改某些产品的数量(如Ram)

第3步:用户在最终列表中看到一些选项,可以
在页面底部选择组装费用(1.无需组装,只需购买清单2.组装一年保修3.组装无保修)以及何时点击"提交",所有选择的产品都会添加到购物车中,这是我们需要添加该费用的地方,因此只有在客户使用此进度时才应添加此费用.

我也尝试添加费用,而我将产品添加到购物车,但它没有这种方式,当我去购物车页面,这样没有额外的费用

    /* 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() );
}

Amin.. 6

这对我有用,如果有人有同样的问题:我将产品添加到购物车时设置一些会话,当购物车获得付款或清空时,我取消设置这些会话.

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

现在,当客户被重定向到另一个页面(在这种情况下,购物车页面)时,我们可以根据该会话确定他/她的购物车的额外费用

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

当购物车付清或被清空时,我们需要取消设置这些会话:

add_action( 'woocommerce_cart_emptied', 'dw_unset_fee_session' );
function dw_unset_fee_session(){
    unset( WC()->session->MY_SESSION );
}

这对我有用,请与我分享,如果有什么不对的话.



1> Amin..:

这对我有用,如果有人有同样的问题:我将产品添加到购物车时设置一些会话,当购物车获得付款或清空时,我取消设置这些会话.

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

现在,当客户被重定向到另一个页面(在这种情况下,购物车页面)时,我们可以根据该会话确定他/她的购物车的额外费用

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

当购物车付清或被清空时,我们需要取消设置这些会话:

add_action( 'woocommerce_cart_emptied', 'dw_unset_fee_session' );
function dw_unset_fee_session(){
    unset( WC()->session->MY_SESSION );
}

这对我有用,请与我分享,如果有什么不对的话.

推荐阅读
吻过彩虹的脸_378
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有