My website uses a private e-commerce system, and unfortunately, while their support team are very helpful, they are not particularly supportive of 'custom code', such the code that I require.
I am trying to configure a "splash" page (which pops up before users complete their order), which offers users the opportunity to buy one of our t-shirts half-price, with an "add to cart" allowing them to add it to their cart from within the splash page.
I have tried copying code from the standard product pages, but unfortunately the button does not work as it should, and instead it simply links through to '#' (which can be seen in the code below).
<form action="includes/cart/add_to_cart.inc.php" method="get" name="addcartform" id="addcartform" onsubmit="return false;">
<!-- Important hidden inputs -->
<input type="hidden" id="js" name="js" value="false" />
<input type="hidden" id="product_id" name="product_id" value="{product:id}" />
<a id="add_to_cart" style="display:none;" href="#" onclick="{product:add_to_cart_js}" class="generated_button" title="Add To Cart" >
<img src="images/icons/cart_add.png" align="absmiddle" />
<span> Add To Cart </span> </a>
<noscript>
<!-- non javascript -->
<input class="generated_button" type="submit" value="Add To Cart" name="submit" id="cart_submit" />
</noscript>
After putting the code in, I change the "display" of the link to "inline-block" (so that it shows), and I also tried putting in the ID of the product that I want users to add to their cart here:
id="product_id" name="product_id" value="{product:id}"
Unfortunately none of this has worked, and as I dont have complete (or easy) access to all of the files I need, I'd really appreciate any help with getting this one resolved!
Thanks in advance,
Dan
UPDATE: The support team behind our eCommerce system have since sent me this piece of code to help, but I'm not entirely sure how to use it:
If you want more control via javascript you can use the core Cart object we make available > in the javascript. Its accessable via the (Private eCommerce System) namespace, and in
fact if you open up the console in an (Private eCommerce System) site and type in (Private > eCommerce System) and hit enter it will return an object to you of what parts of the core > system we make available to you right in the javascript so far, (this will increase in
time too).
The (Private eCommerce System).Cart object has a sub addItem() method/function that can be used here.
You can pass it the product id to add it to cart: (e.g. adding a product with id #52)
Cart.addItem(52);
Or you can pass it these full options like so:
Cart.addItem({
productId: 52,
qty: 1,
options: {},
wishlistId: 0,
extraData: "Some detail i want to show to the user"
}, function () {
console.log('This is the callback for when its complete!');
});
So if they add a button or link, they can use jQuery to set on click of that button to run that code, e.g.
jQuery(function ($) {
$('.some-button').on('click', function () {
Cart.addItem(52);
return false;
});
});
What if you swap out the Javascript button and replace it with the no-script input field?
<form action="includes/cart/add_to_cart.inc.php" method="get" name="addcartform" id="addcartform" onsubmit="return false;">
<!-- Important hidden inputs -->
<input type="hidden" id="js" name="js" value="false" />
<input type="hidden" id="product_id" name="product_id" value="{product:id}" />
<input class="generated_button" type="submit" value="Add To Cart" name="submit" id="cart_submit" />
<img src="images/icons/cart_add.png" align="absmiddle" />
<span> Add To Cart </span>
</form>
You might need to re-style the form fields and the <span> which says "Add to Cart" to get it looking right...
UPDATE:
Given the information the support team have provided, try putting this in your "splash page", instead of the form above.
<div>
<script>
function addTshirtToCart(){
/* Swap out the 34 for the actual product ID of the tshirt you want to
add to the basket */
Cart.addItem(34);
// You might also want some code here to close the "splash page"
return false;
}
</script>
<a href="#" id="add_to_cart" onclick="return addTshirtToCart();">
<img src="images/icons/cart_add.png" align="absmiddle" />
<span> Add To Cart </span>
</a>
</div>
UPDATE 2:
Replace the existing div from my 2nd answer and replaced it with this which works for me:
<form action="includes/cart/add_to_cart.inc.php" method="get" name="addcartform" id="addcartform" onsubmit="return false;">
<input type="hidden" id="js" name="js" value="false">
<input type="hidden" id="product_id" name="product_id" value="1618">
<a id="add_to_cart" style="" href="#" onclick="update_cart_content(); return false;" class="generated_button" title="Add To Cart">
<img src="images/icons/cart_add.png" align="absmiddle">
<span> Add To Cart </span> </a>
<noscript>
<input class="generated_button" type="submit" value="Add To Cart" name="submit" id="cart_submit"/>
</noscript>
</form>
UPDATE 3
To close the popup when adding the t-shirt to the cart, try putting this in the a element above:
onclick="update_cart_content(); spashBox.close(); return false;"
Related
I downloaded some code that uses the CodeIgniter framework, and I have the confirm now button which confirms the appointment. I would like to add the following code to the button so that it will confirm my appointment as well as take the user to the payment gateway.
Payment gateway code:
<a href="https://www.payfast.co.za/eng/process?cmd=_paynow&receiver=10907075&item_name=Tutor+Session&item_description=Tutor+Meetup&amount=170.00&return_url=https%3A%2F%2Fwww.lectiotutoring.co.za&cancel_url=https%3A%2F%2Fwww.lectiotutoring.co.za"><img src="https://www.payfast.co.za/images/buttons/light-small-paynow.png"
width="165"
height="36"
alt="Pay"
title="Pay Now with PayFast" />
</a>
Existing button with function i want to add the above code to:
<form id="book-appointment-form" style="display:inline-block" method="post">
<button id="book-appointment-submit" type="button" class="btn btn-success">
<span class="glyphicon glyphicon-ok"></span>
<?= !$manage_mode ? lang('confirm') : lang('update') ?>
</button>
<input type="hidden" name="csrfToken" />
<input type="hidden" name="post_data" />
</form>
Its not so much the button but rather the logic you put on the event.
you first should validate success of appointment confirmation. If success is true, then you can redirect to payment gateway.
you can do this two ways from php side using header 301 or with ajax and javascript
I have a php page where I load stripe checkout buttons for 12 subscription options. The page takes up to 10 seconds to load. Is there a way to speed it up?. I can't demonstrate it as it requires the user to be logged in before the buttons will load.The code that loads the buttons is in a for loop and looks like this:
<form action="https://www.example.com/plans/subscribe.php" method="post">
<input type="hidden" name="customer" value="<? echo $stripeID ?>">
<input type="hidden" name="plan" value="<? echo $thisPlan['id'] ?>">
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
data-name="www.breakoutwatch.com"
data-image="https://www.example.com/images/eyeLogo.png"
data-description="<? echo $thisPlan['name'] ?>"
data-amount="<? echo $thisPlan['amount'] ?>"
data-locale="auto"
data-panel-label="Subscribe Now"
data-label="Subscribe"
data-allow-remember-me="false">
</script>
</form>
It requires a little javascript work, but I'd recommend using Checkout's custom integration for a case like this!
https://stripe.com/docs/checkout#integration-custom
You'd load checkout once, create your buttons, and add on click handlers. To identify the plan being chosen you could add a hidden <input> which is populated with the plan name based upon the button you click.
Example with multiple buttons
form
<form id="myForm" action="/charge" method="post">
<input type="button" id="buySocks" value="Buy Socks for $10">
<input type="button" id="buyShirts" value="Buy Shirts for $30">
<input type="hidden" id="stripeToken" name="stripeToken" />
<input type="hidden" id="stripeEmail" name="stripeEmail" />
<input type="hidden" id="product" name="product" />
</form>
js
// configure checkout
var handler = StripeCheckout.configure({
key: 'pk_test_6pRNASCoBOKtIshFeQd4XMUh',
token: function(token) {
// append your token id and email, submit your form
$("#stripeToken").val(token.id);
$("#stripeEmail").val(token.email);
$("#myForm").submit();
}});
// click on a button
$('#buySocks').on('click', function(e) {
$("#product").val("socks");
openCheckout("Buy socks for $10", 1000);
e.preventDefault();
});
$('#buyShirts').on('click', function(e) {
$("#product").val("shirts");
openCheckout("Buy shirts for $30", 3000);
e.preventDefault();
});
// open checkout
function openCheckout(description, amount){
handler.open({
name: 'My Cool Shop',
description: description,
amount: amount
});
}
Full example: http://jsfiddle.net/ns2fezag/
While this answer isn't code, I ended up solving this by moving the Stripe checkout form over to a different page. Now my users click "Add to cart" and they're brought to a different page with a single button to invoke Stripe's checkout modal. It's not perfect but it's much faster and works.
So here is my problem. I have the following form:
<form name="picture_categories" action="scripts/catalog.php" method="post">
<input class="visibleForm" onclick="return false;" type="image" src="images/smartphones.png"/>
<label for="smartphones">Smartphones</label>
<input type="hidden" name="device" value="smartphones" />
<div class="hiddenForm" style="display:none">
<input src="images/logos/apple-logo.png" type="image" name="manuf" value="APPLE" />
<input src="images/logos/samsung-logo.png" type="image" name="manuf" value="Samsung" />
<input src="images/logos/blackberry-logo.png" type="image" name="manuf" value="Blackberry" />
<!-- <input src="images/logos/htc_logo.png" type="image" name="manuf" value="HTC" /> add to catalog first-->
<input src="images/logos/lg-logo.png" type="image" name="manuf" value="LG" />
</div>
</form>
Supposedly, when I click on one of inputs[name='manuf'] it submitts its value along with hidden input ('device') value to next page.
Now, the next page has following script:
<?php session_start(); ?>
<?php
if(isset($_POST['device'])) {
$_SESSION['device'] = $_POST['device'];
}
if (isset($_POST['manuf'])) {
$_SESSION['manuf'] = $_POST['manuf'];
}
header ("Location: ../display_catalog.php");
?>
And the last page - display_catalog.php uses $_SESSION data to display related part of the catalog.
The code works excellent in Chrome; however:
In Firefox somehow ignores $_SESSION['manuf'] variable. So it sorts my catalog correctly by $_SESSION['device'], but does not want to sort it by manufacturer name.
In IE it completely ignores both $_SESSION variables.
What could be the issue here?.
That's because input buttons of type image carry the x,y coordinate of the button, not the value (the button is used to make an image act as a submit). Its behaviour is very browser-dependent, that's why you see it working so differently across browsers.
If you want to customize with images and have a submit button properly working you could use the <button> element and style it with CSS background property or put an img element directly:
Something like:
<button type="submit" name="manuf" value="apple"><img src="apple-image.png"></button>
<button type="submit" name="manuf" value="samsung"><img src="somsung-image.png"></button>
Here I used JavaScript to Delete an employee...
<script type="text/javascript">
function delet(emp)
{
var answer = confirm('Are you sure?');
if(answer)
{
window.location='delete.php?emp='+emp;
}
}
</script>
By using anchor tag am calling the function of javascript ....
<a onclick="javascript:delet('<? echo $_GET['emp']; ?>')">
<input type="button" name="delete" id="delete" style="background: url('images/del1.jpg')no-repeat;width:50px;height:50px" value=""/></a>
But my problem is it is working upto showing the alert msg but after answering the alert msg it is not redirecting to the page given by me that is "delete.php?emp=+emp"
Because you're nesting an <input type="Submit"/> inside an <a>, clicking the button is not the same as clicking the <a>, rather it "Submits" nowhere (read to the page you're already on, refreshing the page) before the <a> can do it's job.
Simple demo where you don't go to google.
<input type="Submit"/>
Bergi has pointed out that the behaviour I described is not universal (hello Opera, IE) unless a <form> element is present, so for example the following
<form action="jail.php">
<a href="go.php" onclick="window.location='?collect=£200';">
<input type="submit"/>
</a>
</form>
will send you directly to jail without passing go or collecting £200 across all browsers.
Further, this only really applies to page redirection; other pieces of script may well fire before the page changes, the easiest to observe being console.log, alert, etc.
You're submitting some form by clicking on that button. That submit process will overrule the window.location navigation request. A quick workaround would be to prevent that:
<a onclick="event.preventDefault(); delet('<? echo $_GET['emp']; ?>');">
// cross-browser-safe:
<a onclick="delet('<? echo $_GET['emp']; ?>'); return false">
but actually your markup with the nested button is quite odd. You don't need that link at all, just use
<form action="delete.php" onsubmit="return confirm('Are you sure');">
<input type="hidden" name="emp" value="<? echo $_GET['emp']; ?>" />
<input type="submit" name="delete" id="delete" style="background: url('images/del1.jpg')no-repeat;width:50px;height:50px" value="" />
</form>
So I am trying add an item to the shopping cart without clicking on the add to cart in miva. For example
I go here
http://posnation.com/pos_knowledge
and i click on the Buy now button
it takes me to
http://posnation.com/pos_support/online_kbase
but on that page there is an add to cart button, if you hit it the item gets added to the Miva cart. How do I skip this page
http://posnation.com/pos_support/online_kbase
and go straight to the cart with the item added
If i firebug over the add to cart there is a javascript onchange function called ComputeTotalCost but i am not sure how to get there from the first page
You would need to make it a forum:
<form method="post" name="FORM" action="/mm5/merchant.mvc?">
<input name="Product_Attribute_Count" value="-1" type="hidden">
<input name="Action" value="ADPR" type="hidden">
<input name="Screen" value="BASK" type="hidden">
<input name="Store_Code" value="POS_Systems" type="hidden">
<input name="Product_Code" value="online_kbase" type="hidden">
<input name="Category_Code" value="pos_support" type="hidden">
<input name="Quantity" value="1" size="4" type="text">
<input src="/images/buy_now.png" alt="Add to Cart" align="absmiddle" type="image">
</form>
It needs to post data to the cat in order to add it to the cart, The code above should add 1 of the items to the cart.
you can add a javascript action at the bottom of that page to submit the form.
the form name is "FORM" so the code you need to add is:
<script language="JavaScript" type="text/javascript">
document.FORM.submit();
</script>
Do you want to do this in javascript? You can just put a listener on the buy now button that will redirect to whatever page you want:
This is done in jquery:
$("#yourbuttonid").click(
function(event) {
// do the first action (submit the button to serverside)
// do the redirect
});