In my symfony controller script, Im passing the quantity of my shopping cart. And im able to get the print_r as the following. The problem Im facing is I tried various ways to access the quantity for no success, can sombody help me on how to access this quantity element
public function detailsAction($id, Request $request)
{
$book = $this->getDoctrine()
->getRepository('AppBundle:Booklist')
->find($id);
print_r($request->request->get('form'));
die($request->request->get('form'));
return $this->render('books/details.html.twig', array('book' => $book, 'quantity' => 1));
}
OUTPUT with print_r
Array ( [quantity] => 1 [Add to Cart] => [_token] => Y8XHbH7lHphAFQ5lIjhQShoIleeSj9iGpen1rhxTBz4 ) Array
can somebody help me on accessing quantity??
You have dumped $request->request->get('form') and there is a quantity in it. It should be easy to get the quantity.
$form = $request->request->get('form');
var_dump($form['quantity']);
Related
I am attempting to store basket data in a session in Laravel 5 via AJAX but each time I try and push an item to the array stored as the value to 'basket', the existing 'basket' array is overwritten.
Below is my controller method:
public function add(Request $request)
{
$success = false;
if ($request->ajax()) {
$ticket = Ticket::find((int) $request->get('ticket_id'));
if ($ticket) {
if (!$request->session()->has('basket')) {
$request->session()->put('basket', []);
}
$item = [
'attraction.name' => $ticket->attraction->name,
'ticket.quantity' => $request->get('quantity'),
'ticket.price' => $ticket->price,
];
$request->session()->push('basket', [$ticket->id => $item]);
$success = true;
}
}
return response()->json(['success' => $success]);
}
An item is set in the basket session array on the first instance, but I am expecting it to add to this array rather than overwrite when I post more item data over to this method.
The end result I'm looking to achieve is this:
Array
(
[basket] => Array
(
[123] => Array
(
[attraction.name] => Attraction 1
[ticket.quantity] => 2
[ticket.price] => 14.5
)
[456] => Array
(
[attraction.name] => Attraction 2
[ticket.quantity] => 3
[ticket.price] => 12
)
)
)
I've tried using the Session facade equivalent methods, but to no avail :(
Any help would be greatly appreciated. Thanks :)
I had tried array_merge() but again my session was being overwritten. So, after putting my debug hat on yet again, I've found the reason why it was happening.
Session data was not persisting after each request. As my session wasn't being remembered, !$request->session()->has('basket') was always evaluating to true therefore my basket session was being set back to an empty array each time.
I needed to return my request as JSON using the below line in order for my session data to persist. Maybe this is because I have dataType: 'json' as a property in my AJAX call?
return response()->json($request);
I also took your advice and grabbed the basket session array, pushed new data to it and put back into session.
Sorted! Thank you both for your help :)
I experienced a similar issue and discovered that after adding values to the session you need to call
session()->save();
i m trying to to get the request values of form in zend framework
earlier i was using the post method
but the the iphone app is sending me data in get method .
how do i can use it has i was using the post values like
$post = array(
'id'=>'2',
'email'=>'4',
)
i want to get values also in this form value when a form is submitted .
i was using this to get post values
$post = $this->getRequest ()->getPost ();
and i tried this to get get method values
$post = $this->getRequest();
but
i get this error
Cannot use object of type Zend_Controller_Request_Http as array in
this is the full error message
"Zend_Controller_Request_Http Object ( [_paramSources:protected] =>
Array ( [0] => _GET [1] => _POST ) [_requestUri:protected] =>
/platinum-picks/p-picks/index/registration [_baseUrl:protected] =>
/platinum-picks/p-picks [_basePath:protected] => [_pathInfo:protected]
=> /index/registration [_params:protected] => Array ( [controller] => index [action] => registration [module] => default )
[_rawBody:protected] => [_aliases:protected] => Array ( )
[_dispatched:protected] => 1 [_module:protected] => default
[_moduleKey:protected] => module [_controller:protected] => index
[_controllerKey:protected] => controller [_action:protected] =>
registration [_actionKey:protected] => action ) Fatal error: Cannot
use object of type Zend_Controller_Request_Http as array in D:\Program
Files\Zend\Apache2\htdocs\platinum-picks\application\models\User.php
on line 267"
If I understand correctly, you want to get the values passed using GET method and you have done the same successfully with POST values. The only change you need to make in such a scenario is this:
$post = $this->getRequest()->getPost ();
becomes
$get = $this->getRequest()->getParams();
This will also return parameters from other sources:
Retrieves a merged array of parameters, with precedence of userland params (see setParam()), $_GET, $_POST (i.e., values in the userland params will take precedence over all others).
So refer to the manual if this may be a problem for you.
You can find the GET variables using for example:
$this->getRequest()->getParams();
Or, for a specific variable:
$this->getRequest()->getParam('myVar');
This will also search for POST values. To search the GET vars only, use:
$this->getRequest()->getQuery('myVar');
I've got the following code:
$filters = array('sku' => array('like'=>'%'));
$items = $magConn->call($sessionID, 'product.list', array($filters));
This will return an array of all the products and their sku, description, and qty.
However, I also need to get the price? Is there a way to get that as well?
I've also got this working,
$properties = ($magConn->call($sessionID, 'product.info', $item['sku']));
which will return all the attributes for one product. I've got over 2,000 products, and this is definitely not feasible if I want it to end tonight. ;)
No way without magento source code modification. You should go to \app\code\core\Mage\Catalog\Model\Product\Api.php, find next lines inside items() method:
$result[] = array( // Basic product data
'product_id' => $product->getId(),
'sku' => $product->getSku(),
'name' => $product->getName(),
'set' => $product->getAttributeSetId(),
'type' => $product->getTypeId(),
'category_ids' => $product->getCategoryIds()
);
and add price here.
Load the collection :
$product = Mage::getModel('catalog/product')->load($productId);
Get actual price :
$product->getPrice();
Get special price :
$product->getFinalPrice();
I'm trying to reach an element in an object array. But I couldn't succeed.
Let's think this as an object called $result
How can I reach maskedNumber?
Braintree_Result_Successful Object
(
[success] => 1
[_returnObjectName:private] => customer
[customer] => Braintree_Customer Object
(
[_attributes:protected] => Array
(
[creditCards] => Array
(
[0] => Braintree_CreditCard Object
(
[_attributes] => Array
(
[maskedNumber] => ***********5897
Since the _attributes property of Braintree_Customer is protected you will need to define an accessor method. The other _attributes property of Braintree_CreditCard also looks like it's supposed to be protected, so I've assumed an identical accessor should exist:
$cards = $object->customer->getAttribute('creditCards');
$number = $cards[0]->getAttribute('maskedNumber');
Accessor method to be placed in both classes:
function getAttribute($attribute) {
return $this->_attributes[$attribute];
}
Edit:
Just to improve upon my original answer a little, I would put some decent error checking in an actual accessor method.
function getAttribute($attribute) {
if (isset($this->_attributes[$attribute])) {
return $this->_attributes[$attribute];
}
return NULL;
}
You could also consider using the magic methods __get() and __set() to act as getters and setters.
I solved the problem by asking braintreepayments. They said that I could retrieve this data after I add user to braintree. But my solution is if I really needed this at the very beginning would be to take it with REGEX. For people who are looking for a great online payment company I suggest you to go with braintree
I know this is old, but this might help some.
Braintree has methods for returning the protected information that's returned from functions like Braintree_Customer::create();
$result = Braintree_Customer::create(array(
'firstName' => $_POST['first_name'],
'lastName' => $_POST['last_name'],
'email' => $_POST['email'],
'creditCard' => array(
'cardholderName' => $_POST['cardholder_name'],
'number' => $_POST['number'],
'expirationMonth' => $_POST['month'],
'expirationYear' => $_POST['year'],
'cvv' => $_POST['cvv'],
'billingAddress' => array(
'postalCode' => $_POST['postal_code']
)
)
));
var_dump($result->customer->__get('id'));
var_dump($result->customer->__get('creditCards'));
The _attributes of customer are protected, but the get function returns them.
This method does not require re-requesting data from Braintree.
try
$Result->customer->_attributes['creditCards'][0]->_attributes['maskedNumber']
with $result->customer you should get the *Braintree_Customer* Object and then in that object you should have methods to retrieve the cards as those methods are protected and cannot be accessed directly. Something like
$customer = $result->customer;
foreach($customer->getCreditCards() as $card)
{
echo $card->getMaskedNumber(); // You will need to create that method too
}
Example of getCreditCards method:
Class Braintree_Customer
{
protected $creditCards;
function getCreditCards()
{
return $creditCards;
}
...
}
I am working on PHPunit tests and so far everything is great. There is one issue, however, and it is with the following setup. Ideally I'd like to select the next value in the sequence (postgreSQL) and pass that into the function so I can validate it against the array returned from my class being tested (new row in database).
The issue is that before the array is returned from the data provider (if I echo it) it is the correct value, but during the test it comes through as blank. Is there a particular series of steps I'm missing in here in terms of what I expect or must I do this a different way?
/**
* #dataProvider testSignupProvider
*/
public function testSignup($a, $b, $c)
{
...stuff is done with $a,$b,$c
}
public function testSignupProvider()
{
$uid = fetchOne(X("SELECT currval('users_id_seq')"));
return array(
array(false, array(), $error4),
array(
'email'=>'stack#overflow.com',
'password'=>'youaintgonnagetit',
$error3
),
array(
array('id'=>$uid,'email'=>'PHPUNIT#gmail.com','username'=>'Guest'),
array('email'=>'PHPUNIT#gmail.com','password'=>'youaintgonnagetit'),
null
)
);
}
Output:
Array
(
- [id] =>
+ [id] => 2
[email] => PHPUNIT#gmail.com
[username] => Guest
)
I've gotten around this by assigning the 'id' array element to '#id' and in the function test I pull the current id in the sequence at that time.
Still will accept an answer to anyone who can tell me why this behavior occurs but hopefully this will help anyone else with this issue.