Nixak Posted July 23, 2009 Share Posted July 23, 2009 Hi, I have a function called "getshoppingcartinfo" the function grabs alot of variables. function getShoppingCartInfo() { global $cart, $order; $retStr = ""; if (MODULE_SHOPCART == 'true') { $shipping = $order->info['shipping_cost']; $shipping = number_format($shipping, 2, '.', ''); $products = $cart->get_products(); $no_lines = sizeof($products); $no_lines = $no_lines + 1; $shippingStr .= ":Shipping:1:".$shipping.":----:".$shipping.":".$shipping; $basketStr = "BasketSt=".$no_lines; $moreStr .= ":More products...See Order.:1:----:----:----:----"; $moreLen = strlen( $moreStr ); /* Maxmium size of the basket field according to Documentation V2.22 is 7500 characters. Allowing 7495 as a small safety net. */ $charCount = 7495 - strlen( $shippingStr ) - strlen( $basketStr ); $detail = ""; $linesAdded = 0; $i = 0; $n=sizeof($products); $finished = false; while ( ( $i<$n ) && ( !$finished ) ) { $desc = $products[$i]['name']; $desc = str_replace(":", "", $desc); $qty = $products[$i]['quantity']; $price = $products[$i]['price'] + $cart->attributes_price($products[$i]['id']); $tax = $price/100 * zen_get_tax_rate($products[$i]['tax_class_id']); $tax = number_format($tax, 2, '.', ''); $finalPrice = $price + $tax; $finalPrice = number_format($finalPrice, 2, '.', ''); $lineTotal = $qty * $finalPrice; $lineTotal = number_format($lineTotal, 2, '.', ''); $line = ":".$desc.":".$qty.":".$price.":".$tax.":".$finalPrice.":".$lineTotal; $line = str_replace("\n", "", $line); $line = str_replace("\r", "", $line); $len = strlen( $line ); if ( ( $charCount - $moreLen - $len) > 0 ) { $linesAdded ++; $charCount -= $len; $detail .= $line; } else if ( $charCount - $moreLen > 0 ) { $detail .= $moreStr; $charCount -= $len; $linesAdded ++; $finished = true; } else { // We should not hit this point, but if we do lets fininsh $finished = true; } $i++; } if ( strlen( $detail ) > 0 ) { $linesAdded ++; $retStr = "&" . "BasketSt=" . $linesAdded . $detail . $shippingStr; } } return $retStr; } How do I assign an output name to this function? So later on I can grab the output of that function or have I just missed the ouput name somwhere? Link to comment https://forums.phpfreaks.com/topic/167116-how-do-you-assign-an-output-name-to-a-function/ Share on other sites More sharing options...
dzelenika Posted July 23, 2009 Share Posted July 23, 2009 I don't understand meaning of term "output name". Probably you would to see, from main program, variables grabbed in function? Link to comment https://forums.phpfreaks.com/topic/167116-how-do-you-assign-an-output-name-to-a-function/#findComment-881123 Share on other sites More sharing options...
kickstart Posted July 23, 2009 Share Posted July 23, 2009 Hi Simplest way would be to return an array, and then you store the returned array and access it when required. All the best Keith Link to comment https://forums.phpfreaks.com/topic/167116-how-do-you-assign-an-output-name-to-a-function/#findComment-881126 Share on other sites More sharing options...
Nixak Posted July 23, 2009 Author Share Posted July 23, 2009 Hi, the function grabs the variables and then later on in the script I need to send the output/result of that function elsewhere. For example the script grabs the "delivery phone number" and then later on it sends that result (along with other infomation) via this line of coding $plain .= "DeliveryPhone=" . $order->customer['telephone'] . "&"; I tried to accomplish this by writing this if (MODULE_SHOPCART == 'true') { $plain .= "Basket=" . $cart->basket['shopping_cart'] . ""; } but the function "getshoppingcartinfo" does not seem to have an output name ($cart->basket['shopping_cart']) If Kickstart is correct, how do you code the array? sorry i'm not a coder so i'm on a steep learning curve lol. Link to comment https://forums.phpfreaks.com/topic/167116-how-do-you-assign-an-output-name-to-a-function/#findComment-881135 Share on other sites More sharing options...
dzelenika Posted July 23, 2009 Share Posted July 23, 2009 In main program you can't see variables from function. The easiest way to see variables from outer program is to collect variables into array then return array. For example instead of: $shipping = number_format($shipping, 2, '.', ''); you could wrote: $returnArray['shipping'] = number_format($shipping, 2, '.', ''); .... and at the end instead of: return $retStr; you could wrote: return $returnArray; In outer program you should call function like this: $arr = getShoppingCartInfo(); to collect values grabbed in function. Link to comment https://forums.phpfreaks.com/topic/167116-how-do-you-assign-an-output-name-to-a-function/#findComment-881147 Share on other sites More sharing options...
kickstart Posted July 23, 2009 Share Posted July 23, 2009 Hi Very quick example from your. This returns an array containing the return string, the shipping string and the products array. <?php function getShoppingCartInfo() { global $cart, $order; $retArray = array('retStr' => "", 'shipping' => '', 'products' => ''); if (MODULE_SHOPCART == 'true') { $retArray['shipping'] = $order->info['shipping_cost']; $retArray['shipping'] = number_format($retArray['shipping'], 2, '.', ''); $retArray['products'] = $cart->get_products(); $no_lines = sizeof($retArray['products']); $no_lines = $no_lines + 1; $shippingStr .= ":Shipping:1:".$shipping.":----:".$shipping.":".$shipping; $basketStr = "BasketSt=".$no_lines; $moreStr .= ":More products...See Order.:1:----:----:----:----"; $moreLen = strlen( $moreStr ); /* Maxmium size of the basket field according to Documentation V2.22 is 7500 characters. Allowing 7495 as a small safety net. */ $charCount = 7495 - strlen( $shippingStr ) - strlen( $basketStr ); $detail = ""; $linesAdded = 0; $i = 0; $n=sizeof($retArray['products']); $finished = false; while ( ( $i<$n ) && ( !$finished ) ) { $desc = $retArray['products'][$i]['name']; $desc = str_replace(":", "", $desc); $qty = $retArray['products'][$i]['quantity']; $price = $retArray['products'][$i]['price'] + $cart->attributes_price($retArray['products'][$i]['id']); $tax = $price/100 * zen_get_tax_rate($retArray['products'][$i]['tax_class_id']); $tax = number_format($tax, 2, '.', ''); $finalPrice = $price + $tax; $finalPrice = number_format($finalPrice, 2, '.', ''); $lineTotal = $qty * $finalPrice; $lineTotal = number_format($lineTotal, 2, '.', ''); $line = ":".$desc.":".$qty.":".$price.":".$tax.":".$finalPrice.":".$lineTotal; $line = str_replace("\n", "", $line); $line = str_replace("\r", "", $line); $len = strlen( $line ); if ( ( $charCount - $moreLen - $len) > 0 ) { $linesAdded ++; $charCount -= $len; $detail .= $line; } else if ( $charCount - $moreLen > 0 ) { $detail .= $moreStr; $charCount -= $len; $linesAdded ++; $finished = true; } else { // We should not hit this point, but if we do lets fininsh $finished = true; } $i++; } if ( strlen( $detail ) > 0 ) { $linesAdded ++; $retArray['retStr'] = "&" . "BasketSt=" . $linesAdded . $detail . $shippingStr; } } return $retArray; } ?> When called you would do something like:- $SomeVariable = getShoppingCartInfo(); And then to (say) loop through the products returned:- foreach ($SomeVariable['products'] as $aProduct) { echo $aProduct['name']." ".$aProduct['price']." ".$aProduct['quantity']." ". } However if it gets complicated then it is probably worth doing it with object orietated code (not a fan of object orietation, but this is the kind of situation where it makes sense). All the best Keith Link to comment https://forums.phpfreaks.com/topic/167116-how-do-you-assign-an-output-name-to-a-function/#findComment-881152 Share on other sites More sharing options...
Nixak Posted July 23, 2009 Author Share Posted July 23, 2009 Thanks for the help but unfortunatly neither suggestion worked, at first I had unexpected ; and ) problems but I managed to fix that. Maybe there is a problem with Zen-Cart's coding (as I know thier coding language is slightly different) or maybe I'm just a noobie at coding lol Anyway thank you all for trying Link to comment https://forums.phpfreaks.com/topic/167116-how-do-you-assign-an-output-name-to-a-function/#findComment-881216 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.