I am trying to use Andras Kende's CakePHP Shopping Cart in my own application, and I cant figure why the following changes will not work.
At the end of the order process, after paypal and everything, when the user is about to finalize the transaction, I would like to, if the user is a member/logged in, use $this->Auth->user('id')
to get their authenticated ID and attach it to the order that is about to be finalized. For the life that is left in me, I can't get it to work. This is what I am doing:
I have the following class:
public function review() {
$shop = $this->Session->read('Shop');
if(empty($shop)) {
$this->redirect('/shop/cart');
}
if ($this->request->is('post')) {
$this->loadModel('Order');
$this->Order->set($this->request->data);
if($this->Order->validates()) {
$order = $shop;
$order['Order']['user_id'] = (int)$this->Auth->user('id');
$order['Order']['status'] = 1;
var_dump($order);
if($shop['Order']['order_type'] == 'paypal') {
$paypal = $this->Paypal->ConfirmPayment($order['Order']['total']);
$ack = strtoupper($paypal['ACK']);
if($ack == 'SUCCESS' || $ack == 'SUCCESSWITHWARNING') {
$order['Order']['status'] = 2;
}
$order['Order']['authorization'] = $paypal['ACK'];
}
$save = $this->Order->saveAll($order, array('validate' => 'first'));
if($save) {
$this->set(compact('shop'));
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail();
$email->from($this->requestAction('/settings/getSettingValue/ADMIN_EMAIL'))
->cc($this->requestAction('/settings/getSettingValue/ADMIN_EMAIL'))
->to($shop['Order']['email'])
->subject('Shop Order')
->emailFormat('text')
->viewVars(array('shop' => $shop))
->send();
$this->redirect(array('action' => 'success'));
} else {
$errors = $this->Order->invalidFields();
$this->set(compact('errors'));
}
}
}
if(($shop['Order']['order_type'] == 'paypal') && !empty($shop['Paypal']['Details'])) {
$shop['Order']['first_name'] = $shop['Paypal']['Details']['FIRSTNAME'];
$shop['Order']['last_name'] = $shop['Paypal']['Details']['LASTNAME'];
$shop['Order']['email'] = $shop['Paypal']['Details']['EMAIL'];
$shop['Order']['phone'] = '888-888-8888';
$shop['Order']['billing_address'] = $shop['Paypal']['Details']['SHIPTOSTREET'];
$shop['Order']['billing_address2'] = '';
$shop['Order']['billing_city'] = $shop['Paypal']['Details']['SHIPTOCITY'];
$shop['Order']['billing_zip'] = $shop['Paypal']['Details']['SHIPTOZIP'];
$shop['Order']['billing_state'] = $shop['Paypal']['Details']['SHIPTOSTATE'];
$shop['Order']['billing_country'] = $shop['Paypal']['Details']['SHIPTOCOUNTRYNAME'];
$shop['Order']['shipping_address'] = $shop['Paypal']['Details']['SHIPTOSTREET'];
$shop['Order']['shipping_address2'] = '';
$shop['Order']['shipping_city'] = $shop['Paypal']['Details']['SHIPTOCITY'];
$shop['Order']['shipping_zip'] = $shop['Paypal']['Details']['SHIPTOZIP'];
$shop['Order']['shipping_state'] = $shop['Paypal']['Details']['SHIPTOSTATE'];
$shop['Order']['shipping_country'] = $shop['Paypal']['Details']['SHIPTOCOUNTRYNAME'];
$shop['Order']['order_type'] = 'paypal';
$this->Session->write('Shop.Order', $shop['Order']);
}
$this->set(compact('shop'));
}
At line 11, I add the logged in user ID to the $order
array by doing the following:
$order['Order']['user_id'] = (int)$this->Auth->user('id');
At this point if I do a var_dump($order)
, I will see whatever user id is for the currently logged in user, in this case 18
. So I know for a fact that I am assigning it correctly.
["user_id"]=> int(18)
Everything is fine up to here. However, when I check my DB the user_id
field is NULL. 18
is not being saved as added to the array. I have done multiple things and I cant see what am I doing wrong. Please help!
My orders
table structure look like this:
And one of the order entry look like this:
I run out of ideas and I dont know what to do.....
After some more digging, I decided to change the Db field to user
instead of user_id
and it worked. Why isnt it taking user_id
. My user model $hasMany = array('Order'=>array('foreignKey' => 'user'))
. It would be STANDARD to use user_id
. I just cant figure out why it is not allowing it. So far the fix works!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.