Jump to content

How do you assign an output name to a function?


Nixak

Recommended Posts

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?

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.

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.

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

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.