skip to Main Content

I’ve tried to copy my php code from foreach loop using ob start but failed. Does anyone knows how to do this? I wanted to add my php code in another function like below.

foreach($kekei as $good => $goodness) { 

                        ob_start();

                        $GLOBALS['myfirstbowanstart'] .= "if ( $packing_fee === 'strtolower(".$goodness['shipping_code'].")' ) {
                        $label = __('Shipping fee');
                        $cost  = ".$goodness['shipping_fee'].";
                        }"; // put here your recursive function name

                        ob_get_clean();

                    }

// Add a custom dynamic packaging fee
add_action( 'woocommerce_cart_calculate_fees', 'add_packaging_fee', 20, 1 );
function add_packaging_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    //$domain      = "woocommerce";
    $packing_fee = WC()->session->get( 'the_chosen_one' ); // Dynamic packing fee


    echo $GLOBALS['myfirstbowanstart'];

/*
    if ( $packing_fee === 'gzeatp1' ) {
        $label = __("Shipping fee");
        $cost  = 3.00;
    } elseif ( $packing_fee === 'box' ) {
        $label = __("Shipping fee");
        $cost  = 9.00;
    }
*/
    if ( isset($cost) )
        $cart->add_fee( $label, $cost );
}

2

Answers


  1. <?php
    $GLOBALS = array("results"=>array(), "tests" => array("test1", "test2", "test3"));
    $label = $cost = "";
    $packing_fees = array("gzeatp1" => "3.00", "box" => "9.00", "cotton" => "12.00");
    
    
    $packing_fee = "cotton";   //WC()->session->get( 'the_chosen_one' );
    
    
    $kekei = array(
                    "first_good" => array("param1" => "value1", "shipping_code" => "gzeatp1"),
                    "second_good"=> array("param2" => "value2", "shipping_code" => "box"),
                    "third_good" => array("param3" => "value3", "shipping_code" => "cotton"),
                  );
    
    
    $callback =  
            function ($shipping_code, $packing_fee) use ($cost, &$packing_fees)
            {
                if($packing_fee === strtolower($shipping_code)) { 
                   $label = 'Shipping fee';
                   $cost = $packing_fees[$shipping_code];
    
                   $GLOBALS["results"] = array("label" => $label, "cost"=> $cost);
                }
            };
    
    
    foreach($kekei as $good => $goodness) { 
       $callback($goodness['shipping_code'], $packing_fee) ;
    } 
    
    // Add a custom dynamic packaging fee
    //add_action( 'woocommerce_cart_calculate_fees', 'add_packaging_fee', 20, 1 );
    
    function add_packaging_fee( $cart = "" ) {
        //Disabled for testing
        #if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        #    return;
    
        //$domain      = "woocommerce";
        //$packing_fee = WC()->session->get( 'the_chosen_one' ); // Dynamic packing fee
    
    // If $label and $cost are global variables, comment out next 2 lines.
        $label = $GLOBALS["results"]["label"];
        $cost = $GLOBALS["results"]["cost"];
    
    /*
        if ( $packing_fee === 'gzeatp1' ) {
            $label = __("Shipping fee");
            $cost  = 3.00;
        } elseif ( $packing_fee === 'box' ) {
            $label = __("Shipping fee");
            $cost  = 9.00;
        }
    */
        echo json_encode($GLOBALS);
    
        //if ( isset($cost) )
            //$cart->add_fee( $label, $cost );
    }
    
    add_packaging_fee("test");
    
    Login or Signup to reply.
  2. This is the modified code merged with yours, let me know if it works,

    //Added this block
    $packing_fees = array("gzeatp1" => "3.00", "box" => "9.00", "cotton" => "12.00");  //This is for testing purposes
    
    $packing_fee = WC()->session->get( 'the_chosen_one' );  // make this a global variable
    
    $callback =  
        function ($shipping_code, $packing_fee) use ($cost, &$packing_fees)
        {
            if($packing_fee === strtolower($shipping_code)) { 
               $label = __('Shipping fee');
               $cost = $packing_fees[$shipping_code];
    
               $GLOBALS["myfirstbowanstart"] = array("label" => $label, "cost"=> $cost);
            }
        };
    
    
     foreach($kekei as $good => $goodness) { 
       $callback($goodness['shipping_code'], $packing_fee) ;
     } 
    /*  //Removed this block
     foreach($kekei as $good => $goodness) { 
    
    
                            ob_start();
    
                            $GLOBALS['myfirstbowanstart'] .= "if ( $packing_fee === 'strtolower(".$goodness['shipping_code'].")' ) {
                            $label = __('Shipping fee');
                            $cost  = ".$goodness['shipping_code'].";
                            }"; // put here your recursive function name
    
                            ob_get_clean();
    
                        }
    
    */
    
    // Add a custom dynamic packaging fee
    add_action( 'woocommerce_cart_calculate_fees', 'add_packaging_fee', 20, 1 );
    function add_packaging_fee( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        //$domain      = "woocommerce";
        $packing_fee = WC()->session->get( 'the_chosen_one' ); // Dynamic packing fee
    
        //changed this line
        //echo $GLOBALS['myfirstbowanstart'];
    
        $label = $GLOBALS['myfirstbowanstart']['label'];
        $cost  = $GLOBALS['myfirstbowanstart']['cost'];
    
    /*
        if ( $packing_fee === 'gzeatp1' ) {
            $label = __("Shipping fee");
            $cost  = 3.00;
        } elseif ( $packing_fee === 'box' ) {
            $label = __("Shipping fee");
            $cost  = 9.00;
        }
    */
        if ( isset($cost) )
            $cart->add_fee( $label, $cost );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search