Announcement

Collapse
No announcement yet.

Iterate through order and create nested value/key pairs for output via MVT:Call

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Iterate through order and create nested value/key pairs for output via MVT:Call

    Hi all,

    I have an interesting requirement in that I need to POST order information to a third party API that will then consume/translate this to XML. The API is limited, however, in that it can't take a long string and parse it. The following works to send the data as a long string within an XML output tag of <order>

    Code:
    <mvt:assign name="g.order"   value="miva_array_serialize( l.settings:order )"   />
     
    <mvt:comment>Send data to PHP script and get the response</mvt:comment>
    <mvt:call action="'http://myvendor.net/orderinfo'" method="'POST'" fields="'order'">
        <mvt:eval expr="s.callvalue" />
    </mvt:call>
    However, since the third party API is looking for individual tags (ex. <orderID> <orderPrice>) and can't parse the long string, I need to do something like this:

    Code:
    <mvt:assign name="g.orderaddr"   value="l.settings:order:bill_addr"   />
    <mvt:assign name="g.orderemail"   value="l.settings:order:bill_email"   />
    
    
    <mvt:comment>Send data to PHP script and get the response</mvt:comment>
    <mvt:call action="'http://myvendor.net/orderinfo'" method="'POST'" fields="'orderaddr,orderemail">
        <mvt:eval expr="s.callvalue" />
    </mvt:call>
    The challenge, and where I need help, is that an order may contain multiple items, and I won't know how many items exist/can't hard code a guess for how many items a customer may have in an order (ex. groups[1]:price, groups[2]:price, etc.).

    Is there a way to parse out the order so it creates individual tags, including the appropriate nesting, for items in an order versus sending all data in a string with a single tag of <order> as shown in my first example? I envision that the end output would look something like this:

    Code:
    <orderid>1</orderid>
    <customername>Bob Smith</customername>
    <items>
    <itemname>Item 1</itemname>
    <itemprice>50.00</itemprice>
    </items>
    <items>
    <itemname>Item 2</itemname>
    <itemprice>75.00</itemprice>
    </items>
    Thanks in advance for your assistance!

    Kelly

    #2
    Re: Iterate through order and create nested value/key pairs for output via MVT:Call

    Ah, something is missing (and it might be my brain), but i have to back you up a bit:

    Why would an API be set up to take a post and THEN translate it to XML instead of just taking XML which is an old (albeit satisfactory) method of data transfer? (And all you oauth/rest hipsters need not comment.)

    What is the actual API base requirements. If they support 'nested' items, then there should be a direct way to send the items with an indication that they are nested.
    Bruce Golub
    Phosphor Media - "Your Success is our Business"

    Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
    phosphormedia.com

    Comment


      #3
      Re: Iterate through order and create nested value/key pairs for output via MVT:Call

      Hi Bruce,

      The misunderstanding is my fault! They don't create the XML, but rather I need Miva to send the nested XML per my simplified example. Currently if I output "orders" from my first example, it creates XML like this:

      <orders>
      :orderid=1,customername=me...
      </orders>

      Where I need it to be akin to:

      <orderid>1</orderid>
      <customername>Bob Smith</customername>
      <items>
      <itemname>Item 1</itemname>
      <itemprice>50.00</itemprice>
      </items>
      <items>
      <itemname>Item 2</itemname>
      <itemprice>75.00</itemprice>
      </items>

      Comment


        #4
        Re: Iterate through order and create nested value/key pairs for output via MVT:Call

        Ah, then you want to approach it thusly (or at least one approach would be)

        (And sorry for the mess...)

        Code:
        	<MvASSIGN NAME="l.xml" VALUE="{ '<?xml version="1.0" encoding="ISO-8859-1"?>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<!DOCTYPE OrderList SYSTEM "http://www.domain.com/lib/OrderList.dtd">' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<OrderList MerchantName="' $ l.merchant_name $ '" MerchantId="' $ l.merchant_ID $ '">'$asciichar(10)}">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Order id="' $Orders.d.id$ '">' $asciichar(10) }">	
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<AddressInfo type="ship">' $asciichar(10) }">  
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Name>' $Orders.d.ship_fname$ ' ' $Orders.d.ship_lname$ '</Name>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Company>' $Orders.d.ship_comp$ '</Company>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Address1>' $Orders.d.ship_addr$ '</Address1>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Address2></Address2>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<City>' $Orders.d.ship_city$ '</City>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<State>' $Orders.d.ship_state$ '</State>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Country>' $Orders.d.ship_cntry$ '</Country>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Zip>' $Orders.d.ship_zip$ '</Zip>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Phone>' $Orders.d.ship_phone$ '</Phone>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Email>' $Orders.d.ship_email$ '</Email>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '</AddressInfo>' $asciichar(10)}">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<AddressInfo type="bill">' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Name>' $Orders.d.bill_fname$ ' ' $Orders.d.bill_lname$ '</Name>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Company>' $Orders.d.bill_comp$ '</Company>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Address1>' $Orders.d.bill_addr$ '</Address1>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Address2></Address2>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<City>' $Orders.d.bill_city$ '</City>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<State>' $Orders.d.bill_state$ '</State>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Country>' $Orders.d.bill_cntry$ '</Country>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Zip>' $Orders.d.bill_zip$ '</Zip>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Phone>' $Orders.d.bill_phone$ '</Phone>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Email>' $Orders.d.bill_email$ '</Email>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '</AddressInfo>' $asciichar(10) }">
        	
        	<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.found" VALUE = "{ OrderCharge_FindFirst_Charge( Orders.d.id ) }">
        		<MvWHILE EXPR = "{ l.found }">
        			<MvIF EXPR = "{ ( OrderCharges.d.type EQ 'SHIPPING' ) }">
        				<MvASSIGN NAME = "l.xml_shipping_charge" VALUE = "{ rnd(OrderCharges.d.amount, 2) }">
        				<MvASSIGN NAME = "l.xml_s_d" VALUE = "{ rtrim(ltrim(gettoken(OrderCharges.d.desc, ':', 2)))}">
        				<MvASSIGN NAME = "l.xml_shipping_desc" VALUE = "{ rtrim(ltrim(gettoken(l.xml_s_d, '-', 1)))}">
        			</MvIF>
        			<MvIF EXPR = "{ ( OrderCharges.d.type EQ 'TAX' ) }">
        				<MvASSIGN NAME = "l.xml_tax_charge" VALUE = "{ OrderCharges.d.amount }">
        			</MvIF>
        			<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.found" VALUE = "{ OrderCharge_FindNext_Charge( Orders.d.id ) }">
        		</MvWHILE>
        
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<ShippingMethod>' $l.xml_shipping_desc$ '</ShippingMethod>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<ShippingMerchant>' $ l.merchant_shipname $ '</ShippingMerchant>' $asciichar(10) }">
        
        	
        
        	<MvASSIGN NAME = "l.xml_item_cnt" VALUE = "0">
        	<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.found" VALUE = "{ OrderItem_FindFirst_Order(Orders.d.id) }">
        	<MvWHILE EXPR = "{ l.found }">
        		<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Item num="' $l.xml_item_cnt$ '">' $asciichar(10)}">
        		<MvASSIGN NAME = "l.xml_item_cnt" VALUE = "{ l.xml_item_cnt + 1}">
        		<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<ProductId>' $OrderItems.d.code$ '</ProductId>' $asciichar(10) }">
        		<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Quantity>' $OrderItems.d.Quantity$ '</Quantity>' $asciichar(10) }">
        		<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Unit-Price>' $OrderItems.d.price$ '</Unit-Price>' $asciichar(10) }">
        		<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '</Item>' $asciichar(10) }">
        		<MvASSIGN NAME = "l.xml_sub_total" VALUE = "{ l.xml_sub_total + (OrderItems.d.price * OrderItems.d.quantity) }">
        		<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.found" VALUE = "{ OrderItem_FindNext_Order(Orders.d.id) }">
        	</MvWHILE>
        
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Totals>' $asciichar(10) }">
        	<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.found" VALUE = "{ OrderCharge_FindFirst_Charge( Orders.d.id ) }">
        	<MvWHILE EXPR = "{ l.found }">
        		<MvIF EXPR = "{ ( OrderCharges.d.type EQ 'TAX' ) }">
        			<MvASSIGN NAME = "l.xml_tax_charge" VALUE = "{ OrderCharges.d.amount }">
        		</MvIF>
        		<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.found" VALUE = "{ OrderCharge_FindNext_Charge( Orders.d.id ) }">
        	</MvWHILE>
        	
        
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Shipping>' $l.xml_shipping_charge$ '</Shipping>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Tax>' $l.xml_tax_charge$ '</Tax>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<SubTotal>' $l.xml_sub_total$ '</SubTotal>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<GrandTotal>' $Orders.d.total$ '</GrandTotal>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '</Totals>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '</Order>' $asciichar(10) }">
        	<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '</OrderList>' $asciichar(10) }">
        
        
        then some other conditionals....
        
        		<MvCALL METHOD = "POST" ACTION = "your-url-call" FIELDS = "XML">
        			
        		</MvCALL>
        Last edited by Bruce - PhosphorMedia; 11-19-15, 03:54 PM.
        Bruce Golub
        Phosphor Media - "Your Success is our Business"

        Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
        phosphormedia.com

        Comment


          #5
          Re: Iterate through order and create nested value/key pairs for output via MVT:Call

          I will give this a try! Many thanks. Do I need to adjust it at all since I am using Miva 9/mvt: based code (ex. Mvt:assign).

          Regards,

          Kelly

          Comment


            #6
            Re: Iterate through order and create nested value/key pairs for output via MVT:Call

            Yes, this is mivascript. I didn't look through the entire content of the function, but i think you could do it in SMT code.
            Bruce Golub
            Phosphor Media - "Your Success is our Business"

            Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
            phosphormedia.com

            Comment


              #7
              Re: Iterate through order and create nested value/key pairs for output via MVT:Call

              Thanks Bruce. I am newer to MScript. Where is the loop to list our each item? I believe its this:

              Code:
              <MvASSIGN NAME = "l.xml_item_cnt" VALUE = "0">
              	<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.found" VALUE = "{ OrderItem_FindFirst_Order(Orders.d.id) }">
              	<MvWHILE EXPR = "{ l.found }">
              		<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Item num="' $l.xml_item_cnt$ '">' $asciichar(10)}">
              		<MvASSIGN NAME = "l.xml_item_cnt" VALUE = "{ l.xml_item_cnt + 1}">
              		<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<ProductId>' $OrderItems.d.code$ '</ProductId>' $asciichar(10) }">
              		<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Quantity>' $OrderItems.d.Quantity$ '</Quantity>' $asciichar(10) }">
              		<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '<Unit-Price>' $OrderItems.d.price$ '</Unit-Price>' $asciichar(10) }">
              		<MvASSIGN NAME="l.xml" VALUE="{ l.xml $ '</Item>' $asciichar(10) }">
              		<MvASSIGN NAME = "l.xml_sub_total" VALUE = "{ l.xml_sub_total + (OrderItems.d.price * OrderItems.d.quantity) }">
              		<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.found" VALUE = "{ OrderItem_FindNext_Order(Orders.d.id) }">
              	</MvWHILE>
              Originally posted by Bruce - PhosphorMedia View Post
              Yes, this is mivascript. I didn't look through the entire content of the function, but i think you could do it in SMT code.

              Comment


                #8
                Re: Iterate through order and create nested value/key pairs for output via MVT:Call

                Yes, and it assumes you are doing this during fulfillment, with in an order content template (and associated item).
                Bruce Golub
                Phosphor Media - "Your Success is our Business"

                Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
                phosphormedia.com

                Comment


                  #9
                  Re: Iterate through order and create nested value/key pairs for output via MVT:Call

                  Thanks, it is on my Invoice page so the order data should be intact.
                  Originally posted by Bruce - PhosphorMedia View Post
                  Yes, and it assumes you are doing this during fulfillment, with in an order content template (and associated item).

                  Comment


                    #10
                    Re: Iterate through order and create nested value/key pairs for output via MVT:Call

                    Hi Bruce,

                    How would I do this using Miva Template Language?

                    Ex. <mvt:assignname="l.xml" value="'<?xml version=\"1.0\"encoding=\"ISO-8859-1\"?>' $ asciichar(10)" />

                    Thanks!

                    Kelly

                    Originally posted by Bruce - PhosphorMedia View Post
                    Yes, and it assumes you are doing this during fulfillment, with in an order content template (and associated item).

                    Comment

                    Working...
                    X