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);
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.