Jump to content
Killersites Forums

Php Cart Tweak Help


Recommended Posts

I have been working with Ben Falk’s PHP shopping cart series and everything works very well. I am trying to slightly customize the cart array so that it includes a second multiplier value.

 

I am planning to use this cart to place meal orders for a fitness and food service. As part of the service, a customer inputs their Procarb# in order to determine the serving size of their meal based on their lean body mass. So the array of numbers in the cart row that determines the total would be unit price x Procarb# x Quantity: I know that this is doable but I am having a hard time making this edit work as I am new to such dynamic designs.

 

Can anyone help me determine where and how this edit should be made?

 

-Ricky

Link to post
Share on other sites

I think it partially depends on where in the process you need the visitor to enter their procarb #. Do they have one number that's used on all products? Can that number change per product? Is that number entered when they first enter the store? When they first view a product? When they first add a product to the cart?

 

If I understand things correctly... this is how I would do it. I'd have a form field in your "add to cart" form that asks the customer for their # when they place a product in the cart. That field would be required, so they can't add an item to the cart without the number. When the form submits, you'd save the number in a session variable, and access that session variable when calculating the price of items in the cart. Perhaps if you wanted to be fancy, you could use some Javascript/jQuery and AJAX to retrieve

 

Once that number is saved in the session variable, you'd automatically want fill out the number field on any other product pages, so they don't have to continually re-enter the number. If they decide to change the number and it no longer matches what has been stored in the session variable, you'd want to update the session variable with the new number.

 

I imagine you'd likely want to do most of the code changing within the m_cart.php model. You'll want to add methods to set, update and retrieve the procarb session variable, update the get_total_cost() method, and of course adjust the views and controllers to allow a visitor to enter their procarb number and save it.

Link to post
Share on other sites

Thanks very much for responding!

I would like to integrate the Procarb# very much the same as the quantity number.

 

I would like it to add to the cart session array, with a value of 30, by default when an item is added. Then, the user can update either value.

 

One difference is that if a user adds the same product to the cart Id like to generate a new cart row so that a user would be able to submit one row of product A x Procarb# 30 x Quantity: 5. and another row of product A x Procarb# 20 x quantity 5.

 

I think if I better understood the whole life cycle of the Quantity value I could recreate it better. So far I have made updates to the cart model so that I see the heading Procarb# next to Quantity in the rendered cart, there are two inputs there but only one works. I am hitting this message:

 

Notice: Undefined variable: procarb in C:\xampp\htdocs\phpcartoopmvc-projectfiles\app\models\m_cart.php on line 253

 

Notice: Undefined index: in C:\xampp\htdocs\phpcartoopmvc-projectfiles\app\models\m_cart.php on line 253

 

Notice: Undefined variable: procarb in C:\xampp\htdocs\phpcartoopmvc-projectfiles\app\models\m_cart.php on line 256

 

Notice: Undefined index: in C:\xampp\htdocs\phpcartoopmvc-projectfiles\app\models\m_cart.php on line 256

 

 

 

So I am wondering how/where the variable 'product' is defined as the quantity when adding an item to the cart, and how I can make procarb generate in the exact same way and be a first multiplier in calculating the total and allow the user to adjust both numbers in the cart.

Edited by Ricky
Link to post
Share on other sites

Here are some edits I have been trying if you need a laugh.

 

/**

* Adds item to the cart

*

* @access public

* @param int, int,int

* @return null

*/

public function add($id, $num = 1, $pronum = 30)

{

// setup or retrieve cart

$cart = array();

if (isset($_SESSION['cart']))

{

$cart = $_SESSION['cart'];

}

 

// check to see if item is already in cart

if (isset($cart[$id]))

{

// if item is in cart

$cart[$id] = $cart[$id] + $num ;

}

else

{

// if item is not in cart

$cart[$id] = $num && $pronum;

}

 

$_SESSION['cart'] = $cart;

}

 

/**

* Update the quantity of a specific item in the cart

*

* @access public

* @param int, int, int

* @return NULL

*/

public function update($id, $num, $pronum)

{

if ($num == 0)

{

unset($_SESSION['cart'][$id]);

}

else

{

$_SESSION['cart'][$id] = $num;

}

if ($pronum == 0)

{

unset($_SESSION['cart'][$id]);

}

else

{

$_SESSION['cart'][$id] = $pronum;

}

}

Link to post
Share on other sites

Create page parts

*/

/**

* Return a string, containing list items for each product in cart

*

* @access public

* @param

* @return array

*/

public function create_cart()

{

// get products currently in cart

$products = $this->get();

 

$data = '';

$form = '';

$total = 0;

 

$data .= '<li class="header_row"><div class="col1">Product Name:</div><div class="col2">Procarb#:</div><div class="col3">Quantity:</div><div class="col4">Meal Price:</div><div class="col5">Total Price:</div></li>';

 

if ($products != '')

{

// products to display

$line = 1;

$shipping = 0;

 

foreach($products as $product)

{

$item_shipping = $this->get_shipping_cost($product['price']) * $_SESSION['cart'][$product['id']];

$shipping += $item_shipping;

 

// create new item in cart

$data .= '<li';

if ($line % 2 == 0)

{

$data .= ' class="alt"';

}

$data .= '><div class="col1">' . $product['name'] . '</div>';

$data .= '<div class="col2"><input name="procarb' . $product['id'] .'" value="'. $_SESSION['cart'][$procarb['id']] .'"></div>';

$data .= '<div class="col3"><input name="product' . $product['id'] .'" value="'. $_SESSION['cart'][$product['id']] .'"></div>';

$data .= '<div class="col4">$' . $product['price'] . '</div>';

$data .= '<div class="col5">$' . $product['price'] * $_SESSION['cart'][$product['id']]* $_SESSION['cart'][$procarb['id']] . '</div></li>';

 

// create PayPal form fields

$form .= '<input type="hidden" name="item_name_' . $line . '" value="' . $product['name'] . '">';

$form .= '<input type="hidden" name="amount_' . $line . '" value="' . number_format($product['price'], 2) . '">';

$form .= '<input type="hidden" name="quantity_' . $line . '" value="' . $_SESSION['cart'][$product['id']] . '">';

$form .= '<input type="hidden" name="shipping_' . $line . '" value="' . $item_shipping . '">';

 

$total += $product['price'] * $_SESSION['cart'][$product['id']];

$line++;

}

Link to post
Share on other sites

I'm not quite sure what you are doing here:

 

$cart[$id] = $num && $pronum;

 

That's not really valid code, as far as I can tell.

 

You could do something like this instead?

 

$cart[$id]['num'] = $num;

$cart[$id]['pronum'] = $pronum;

 

Basically, that's the way I'd approach it. Currently, the way the system stores item in the cart is within a $cart array, that is then saved to a session variable. The array key represents the id of the product, and the value represents the number of that item in the cart:

 

$cart[key] = value;

 

I'd modify it so that $cart[key] holds an associative array instead, so you can store separate values for the number of that item in the cart, and also the procarb number. This would allow you to have separate procarb numbers per product, rather than one global one that works for all products. This means you'd need to update any place in the cart model that uses $cart[$id] to use $cart[$id]['num'], and then use $cart[$id]['pronum'] anytime you want to store the procarb number related to that product.

 

As for your error messages, they are exactly what they describe: in line 253 in m_cart.php, you are trying to access a variable that doesn't exist. My guess is that you likely are trying to use $procarb, when you should be accessing $pronum instead.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...