首页 > 解决方案 > 当我未登录 Wordpress 时,该功能有效。但是,当我登录时,它不起作用

问题描述

我有一个功能,我想根据一些计算添加费用。该功能在我未登录网站时有效,但在我登录时无效。登录时,Cart页面不显示任何内容(没有产品或其他任何内容);它只显示一个标题为“购物车”的白页,仅此而已。这是我的代码:

 function prefix_add_discount_linett(  $cart ) {

     $termID = $_SESSION['clicked'];



// get the quantity total number of the category that have ID '16' and the products options must be "sénior" or "méga". 
    function cat_cart_count( $cat_name, $termID ) {
      $termID = $_SESSION['clicked'];
        $cat_count = 0;        

        foreach(WC()->cart->get_cart() as $cart_item) 

            if( (has_term( $cat_name, 'product_cat', $cart_item['product_id']) && $cart_item['yith_wapo_options'][0]['value']=='sénior') || (has_term( $cat_name, 'product_cat', $cart_item['product_id']) && $cart_item['yith_wapo_options'][0]['value']=='méga')){
                $cat_count += $cart_item['quantity'];
     }else{
      continue;
     }

// on this one, I have two buttons one is delivery and in this one it is take_away so take away must check if the number is divisible into two or not and I must alawys get an integer number, but when I choose Delivery check if the number is divisible into three or not and I must alawys get an integer number as well.
if($termID=="take_away"){
            if ($cat_count%2 == 1) {     

              $cat_count=$cat_count/2;
              floor($cat_count);           

              }else{

                 $cat_count=$cat_count/2;
                   floor($cat_count);

                    }

 }else{

            if ($cat_count%3 == 0) {        

              $cat_count=$cat_count/3;
              floor($cat_count);

             }else{

                $cat_count=$cat_count/3;
                floor($cat_count);

                  }

       }
                return floor($cat_count);
    }
    cat_cart_count( 16 );


// this function is for getting the minimal price of the category '16' with the options "sénior" or "méga".
    function cat_cart_prix_min( $cat_name ) {

        $testprix = 900000;        

        foreach(WC()->cart->get_cart() as $cart_item)  
         if( (has_term( $cat_name, 'product_cat', $cart_item['product_id']) && $cart_item['yith_wapo_options'][0]['value']=='sénior') || (has_term( $cat_name, 'product_cat', $cart_item['product_id']) && $cart_item['yith_wapo_options'][0]['value']=='méga')){

                    if ($testprix>$cart_item['yith_wapo_options'][0]['price_original']) {

                        $testprix = $cart_item['yith_wapo_options'][0]['price_original'];

                    }else{

                    continue;

                    }

         }else{

      continue;

        }return $testprix;


    }

     if($termID=="delivery" || $termID=="take_away"){
        $prixmin = cat_cart_prix_min(16);

     $total_QN = cat_cart_count( $cat_name );


// this variable '$simo' it count the total of the quantities multiplied by the minimal price ($simo = quantities * minimal price)
    $simo = $total_QN*$prixmin;

    }

// this one is for counting the fees
      $discount = $simo;
      if($discount>0){
      $cart->add_fee( __( 'Offertes', 'yourtext-domain' ) , -$discount );
    }
    }
    add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_linett' );

标签: phpwordpresswoocommercewordpress-login

解决方案


推荐阅读