Ob_start the code

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

hello can anyone help me with this?

ob captures output. It stands for “output buffering”, which means the output goes into a buffer and not out immediately. That’s obviously not what you want to do.

So either copy the code, or more preferably, place that code into another function that gets called when needed by anything that would use it.

Can you please show how to do it using the code above?

Since you obviously arent here to learn…

Sponsor our Newsletter | Privacy Policy | Terms of Service