How do I add a value to the $values array in Woocommerce? - arrays

I'm trying to get my own custo`m value in the $values array, but I'm having some trouble to find out where the array is actually build.
I think I need to edit class-wc-cart.php, but if I just add my variable to
$this->cart_contents[$cart_item_key] = apply_filters( 'woocommerce_add_cart_item', array_merge( $cart_item_data, array(
'product_id' => $product_id,
'variation_id' => $variation_id,
'variation' => $variation,
'quantity' => $quantity,
'data' => $product_data
) ), $cart_item_key );
I get an error. So I was wondering where the $values array is actually build.

Related

Remove array key from array in cakephp

print array
array(
'Order' => array(
'id' => '1',
'base_price' => '65',
'min_price' => '95',
)
)
Is it possible to remove the key('Order') when you retrieving data? if Not how can I use array_shift or end in one line and to prevent below error?
I am getting this error Only variables should be passed by reference when I remove the key from array.
$orders = array_shift or end ($this->Order->read(null, $id));
debug($orders);
You want only id from it then following code will help you
$arrOrderId=Set::extract("/Order/id",$data);
here $data is your array from where you want to delete this "Order" key.
You will get following array when you do debug($arrOrderId);
[0]=>1
if you want base_price then write following code
$arrOrderId=Set::extract("/Order/base_price",$data);
You can use Set functions for manipulating arrays:
Set::extract($array, 'Order');
Will output:
array(
'id' => '1',
'base_price' => '65',
'min_price' => '95',
)
If you need to do this on every output, you can override afterFind() method on your model.
Please see the docs:
http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::extract
http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::classicExtract

save data in separate array in each iteration

$this->layout='defaultall';
$id = $this->Auth->user('idUser');
$data= $this->Contact->find('all',array(
'conditions' => array('Contact.User_id' => $id)));
$this->set('contacts',$data);
$this->loadModel('Calllog');
/* for($i=1; $i < 2; $i++)
{
$mobile =$data[$i]['Contact']["mobileNo"];
$work =$data[$i]['Contact']["workNo"];
$home =$data[$i]['Contact']["homeNo"];*/
for($data as $datas){
$mobile= $datas['Contact']['mobileNo'];
$home= $datas['Contact']['homeNo'];
$work= $datas['Contact']['workNo'];
$recent=$this->Calllog->find('all',
array('order'=>'Calllog.idCallLog DESC',
'limit' => 1,
'conditions' => array('Calllog.mobileNo' => array($mobile,$work,$home ),
'Calllog.User_id' => $id
)));
//here array want to declare
$this->set('a',$recent);
//here i want to declare an another array which saves the data of every iteration..because i dont want override the data
sorry i am week in arrays.. and do tell me how can i then print the array ...if i want to display the 1st iteration result in my view page
If you just want to add new element to the array without deleting its content you can do that just like that:
$recent[] = 'new_string1';
or if you have more elements:
array_push($recent, 'new_string1', 'new_string2');
But in the future, please read manual: http://php.net/manual/en/function.array-push.php

CGridView show data from dataProvider

I have a data provider which generates content like:
array(
[0] => ParentObjects array(
Child1Object array(...),
Child2Object array(...),
)
)
The first level of this array will always be 1.
I want to show in a CGridView the content of both childs.
$dataProvider=new CActiveDataProvider('Campanii', array(
'criteria'=>array(
'with'=>array('stocs','produse'),
),
'pagination'=>array(
'pageSize'=>20,
),
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array('name'=>'Cod','value'=>'$data->stocs[0]->cod_produs'),
),
));
As you see, the column cod has the value array('name'=>'Cod','value'=>'$data->stocs[1]->cod_produs'). Notice that [0]. Instead of that zero, I want something like an iterator. In this case, it shows me the content of that attributes in a column.
Is this possible?
Yes, you can write any complex expression you want. However, for readability it is better to use an actual function. This example assumes PHP 5.3+, if your environment doesn't support that, just create a normal function instead of an anonymous one (or find a better environment).
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
array(
'name' => 'Cod',
// needed when returning HTML instead of plain text
'type' => 'raw',
// this function is called each time a cell needs to be rendered
'value' => function(Companii $data) {
$codes = array();
foreach($data->stocs as $stoc) {
$code = CHtml::encode($stoc->cod_produs);
$codes []= CHtml::tag('li', array(), $code);
}
$codes = implode("\n", $codes);
return CHtml::tag('ul', array(), $codes);
},
),
),
));

cakephp $this->Model->save() not updating

I'm trying to build an array and update a few fields in a loop.
This is my request->data
Array
(
[list] => Array
(
[4] => null
[2] => null
[3] => null
[5] => 3
)
)
It's the return value from a jquery serialized list. The key being the row id and the value being the rows parent_id.
So I loop through the array in the controller:
foreach ($this->request->data['list'] as $key => $value) {
(!isset($orders[$value])) ? $orders[$value]=0 : $orders[$value]++;
$data = array('id' => $key, 'parent_id' => (int)$value, 'display_order' => $orders[$value]);
$this->Category->save($data);
}
The $data array that I create in the loop is correct but the sql logs only shows SELECT COUNT(*) etc. for each row and no UPDATE commands.
I have tried all manner of ways to save this: using set() method, using $this->Category->id = $key; instead of directly adding the key in the data array.
Any ideas why this is not saving? I'm sure it is something simple...
i think u forgot the cake convention in the save method, you have to put all the field values inside an array with the Model name FirstCap too, something like this:
Array
(
[ModelName] => Array
(
[fieldname1] => 'value'
[fieldname2] => 'value'
)
)
Else you can use set for each and every value, and also youre forgetting to create one row for every insert youre making so try something like this:
foreach ($this->request->data['list'] as $key => $value) {
(!isset($orders[$value])) ? $orders[$value]=0 : $orders[$value]++;
$data = array( 'Category' => array('id' => $key, 'parent_id' => (int)$value, 'display_order' => $orders[$value]));
$this->Category->save($data);
}
Also you can set each and every id and then iterates over the values
foreach ($this->request->data['list'] as $key => $value) {
(!isset($orders[$value])) ? $orders[$value]=0 : $orders[$value]++;
$this->Category->id = $key;
$this->Category->set(array('parent_id' => (int)$value,
'display_order' => $orders[$value]
));
$this->Category->save();
}
try each and every answer but i think the last one will fit better at your problem.
From the CakePHP book.
When calling save in a loop, don’t forget to call create().
echo $data and match it with save() method's syntax.also do not slip to match data types of fields in table.

CakePHP: saving associated data

I'm trying to save IssueHistoryDescription, which belongsTo IssueHistory.
So IssueHistory hasMany IssueHistoryDescription. This has all been set in the model.
Yet, when I save this in IssueHistory, using $IssueHistory->save($data);
(With or without a $IssueHistory->create(); before...)
Array
(
[IssueHistory] => Array
(
[id] => 22
)
[IssueHistoryDescription] => Array
(
[old_description] => OLD
[description] => NEW
)
)
It doesn't work, nothing is saved.
When I try to use saveAssociated() I get an error:
Fatal error: Cannot use string offset as an array in /var/www/xdev/kipdomanager/cakephp/lib/Cake/Model/Model.php on line 2248
You can try this:
$data = array(
'IssueHistory' => array('id' => 2),
'IssueHistoryDescription' => array(
array('old_description' => 'OLD', 'description' => 'new')
)
);
$IssueHistory->create();
$IssueHistory->saveAll( $data );

Resources