Zen Cart Logo
Forums / General Questions / Posting directly to the cart via a new page(script)

Posting directly to the cart via a new page(script)

Views: 700

Results 1 to 8 of 8
3 Apr 2012, 5:56 PM
#1
btopol avatar

btopol

New Zenner

Join Date:
Apr 2012
Posts:
4
Plugin Contributions:
0

Posting directly to the cart via a new page(script)

Hi,

I've created a new page where I wrote the following sql to post directly to the cart

$sql_cart = "insert into " . TABLE_CUSTOMERS_BASKET . "
(customers_id, products_id, customers_basket_quantity,
customers_basket_date_added)
values ('" . (int)$_SESSION['customer_id'] . "', '".$product_id."', '" .
"1', '" . date('Ymd') . "')";

The quantity will always be 1 in my case.

The script updates the cart correctly in the db, however, I do not see the changes in cart index.php?main_page=shopping_cart unless I logout and log back in.

Can anyone help?

3 Apr 2012, 7:06 PM
#2
niccol avatar

niccol

Totally Zenned

Join Date:
Apr 2009
Posts:
2,138
Plugin Contributions:
1

Re: Posting directly to the cart via a new page(script)

The cart that is actually in use is stored in the SESSION variable. If the user is logged in then it is also copied to the database. So, from your point of view adding the product to the session is the critical thing.

Off the top of my head I can't remember the details but if you put

<?php

print_r($_SESSION);

?>

somewhere down at the bottom of the page then you should be able to work out exactly what you need to do.

3 Apr 2012, 8:44 PM
#3
btopol avatar

btopol

New Zenner

Join Date:
Apr 2012
Posts:
4
Plugin Contributions:
0

Re: Posting directly to the cart via a new page(script)

Thanks for the answer.
I get it but still do not know how to add the product to the session.

Here is the print out as you suggested.
is it new_products_id_in_cart?

Would I need to add $_SESSION['cart']->new_products_id_in_cart = '$product_id'; ?

Array ( [securityToken] => bcafcdf4b02f791b5fff6bf968faba5a [customers_host_address] => adsl-75-38-108-14.dsl.irvnca.sbcglobal.net [cartID] => [cart] => shoppingCart Object ( [contents] => Array ( [102] => Array ( [qty] => 1 ) [100] => Array ( [qty] => 1 ) ) [total] => 150 [weight] => 0 [cartID] => 27584 [content_type] => [free_shipping_item] => 1 [free_shipping_weight] => 0 [free_shipping_price] => 75 ) [navigation] => navigationHistory Object ( [path] => Array ( [0] => Array ( [page] => logoff [mode] => NONSSL [get] => [post] => Array ( ) ) [1] => Array ( [page] => login [mode] => NONSSL [get] => Array ( [action] => process ) [post] => Array ( ) ) [2] => Array ( [page] => index [mode] => NONSSL [get] => [post] => Array ( ) ) [3] => Array ( [page] => page_4 [mode] => NONSSL [get] => [post] => Array ( ) ) [4] => Array ( [page] => shopping_cart [mode] => NONSSL [get] => [post] => Array ( ) ) ) [snapshot] => Array ( ) ) [check_valid] => true [language] => english [languages_id] => 1 [languages_code] => en [currency] => USD [today_is] => 2012-04-03 [updateExpirations] => 1 [session_counter] => 1 [customers_ip_address] => 75.38.108.14 [customer_id] => 2 [customer_default_address_id] => 2 [customers_authorization] => 0 [customer_first_name] => Cindi [customer_last_name] => Topol [customer_country_id] => 223 [customer_zone_id] => 43 [valid_to_checkout] => 1 [cart_errors] => [new_products_id_in_cart] => 100 )

3 Apr 2012, 9:09 PM
#4
btopol avatar

btopol

New Zenner

Join Date:
Apr 2012
Posts:
4
Plugin Contributions:
0

Re: Posting directly to the cart via a new page(script)

I got it. Thanks for your help

$_SESSION['cart']->contents['59']['qty'] = 1;

3 Apr 2012, 9:30 PM
#5
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Posting directly to the cart via a new page(script)

The approach you're taking is dangerous, since it bypasses proper checks and balances and safety validations, as well as bypasses built-in hooks for plugins that you might be better off using.

You'd be far better off to use the smarter method of calling $_SESSION['cart']->add_product() or simply giving the customer a suitable button to click to accomplish the same thing (examples the built-in add-to-cart buttons on all the product and product-listing pages).

But A VERY IMPORTANT THING you've not mentioned here is the WHY behind what you're trying to do. What's so special about this particular page that makes you want to do as you've described? What's the business problem you're trying to solve by using the method you've posted about? Perhaps there are smarter and more efficient ways of accomplishing the same thing that will maybe even survive upgrades easier and be more adaptable to future needs, etc.

3 Apr 2012, 9:40 PM
#6
schoolboy avatar

schoolboy

Totally Zenned

Join Date:
Jun 2005
Location:
Cumbria, UK
Posts:
10,327
Plugin Contributions:
0

Re: Posting directly to the cart via a new page(script)

What about simple hyperlinks?

<a href="index.php?main_page=index&action=buy_now&products_id=xxx">ANCHOR TEXT OR IMG</a>

4 Apr 2012, 4:09 AM
#7
btopol avatar

btopol

New Zenner

Join Date:
Apr 2012
Posts:
4
Plugin Contributions:
0

Re: Posting directly to the cart via a new page(script)

What I'm trying to is give users an option to buy a swimming lessen for their children. This is divided to categories and levels.
Each child can only have one.
The children's names come from a different db.

Once they add the activity(product) to the cart I let zen do the rest.

Is there a better way to do this? What would you suggest?