Jump to content

I need to get multiple lines of html into a variable during a foreach


Maltechers

Recommended Posts

Alright the issue that I am having is that I need to get multiple lines into a variable to output later in the code. my $pieces variable is not returning anything, much less missing parts of it, but even the text code is not returning.

foreach($packages as $pack)
{
$weight+=$pack->weight;
$items_no++;
$singleweight=$pack->weight;
$length=$pack->length;
$height=$pack->height;
$width=$pack->width;

$pieces .= '<piece actual_weight="'.$singleweight.'" length="'.$length.'" height="'.$height.'" width="'.$width.'">';
}

if(!empty($post_code) && $weight>0 && $items_no>0)
{
$data='?shipment='.urlencode('<shipment weight_system="IMPERIAL" shipper_number="'.$this->shipper_number.'" destination_postal_code="'.$post_code.'" service_type="'.$service.'">'.$pieces.'</shipment>').'&token='.$this->token;
// $woocommerce->add_message('Items No:'.$items_no.'- Postalcode:'.$post_code.'- ');
$resp=file_get_contents($this->canpar_url.$data,false,$context);

If I do this in another method that only requires a single line (getting the total weight and number of items) it works fine, but this does not return the proper shipping cost for low weight items. On a site where higher weight items is shipped the plugin works fine. However I need to modify it to work based on dimensions. The result I am looking for is something like this:

https://www.canpar.com/XML/RatingXML.jsp?shipment=

<shipment weight_system="IMPERIAL" shipper_number="42500047" destination_postal_code="M1M1M1" service_type="1">

<piece actual_wieght="7" length="24" height="24" width="24"/>

<piece actual_wieght="5" length="10" height="12" width="14"/>

</shipment>

&token=WZHBRS4KQYNA

using an output system I can output weight, packs, length, width, height but it does not return anything for $pieces and obviously shipping costs because I am getting an invalid xml response.

$woocommerce->add_message('Weight: '.$weight.' Packs: '.$items_no.' Shipping cost : '.$atts['total_charge'].' Pieces: '.$item_box.' Length: '.$length.' Width: '.$width.' Height: '.$height);

Didn't follow all the of the code - too much to piece together since we don't know how they are connected. But - have to ask - what exactly is a <piece> tag? I do not see that listed in my html reference (incl. 5).

This line should be generating the <pieces> XML elements for each item in the $packages array/object

$pieces .= '<piece actual_weight="'.$singleweight.'" length="'.$length.'" height="'.$height.'" width="'.$width.'">';

The .= stands for concatenation assignment operator.

 

If you echo $pieces after the foreach loop does it show the correct generated xml structure?

echo htmlentities($pieces); // apply htmlentities otherwise it will be treated as HTML and wont be displayed

I dont understand why the XML being sent in the url? What is the reason for this?

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.