Post by michael14 » Fri Apr 06, 2012 12:58 pm

How can I pass options into the add to cart function? I already know how to supply the product_id and the quantity, however, I do not know what information must be in the $options array to add product options to the product.

For example I have the following statement: $this->cart->add( $product_id, $qty = 1, $options = array());

What must be passed to the $options = array()) segment? I assume at least the option_id value must be provided.

An example demonstrating this would be awesome!
Thanks very much!! ;D

Newbie

Posts

Joined
Mon Mar 05, 2012 10:46 am

Post by Avvici » Fri Apr 06, 2012 1:50 pm

In the cart.php file in catalog/controller/checkout/cart.php you can see the main add function being called here:

Code: Select all

$this->cart->add($this->request->post['product_id'], $this->request->post['quantity'], $option);
The actual add function in system/library/cart.php meet's the 3 arguments here:

Code: Select all

public function add($product_id, $qty = 1, $options = array()) {
There you have your answer. It's the variable $option, which is already an array.

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by Qphoria » Fri Apr 06, 2012 8:59 pm

He's asking about the format of the array.

The array can be a few different ways, depending on the option type.

Selectbox & Radio:

Code: Select all

    [option] => Array
        (
            [252] => 78
        )
Checkbox:

Code: Select all

    [option] => Array
        (
            [253] => Array
                (
                    [0] => 93
                    [1] => 94
                    [2] => 95
                )

        )
Text, Date, TextArea:

Code: Select all

    [option] => Array
        (
            [254] => "my text value"
        )
the "252, 253, 254" are the option_ids from the option table
the checkbox values "78, 93, 94, 95" are the option_value_ids from the option_value table
Textboxes are simply stored as a string as entered from the customer.

Take a look at the system/library/cart.php to see how it handles each option type. It looks up the option_id to see what "type" of option it is, then uses the option values for the associated values.

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by michael14 » Fri Apr 06, 2012 10:28 pm

Thank you for your replies!! I am trying to pass checkbox options, however I am still unsuccessful in doing so.

The resulting JSON code shows that the options are still empty for the product:
----start var_dump ($options) for display purposes----
array
'option' =>
array
228 => int 52
----end var_dump ($options) for display purposes----
array
'60:YToxOntzOjY6Im9wdGlvbiI7YToxOntpOjIyODtpOjUy1200' =>
array
'key' => string '60:YToxOntzOjY6Im9wdGlvbiI7YToxOntpOjIyODtpOjUy1200' (length=51)
'product_id' => string '60' (length=2)
'name' => string 'Custom Set' (length=10)
'model' => string 'customset' (length=9)
'shipping' => string '1' (length=1)
'image' => string '' (length=0)
'option' =>
array
empty


I am using the correct ids from the database, so that shouldn't be the issue.

Help is much appreciated! Thanks again guys!! ;D

Newbie

Posts

Joined
Mon Mar 05, 2012 10:46 am

Post by antoncona » Wed May 22, 2013 5:41 pm

Any way to pass the Options array from a form. .like list..? How to add the options in this form?


<form action="index.php?route=checkout/cart" method="post">
<input type="text" name="product_id" value="66">
<input type="hidden" name="quantity" value="1">
<input type="submit" name="submit" value="submit">
</form>

Newbie

Posts

Joined
Wed Mar 06, 2013 2:55 am

Post by Avvici » Wed May 22, 2013 5:48 pm

antoncona wrote:Any way to pass the Options array from a form. .like list..? How to add the options in this form?


<form action="index.php?route=checkout/cart" method="post">
<input type="text" name="product_id" value="66">
<input type="hidden" name="quantity" value="1">
<input type="submit" name="submit" value="submit">
</form>
They already are passed from a form. All you have to do is use Qforia's array formation and look at product.tpl file as an example of the form set up. The options are stored in the $option variable:

Code: Select all

$this->cart->add($this->request->post['product_id'], $this->request->post['quantity'], $option);
In your checkout/cart.php file in the add() function you will see this code:

Code: Select all

if (isset($this->request->post['option'])) {
				$option = array_filter($this->request->post['option']);
			} else {
				$option = array();	
			}
This picks up the post data from product.tpl when people are adding options. This is all you need to know in order to fill the $option variable. Aagain, you'll need to look in the product.tpl to mimic how the html fields are set up.

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC
Who is online

Users browsing this forum: Ahrefs [Bot], Majestic-12 [Bot] and 76 guests