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
This is the modified code merged with yours, let me know if it works,