I'm using a Magento extension which sends shipments automatically to my carrier. Now I want the 'custom_id' field to be a comma separated list of the product sku's in the shipment.
First part of the code:
$items = $myParcelShipment->getOrder()->getAllItems();
foreach($items as $item) {
$sku= $item->getSku();
}
$CUSTOMLIST = [HERE A COMMA SEPARATED LIST OF PRODUCT SKU'S]
Second part:
$data = array(
'ToAddress' => array(
'name' => trim($shippingAddress->getName()),
'postcode' => trim($shippingAddress->getPostcode()),
'street' => trim($streetData['streetname']),
'house_number' => trim($streetData['housenumber']),
'town' => $shippingAddress->getCity(),
),
'ProductCode' => $this->_getProductCodeData($myParcelShipment),
'custom_id' => $CUSTOMLIST,
);
Any suggestions on how to transform the foreach loop into a comma separated string to be used as 'custom_id'?
Use following code
$skus = array();
$items = $myParcelShipment->getOrder()->getAllItems();
foreach($items as $item) {
$skus[] = $item->getSku();
}
$CUSTOMLIST = implode(',', $skus);
Related
I have following foreach loop
$selectedids = "1255;1256;1257";
$selectedidsarr = explode(';', $selectedids);
$idstand = '1';
foreach ($selectedidsarr as $item) {
$output1 = $idstand++;
echo "<li>product_id_$output1 = $item,</li>";
}
I want to add the output of the above loop inside following associative array
$paramas = array(
'loginId' => $cred1,
'password' => $credpass1,
'orderId' => $orderid,
'offer' => $offerid,
'shipid' => $shipcharge
)
So that the final array will look like this;
$paramas = array(
'loginId' => $cred1,
'password' => $credpass1,
'orderId' => $orderid,
'offer' => $offerid,
'shipid' => $shipcharge,
'product1_id' => 1255,
'product2_id' => 1256,
'product3_id' => 1257,
)
I tried creating following solution but its not working for me
$selectedids = $boughtitem;
$selectedidsarr = explode(';', $selectedids);
$idstand = '1';
foreach ($selectedidsarr as $item) {
$idoutput1 = $idstand++;
$paramas [] = array (
'product$idoutput1_id' => $item,
);
}
Need advice.
You don't need to define a new array, just set the key of the current array to the value you want, in the form of $array[$key] = $value to get an array that looks like [$key=>$value], or in your case...
$paramas['product' . $idoutput1 . '_id'] = $item;
I have two table one for sections where each section has many questions when i echo i got the section text but the questions as index value 0 1 2
<?php
$result = mysql_query($query);
if ($result) {
$data = array();
while ($row = mysql_fetch_assoc($result)) {
$data[($row['SECTION_NAME'])][][($row['QUES_TEXT'])][] = array(
'SECTION' => $row['SECTION_NAME'],
'QUESTION' => $row['QUES_TEXT']
);
}
foreach ($data as $SECTION => $QUESTIONS) {
echo '<h2>',htmlentities($SECTION),'</h2>';
foreach ($QUESTIONS as $QUESTIONS_TEXT => $TEXT) {
echo '<h2>',($QUESTIONS_TEXT),'</h2>';
}
}
}
?>
You are creating extra nested arrays and also the parens are unneeded:
$data[$row['SECTION_NAME']][][$row['QUES_TEXT']][] = array(
'SECTION' => $row['SECTION_NAME'],
'QUESTION' => $row['QUES_TEXT']
);
Should be:
if (empty($data[$row['SECTION_NAME']]) {
$data[$row['SECTION_NAME']] = array();
}
$data[$row['SECTION_NAME']][$row['QUES_TEXT']] = array(
'SECTION' => $row['SECTION_NAME'],
'QUESTION' => $row['QUES_TEXT']
);
Also please see the comments to your original question, this is no longer a safe way to use MySQL from PHP.
I'm trying to update the website and category IDs for a number of products in Magento and I'm having issues. Here's my code:
$client = new SoapClient('http://magentohost/api/v2_soap/?wsdl');
$session = $client->login('apiUser', 'apiKey');
$productarray = array("12345" => "1,2,3", "67890" => "1,5,6");
foreach ($productarray as $product_id => $cats) {
$update = array(
'websites' => array(1,2,3),
'categories' => array($cats)
);
$updatewebsite = $client->catalogProductUpdate($session,$product_id,$update);
}
When I run this code, it's changing the products to have the new website IDs but it's only updating the category IDs with the first one in $cats.
For example, "12345" will only have category ID 1 and not 2 or 3 as it should have.
When I print out what $cats is for each product, it's showing me the info correctly (as "1,2,3" and "1,5,6" for the examples above).
I'm not sure what I've done but I just can't seem to get it to update all the category IDs. I've got thousands of products to run through that all have different category IDs so I can't be doing this manually!
EDIT - SOLVED
I've changed my code so it's now like this (which works):
$client = new SoapClient('http://magentohost/api/v2_soap/?wsdl');
$session = $client->login('apiUser', 'apiKey');
$productarray = array("12345" => "1,2,3", "67890" => "1,5,6");
foreach ($productarray as $product_id => $cats) {
$cats = explode(",", $cats);
$update = array(
'websites' => array(1,2,3),
'categories' => $cats
);
$updatewebsite = $client->catalogProductUpdate($session,$product_id,$update);
}
The categories property would expect an array of IDs, whereas you're passing a string. Try using explode() to break it up:
foreach ($productarray as $product_id => $cats) {
$update = array(
'websites' => array(1,2,3),
'categories' => explode(',', $cats)
);
$updatewebsite = $client->catalogProductUpdate($session,$product_id,$update);
}
I am fairly new to Zend Framework and have searched for days without finding an example for the following issue:
The result I am trying to achieve from the code below is a list that would have the main Category with the matching sub categories beneath it and loop through for all my main categories.
Example:
Appliances (Main Category)
Microwave
Stove
Electronics (Main Category)
Computer
Radio
Here is the code (again I have no idea how to do this; my thought process was to do nested foreach(), with the second foreach() getting the main category id from the first select):
// Get Categories with Sub Categories
$catid = 0;
$selectmain = $this->dbhInstance->select()
->from(array('a' => 'code_sub_category'),
array('b.id as mainid', 'b.site_category'))
->join(array('b' => 'site_categories'),
'b.id = a.site_category_id')
->group("b.id")
->order("b.site_category");
$selectsub = $this->dbhInstance->select()
->from(array('a' => 'code_sub_category'),
array('a.id as subid', 'a.sub_category', 'a.site_category_id'))
->join(array('b' => 'site_categories'),
'b.id = a.site_category_id')
->where("a.site_category_id = '" . $catid . "'")
->order("a.sub_category");
$fetch = $this->dbhInstance->fetchAll($selectmain);
$fetch2 = $this->dbhInstance->fetchAll($selectsub);
//var_dump($fetch2);
$items = array();
$items2 = array();
foreach ($fetch as $key => $value) {
$catid = $value['id'];
$items = array_merge($items, array($key => $value));
foreach ($fetch2 as $key => $value) {
$items = array_merge($items, array($key => $value));
}
$this->view->getsubcategories = $items;
}
$this->view->getmaincategories = $items;
//End FULL Categories for My Categories
Can you try this :
$selectCat = $this->dbhInstance->select()
->from(array('c' => 'site_categories'), array('c.id as id', 'c.site_category as text, 0 as parent'))
->order('c.site_category');
$selectSubCat = $this->dbhInstance->select()
->from(array('sc' => 'code_sub_category'), array('sc.id as id', 'sc.sub_category as text', 'sc.site_category_id as parent')
->order('sc.sub_category');
$select = $this->dbhInstance->select()->union(array($selectCat, $selectSubCat));
$statement = $this->dbhInstance->query($select);
$items = array();
$childs = array();
while( $row = $statement->fetch() ) {
$items[] = (object)array(
'id' => $row['id'],
'text' => $row['text'],
'parent' => $row['parent'],
'item' => array()
);
}
foreach($items as $item)
$childs[$item->parent][] = $item;
foreach($items as $item) if (isset($childs[$item->id]))
$item->item = $childs[$item->id];
if( !isset($childs[0]) ) $this->view->getmaincategories = $getmaincategories = array();
else
$this->view->getmaincategories = $getmaincategories = $childs[0];
// Display the result
Zend_Debug::dump($getmaincategories);
The result will be an array of objects, something like this :
array(
'Appliances' => array(
'Microwave',
'Stove'
),
'Electronics' => array(
'Computer',
'Radio'
)
)
Hope this help!
Regards,
Ahmed.
This seems like a simple challenge, but I'm struggling.
I want to retrieve records using a join query on two database tables and represent them as an array of arrays, whereby each of the elements in the root array is a parent record and each nested element represents a child record.
The SQL query is working fine, and it returns a set of rows in which the channel_key column is a grouping column.
Here's my attempt at populating the array structure from the rows:
$rows = $db->get_results($query);
$key = '';
$programmes = array();
foreach ($rows as $row) {
$programme = array(
'title' => $row->title,
'start' => $row->start,
'duration' => $row->duration
);
$programmes[] = $programme;
if ($key != $row->channel_key) {
$channels[] = array(
'key' => $row->channel_key,
'programme' => $programmes
);
$key = $row->channel_key;
$programmes = array();
}
}
Unfortunately this only populates the root level arrays (the ones that correspond to the parent records).
Any suggestions please?
Thanks,
Tim
You only have one programme in the $programmes array when you assign it to the channel.
An alternative would be to build the channel array with the channel_key.
e.g.
<?php
$channels = array();
foreach($rows as $row) {
// Create the channel node if it doesn't exist
if (!isset($channels[$row->channel_key])) {
$channels[$row->channel_key] = array(
'key' => $row->channel_key,
'programme' => array()
);
}
$programme = array(
'title' => $row->title,
'start' => $row->start,
'duration' => $row->duration
);
// Add to the existing channel node.
$channels[$row->channel_key]['programme'][] = $programme;
}
Simple solution can be.
$rows = $db->get_results($query);
$key = '';
$programmes = array();
foreach ($rows as $row) {
$programme = array(
'title' => $row->title,
'start' => $row->start,
'duration' => $row->duration
);
$programmes[$row->channel_key][] = $programme;
}