Link to home
Start Free TrialLog in
Avatar of Eddie Shipman
Eddie ShipmanFlag for United States of America

asked on

Iterating and modifying a PHP object

I have the code below that is like code that creates an Object built from a Magento order and converted to JSON then sent to our Oracle DB for processing. The issue we have with this is that Oracle has an issue with the length of the JSON string.

What I'd like to do is to parse this out to where the ItemCol collection has a maximum of 3 items each. The GrandTotal will need to be recalculated for the number of items in each ItemCol collection.

So, essentially just the ItemCol is what is changing (except the GrandTotal) The main object will need to be cloned and the ItemCol be amended to a maximum of three items each.

class NATransportMsg {
	public $CartSession;
	public $ClientIP;
	public $ClientBrowser;
	public $CartTime;
	public $Version;
	public $OracleSession;
	public $Action;
	public $ObjectSent;
}

class NAOrder {
	public $ShippingAmount;
	public $ShippingMethod;
	public $BaseTaxAmount;
	public $HowDidYouHear;
	public $Payment;
	public $ItemCol;
	public $CouponCode;
	public $OrderID;
	public $CustomerID;
	public $AddressesCol;
}

class NAOrderItem {
	public $Price;
	public $DiscountAmount;
	public $QuantityOrdered;
	public $Sku;
	public $CEDeclined;
	public $CEFirstName;
	public $CELastName;
	public $CEEventCode;
	public $CELicense;
	public $CEState;
	public $CETypeID;
	public $SP;
}

class NAOrderBillAddress {
	public $AddressType;
	public $Address1;
	public $Address2;
	public $Address3;
	public $City;
	public $State;
	public $Zip;
	public $Country;
}

class NAOrderShipAddress {
	public $AddressType;
	public $Address1;
	public $Address2;
	public $Address3;
	public $City;
	public $State;
	public $Zip;
	public $Country;
}

class NAPayment {
	public $AuthID;
	public $CCLast4;
	public $CCType;
	public $TransID;
	public $Method;
	public $GrandTotal;
}

class NASP {
	public $Address1;
	public $Address2;
	public $Address3;
	public $City;
	public $State;
	public $Country;
	public $PID;
	public $Email;
	public $Zip;
	public $LastName;
	public $FirstName;
}


$message                = new NATransportMsg;
$message->CartSession   = "32 bit hash value";
$message->ClientIP      = "Client IP address";
$message->ClientBrowser = "Client Browser UserAgent";
$message->CartTime      = "01-01-2015";
$message->Version       = 2;
$message->OracleSession = "";
$message->Action        = "Order";

$Object = new NAOrder;

$Order->ShippingAmount = 0;
$Order->ShippingMethod = "Shipping Method";
$Order->BaseTaxAmount = 0;

$Payment = new NAPayment;

$Payment->AuthID = null;
$Payment->CCLast4 = 9999;
$Payment->CCType = "VI";
$Payment->TransID = "PayPal V#";
$Payment->Method = "paypal";
$Payment->GrandTotal = 2150;

$Order->Payment = $Payment;

for($i = 1; $i < 6; $i++) {
	// create itemcol and SP
	$item = new NAOrderItem;	
	$sp = new NASP;
	$item->Price = 430;
	$item->DiscountAmount = 0;
	$item->QuantityOrdered = 1;
	$item->Sku = "SKU FOR ITEM";
	$item->C_Declined = null;
	$item->C_TypeID = null;
	
	$sp->DOB="99-99-9999";
	$sp->Address1="P.O. Box 1490";
	$sp->Address2=null;
	$sp->Address3=null;
	$sp->City="Jackson";
	$sp->State="NV";
	$sp->Country="US";
	$sp->PID="9999999";
	$sp->Phone="1-888-5551212-";
	$sp->Email="user".$i."@email.com";
	$sp->Zip="99999";
	$sp->LastName="Last1".$i;
	$sp->FirstName="First".$i;

	$item->SP = $sp;
	$ItemCol[] = $item;
}

$Order->ItemCol = $ItemCol;
$Order->OrderID = 9999999999;
$Order->CustomerID = 9999999;
$Order->CouponCode = null;

$AddressCol = new NAOrderBillAddress;

$AddressCol->AddressType = "B";
$AddressCol->Address1 = "P.O. Box 1490";
$AddressCol->Address2 = null;
$AddressCol->Address3 = null;
$AddressCol->City = "Jackson";
$AddressCol->State = "NV";
$AddressCol->Country =  "US";
$Order->AddressesCol = $AddressCol;

$message->ObjectSent    = $Order;

if(count((array)$message->ObjectSent->ItemCol) > 3)) {
	foreach($message->ObjectSent->ItemCol) {
		// Not sure how to actually iterate this and create a new $message object
		// for each time it goes past 3 items and copy the rest of the $message object and
		// update the GrandTotal of each $message object		
	}
}

Open in new window


I will also have an issue with transmission of the order data at 2 minute intervals but that is another question for later.
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eddie Shipman

ASKER

I like the last option, I will give it a test. I had it up on phpfiddle but didn't save it.
Although we chose to do this in our Oracle code, I'm gonna accept it.