I'm trying to replace the current WooCommerce cart with a saved one, during Ajax.
This is the code i'm using:
function restore()
{
$current_user_id = get_current_user_id();
if ($current_user_id > 0) {
if (function_exists('get_user_attribute')) {
$buy_now_persistent_cart = get_user_meta($current_user_id, '_buy_now_persistent_cart', true);
error_log(print_r($buy_now_persistent_cart, 1));
if (!empty($buy_now_persistent_cart['cart'])) {
WC()->session->cart = $buy_now_persistent_cart['cart'];
WC()->session->applied_coupons = $buy_now_persistent_cart['applied_coupons'];
if (function_exists('delete_user_attribute')) {
//delete_user_attribute( $current_user_id, '_buy_now_persistent_cart' );
}
//delete_user_meta( $current_user_id, '_buy_now_persistent_cart' );
}
}
}
This is the $buy_now_persistent_cart output:
Array
(
[cart] => Array
(
[3430dcf4efe8aa0c418434656773a73a] => Array
(
[key] => 3430dcf4efe8aa0c418434656773a73a
[product_id] => 126096
[variation_id] => 0
[variation] => Array
(
)
[quantity] => 1
[data_hash] => b5c1d5ca8bae6d4896cf1807cdf763f0
[line_tax_data] => Array
(
[subtotal] => Array
(
)
[total] => Array
(
)
)
[line_subtotal] => 2.4
[line_subtotal_tax] => 0
[line_total] => 2.4
[line_tax] => 0
)
)
[applied_coupons] => Array
(
)
)
When I output WC()->session->cart after setting the new data it seems to have changed correctly, but the updated cart is not there when the Ajax finishes.
I guess it is about saving the cart and/or setting a cart hash and cookies?
I just can't figure it out.
Any help will be appreciated.
I would approach it in a different way. First empty the user cart, then add all the products.
Something like:
if (!empty($buy_now_persistent_cart['cart'])) {
WC()->cart->empty_cart();
foreach($buy_now_persistent_cart['cart'] as $item){
WC()->cart->add_to_cart($item['product_id'], $item['quantity']);
}
}
I didn't tested it, but in general I think it's a better approach than forcing a session
Related
Is it possible to find the cakephp with additional condition or can we multiply the sum() field with conditional variable?
I don't know, what could be a best question to let you guys know what I'm asking. so I'm giving a more detail about question. I'm able to find similar data with single array with some trick. but I'm unable to find for this. Please help me or give me an idea to do such task. Thanks.
Using CakePhp 2.6
I want to find data from first table here..
$incentiveweekly=$this->Incentive->find('all',
array('conditions' =>
array(
"AND"=>array(
"Incentive.fromdate >=" => $from,
"Incentive.todate <=" => $to
)
)
)
);
according to number of rows. I have to find another table
Here is the result of above find condition.
Array
(
[Incentive] => Array
(
[id] => 2
[target1] => 3000
[price1] => 1.5
[target2] => 6000
[price2] => 2.5
[target3] => 8000
[price3] => 3.5
[formonth] =>
[type] => 1
[fromdate] => 2016-11-13
[todate] => 2016-11-21
[updatedby] => 1
[created] => 2016-11-15 23:57:21
[modified] => 2016-11-15 23:57:21
)
)
Array
(
[Incentive] => Array
(
[id] => 3
[target1] => 3000
[price1] => 1.5
[target2] => 6000
[price2] => 2.5
[target3] => 8000
[price3] => 3.5
[formonth] =>
[type] => 1
[fromdate] => 2016-11-24
[todate] => 2016-11-28
[updatedby] => 1
[created] => 2016-11-15 23:57:21
[modified] => 2016-11-15 23:57:21
)
)
Now I want to find the array according to number of array record.
$byweek=array(); // Storing Customer data by Target dates in array()
foreach ($incentiveweekly as $weekly){
print_r($weekly);
$target3=$weekly['Incentive']['target3'];
$target2=$weekly['Incentive']['target2'];
$target1=$weekly['Incentive']['target1'];
$price3=$weekly['Incentive']['price3'];
$price2=$weekly['Incentive']['price2'];
$price1=$weekly['Incentive']['price1'];
$byweek[]=$customers=$this->Customer->find('all',array(
'fields'=>array(
'SUM(amount) AS amount',
'created_by'
),
'group' => 'Customer.created_by',
'conditions' => array(
"AND" =>array(
"Customer.created >=" => $weekly['Incentive']['fromdate'],
"Customer.created <=" => $weekly['Incentive']['todate']
)
),
'recursive'=>-1 )
);
//print_r($byweek);
}
I'm getting result like ...
Array
(
[0] => Array
(
[0] => Array
(
[Customer] => Array
(
[created_by] => 3
)
)
)
)
Array
(
[0] => Array
(
[0] => Array
(
[Customer] => Array
(
[created_by] => 3
)
)
)
[1] => Array
(
[0] => Array
(
[Customer] => Array
(
[created_by] => 1
)
)
[1] => Array
(
[Customer] => Array
(
[created_by] => 2
)
)
)
)
But I want that amount would multiply with on the if else condition where I'm using ternary operator.
$value[0]['amount']>=$valuem['Incentive']['target3']?"Target Third":($value[0]['amount']>=$valuem['Incentive']['target2']?"Target Second":($value[0]['amount']>=$valuem['Incentive']['target1']?"Target First":"None"))
Main purpose to find the details are. I want to create an incentive amount where total sales amount should be match with given target amount. If target1 amount one match then incentive would be total amount*price1 and same with target2 and target3. where i'm using tenantry operator. Target price should be multiply(by condition) with the same find condition data.
I search on google and stack overflow but can't find it's solutions. However I'm thankful to stack overflow and all you gusy that i'm able to find a lots of trick and solutions.
I haven't got exactly though you can try with below solution as per my thought,
You need to use WHEN close in MYSQL,
$incenve = 'CASE WHEN amount>5000 THEN "FIRST Target" WHEN amount>3000 THEN "Second Target" ELSE 0 '
and inside fields in above query
'incentive' => $incentive
Thanks Oldskool for this solution Virtualfields with If Condition
Finaly I, Got my solutions by this code bellow. I'm getting result as expected. :)
$this->Customer->virtualFields=array(
'incentive' => "if(SUM(Customer.amount)>=$target3,(SUM(Customer.amount))*$price3,if(SUM(Customer.amount)>=$target2,(SUM(Customer.amount))*$price2,if(SUM(Customer.amount)>=$target1,(SUM(Customer.amount))*$price1,0)))",
);
Now I can use Virtualfied to get the Incentive value.
I'm writing a console app in Symfony 3, but for some reason my query results are coming back with only 1 result in the array where I confirmed running it in MySQL and get several results back. Here is the relevant code:
$arr_id = array();
$em = $this->getContainer()->get('doctrine')->getManager();
$dql2 = 'SELECT r.productId as pid, COUNT(r.id) as cnt
FROM AppBundle:ReferralTrack r
WHERE r.productId in (:in)
GROUP BY r.productId
ORDER BY cnt DESC';
// $arr_id is populated prior to the next command and is, for example, [1,2,3,4,5]
$rt = $em->createQuery($dql2)
->setParameters(
array(
'in' => implode(',', $arr_id),
)
)
;
$traffic_count = $rt->getResult();
When I run the query in MySql, the exact same query shown by $rt->getSql() and replace the ? in the query with what is echoed out from implode(',', $arr_id), I get a similar result like this:
echo print_r( $traffic_count, 1);
---------------------------------
Array
(
[0] => Array
(
[pid] => 11234
[cnt] => 21
)
)
Here is the query I run in MySQL:
SELECT r0_.product_id AS pid, COUNT( r0_.id ) AS cnt
FROM referral_track r0_
WHERE r0_.product_id
IN ( 11234, 57, 58, 60, 61, 9677, 11216, 11217, 11239, 11296 )
GROUP BY r0_.product_id
ORDER BY cnt DESC
and it returns 7 results. What I have noticed is that getResult() does seem to always return the LAST record, that is what is shown the the array there. What is going on?
Try:
$rt = $em->createQuery($dql2)
->setParameters(
array(
'in' => $arr_id,
)
)
;
I am new to unit testing in PHP and I'm having a little trouble. Whether that is because I'm using the Cake framework or because I'm used to the Java way, point it I'm having issues.
I'm writing tests for a Model function that gets called on the submit of a form. The function receives two parameters, which I think I'm passing through correctly, and a data object that is not received as a parameter. My question is how do I populate that "data" object? I keep getting and "undefined index" error when I run the tests.
I've tried both mocking the data and using fixtures, but in all honesty, I don't get this stuff. Below is my model function, followed by my test code.
public function isUniqueIfVerified($check, $unverified){
$found = false;
if ($this->data['Client']['client_type_id'] == 5) {
$found = $this->find ( 'first', array (
'conditions' => array (
$check,
$this->alias . '.' . $this->primaryKey . ' !=' => $this->id,
'client_type_id <>' => 5
),
'fields' => array (
'Client.id'
)
) );
} else {
$found = $this->find ( 'first', array (
'conditions' => array (
$check,
$this->alias . '.' . $this->primaryKey . ' !=' => $this->id
),
'fields' => array (
'Client.id'
)
) );
}
if ($found) {
return false;
} else {
return true;
}
}
This is like the 52 version of my test function, so feel free to just do whatever you want with it. I was thinking that mocking the data would be easier and faster, since I only really need the 'client_type_id' for the condition inside my Model function, but I couldn't get that 'data' object to work, so I switched to fixtures... with no success.
public function testIsUniqueIfVerified01() {
$this->Client = $this->getMock ( 'Client', array (
'find'
) );
$this->Client->set(array(
'client_type_id' => 1,
'identity_no' => 1234567890123
));
//$this->Client->log($this->Client->data);
$check = array (
'identity_no' => '1234567890123'
);
$unverified = null;
$this->Client = $this->getMockforModel("Client",array('find'));
$this->Client->expects($this->once())
->method("find")
->with('first', array (
'conditions' => array (
"identity_no" => "1234567890123",
"Client.id" => "7711883306236",
'client_type_id <>' => 5
),
'fields' => array (
'Client.id'
)
))
->will($this->returnValue(false));
$this->assertTrue($this->Client->isUniqueIfVerified($check, $unverified));
unset ( $this->Client );
}
Again, I'm very green when it comes to Cake, and more specifically PHP Unit Testing, so feel free to explain where I went wrong.
Thanks!
You'll need to make a slight adjustment to your model function (which I'll show below) but then you should be able to do something like this to pass through data in the data object:
$this->Client->data = array(
'Client' => array(
'client_type_id' => 5,
'identity_no' => 1234567890123
));
This is instead of the "set" you used, as below:
$this->Client->set(array( ...
Also, you mocked the Client model, then "set" a few things, but then just before you do the test, you mock it again. This means you're throwing away all the thins you set for the mock you did right at the top. You can do something as below which should solve you problem:
public function testIsUniqueIfVerified01() {
$this->Client = $this->getMock ( 'Client', array (
'find'
) );
$this->Client->data = array(
'Client' => array(
'client_type_id' => 5,
'identity_no' => 1234567890123
));
$check = array (
'identity_no' => '1234567890123'
);
$unverified = null;
$this->Client->expects($this->once())
->method("find")
->with($this->identicalTo('first'), $this->identicalTo(array(
'conditions' => array (
$check,
"Client.id !=" => 1,
'client_type_id <>' => 5
),
'fields' => array (
'Client.id'
)
)))
->will($this->returnValue(false));
$this->assertTrue($this->Client->isUniqueIfVerified($check, $unverified));
unset ( $this->Client );
}
This should at least give you an idea of what to do. Hope it helps!
I am trying to read all the status messages of a user. I have used the following code.
//gets statusid, message and uid of the user
function get_status_message($access_token){
// Run fql query
$fql_query_url = 'https://graph.facebook.com/'
. '/fql?q=SELECT+message+,+status_id+,+uid+FROM+status+WHERE+uid=me()'
. '&access_token=' . $access_token;
$fql_query_result = file_get_contents($fql_query_url);
$fql_query_obj = json_decode($fql_query_result, true);
//display results of fql query
echo '<pre>';
print_r("query results:");
print_r($fql_query_obj);
echo '</pre>';
return $fql_query_obj;
}
It returns the following
query results:Array
(
[data] => Array
(
[0] => Array
(
[message] => test3
[status_id] => 5.0606879274642E+14
[uid] => 1.0000029926744E+14
)
[1] => Array
(
[message] => test2
[status_id] => 5.060443560822E+14
[uid] => 1.0000029926744E+14
)
[2] => Array
(
[message] => testing !
[status_id] => 5.0304255638238E+14
[uid] => 1.0000029926744E+14
)
)
)
I have to retrieve the status id and put it in mysql db. I tried foreach but I am unable to loop. The code below works
but I need to explicitly give the first index value as $value*[0]*["status_id"]. Can someone please help
You need to check if the Facebook call actually retrieved data for you to process then loop through the result and insert to your DB, with something like:
if(!empty($fql_query_obj['data'])) {
foreach($fql_query_obj['data'] as $status) {
$status_id = $status['status_id'];
// create SQL insert batch
}
// Run the insert batch
}
I have a Problem with the Querybuilder,
$qb = $this->_em->createQueryBuilder();
$qb->select('k, l')
-> i got a array such as
Array
(
[0] => Array
(
[id] => 15
-> ok
when i do
$qb->select('k.id, l.title')->add('from', 'Base\Entities\Company k')
i get
Array
(
[0] => Array
(
[id] => 15
[title] =>
)
-> ok
ISSUE:
When id do:
$qb->select('k, l.title')->add('from', 'Base\Entities\Company k')
i get:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 15
i always do $result = $query->getArrayResult(); at the End
how can i remove the array in the array? i only want title as part of the array in k (like above)
I believe it's not really possible to do so, You'll have to iterate over the results to achieve this. You can read more about it in documentation.