Jump to content

Update page using some form of PHP function


slaterino

Recommended Posts

Okay,

Currently my code always adds just one item to my shopping basket at a time, which is completely fine for most items. However there are a few items that are in multiples, and are therefore only available in quantities of 5, 10 and 20. I have altered my code slightly so that when looking in the basket customers can change the quantities solely to these amounts but have a problem as when the basket page is opened the quantity always defaults to 1 item, even though it may show in the basket as 5. It is then necessary to click the 'Update Cart' button to refresh the amount. My question then is thus: what can I add to the script so that the update cart function is run as soon as the page loads? As this to me seems like the easiest way to do it.

 

This is my script below. I want the mutliple items to default to the value of $multi1a and not to $qty (as when the page loads $qty will always be the value of 1)

 

			$output[] = '<td><select name="qty'.$id.'" value="'.$qty.'"><option selected value="'.$multi1a.'">'.$multi1a.'</option><option value="'.$multi2a.'">'.$multi2a.'</option><option value="'.$multi3a.'">'.$multi3a.'</option></td>';
		}	
        if ($pricing == "Multi")
		{
		if ($qty == $multi1a) {
		$output[] = '<td>£'.($multi1b).'</td>';
		$total += ($multi1b);
		}
		if ($qty == $multi2a) {
		$output[] = '<td>£'.($multi2b).'</td>';
		$total += ($multi2b);
		}
		if ($qty == $multi3a) {
		$output[] = '<td>£'.($multi3b).'</td>';
		$total += ($multi3b);
		}
		}
		else {
		$output[] = '<td>£'.(($Price) * $qty).'</td>';
		$total += ($Price) * $qty;
		}
		$output[] = '</tr>';
	}
	$sum = array_sum($contents);
	$output[] = '</table>';
	$output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>';
	$output[] = '<div><button type="submit">Update cart</button></div>';
	$output[] = '</form>';

 

Thanks!!

Link to comment
Share on other sites

Okay, that would probably help yes. This is both the 'add' and the 'update function. They both use php. I guess the thing will either to have the 'update' function load when the page loads or maybe to change the add function but I was hoping to find a quick solution to this, which for me would be just to have that 'update' function load when the page does. Would I be able to simply place the 'update' function at the top of the page so that it will always run when the page loads. Is this possible or bad practice?

 

switch ($action) {
case 'add':
	if ($cart) {
		$cart .= ','.$_GET['id'];
	} else {
		$cart = $_GET['id'];
	}
	break;
case 'update':
if ($cart) {
	$newcart = '';
	foreach ($_POST as $key=>$value) {
		if (stristr($key,'qty')) {
			$id = str_replace('qty','',$key);
			$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
			$newcart = '';
			foreach ($items as $item) {
				if ($id != $item) {
					if ($newcart != '') {
						$newcart .= ','.$item;
					} else {
						$newcart = $item;
					}
				}
			}
			for ($i=1;$i<=$value;$i++) {
				if ($newcart != '') {
					$newcart .= ','.$id;
				} else {
					$newcart = $id;
				}
			}
		}
	}
}
$cart = $newcart;
}
break;

Link to comment
Share on other sites

I don't like your approach honestly for example if I need 5 I'll take a pack of 5. If I need 10 I'll take a pack of 10. When I need 15 I'll... go somewhere else??

 

Create separate products for each and allow me (the customer) to select how many I want. In the example a pack of 10 and 5 (or 3 packs of 5). If I need 100 I'll add the pack of 20 and update my cart to 5. A customer will not buy 20 just because you don't support 15 and certainly not if your competitor is more flexible than you.

Link to comment
Share on other sites

Okay,

I got it sorted in the end. Had to try a different approach though but got it sorted.

 

Thanks for all your help!

 

And ignace, I'm not really sure when this post became about business ethics and quite why you felt the need to bring that into it! But anyway, thanks to everyone else!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.