Magento - Product add to mini cart from slider - php

I want to add product to mini cart from slider.
Slider contain number of thumbnails of products ( It is dynamically generate from admin side. When admin is adding product, the product image will automatically adding in slider too.)
Now my question is
-> If user clicks on specific thumbnail, and click to add-to-cart button, it should be add in Mini cart in header using ajax.
-> If I'm getting product id with Image, Is it enough ? If not, what are parameters are required ?
I haven't more knowledge for Magento, If you knowing even One point from above, please guide me. It will me more helpful to solve out issue.

As of this post:(EE 1.12 or CE 1.7)
Adding a product to the (mini) cart/cart remains relatively the same process across the site, however, where are you adding this slider?
Home Page? Product Listing Page? Product View/Detail Page? Other?
1) Yes, if you properly link the Product via: Link, Image, or (Add to Cart) button, you can have the item added to the cart. See below.
2) We use the ID/SKU to retrieve the product information and in turn it's Image/Small/Thumbnail images as well as Label, Short/Long Description or any other Product related data to the SKU/ID, so yes the product ID is enough information to add the product to the cart.
Are you using a specific slider? Making your own?
Slider Template via Listing Page, read, apply, and expand; do not just copy and paste.
//You'll want to loop through your collection for the slider, is this collection from a category? Custom module?
<?php foreach ($_productCollection as $_product): ?>
//Get/Load the Product Id when looping through a/your collection:
<?php $product = Mage::getModel('catalog/product')->load($_product->getId()); ?>
//Get the Image/Link Information you want to display for your slider:
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(100); ?>" width="100" height="100" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
//Basic Add to Cart:
<?php //echo $this->helper('checkout/cart')->getAddUrl($_product) ?>
//Ajax Button
<button onclick="productAddToCartForm.submit()" class="button btn-cart"><span><span>Get a Quote</span></span></button>
//Basic Javascript for Button
<script type="text/javascript">
//<![CDATA[
var productAddToCartForm = new VarienForm('product_addtocart_form');
productAddToCartForm.submit = function(){
if (this.validator.validate()) {
this.form.submit();
}
}.bind(productAddToCartForm);
//]]>
</script>
For additional Details, the following provides a complete implementation as well.
The following provides a good example of the logic though:
http://tutorialmagento.com/add-multiple-product-to-cart-using-magento-ajax-add-cart
--
Update:
Magento's EE Iphone Theme also add's in a visual for adding the product to the cart.
Please see the following file (EE 1.12), I'll have to check to see if this is available in CE, keep in mind this is a Detail page and targeting product ID's on the home page will be different, however, once the Product ID is found, the rest is applicable.
app/design/frontend/enterprise/iphone/template/catalog/product/view.phtml
<?php // SAMPLE?>
<?php $_helper = $this->helper('catalog/output'); ?>
<?php $_product = $this->getProduct(); ?>
<script type="text/javascript">
var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>);
</script>
<div id="messages_product_view"><?php echo $this->getMessagesBlock()->setEscapeMessageFlag(true)->toHtml() ?></div>
<div class="product-view">
<form action="<?php echo $this->getSubmitUrl($_product) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<div class="no-display">
<input type="hidden" name="product" value="<?php echo $_product->getId() ?>" />
<input type="hidden" name="related_product" id="related-products-field" value="" />
</div>
<div class="product-essential">
<div class="product-img-box">
<?php echo $this->getChildHtml('media') ?>
</div>
<div class="product-shop">
<div class="product-main-info">
<div class="product-name">
<h1><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h1>
</div>
<?php echo $this->getChildHtml('alert_urls') ?>
<?php echo $this->getChildHtml('product_type_data') ?>
</div>
<?php echo $this->getChildHtml('tierprices') ?>
<?php echo $this->getChildHtml('extrahint') ?>
<?php if (!$this->hasOptions()):?>
<div class="add-to-box">
<?php if($_product->isSaleable()): ?>
<?php echo $this->getChildHtml('addtocart') ?>
<?php endif; ?>
</div>
<?php else:?>
<?php if ($_product->isSaleable() && $this->hasOptions() && $this->getChildChildHtml('container1') ):?>
<div class="options-container-small">
<?php echo $this->getChildChildHtml('container1', '', true, true) ?>
</div>
<?php endif;?>
<?php endif; ?>
<?php echo $this->getChildHtml('other');?>
</div>
</div>
// -------
<script type="text/javascript">
//<![CDATA[
var productAddToCartForm = new VarienForm('product_addtocart_form');
productAddToCartForm.submit = function(button, url) {
if (this.validator.validate()) {
var form = this.form;
var oldUrl = form.action;
if (url) {
form.action = url;
}
var e = null;
try {
var transformValue = {};
if ( Modernizr.csstransforms3d ) {
transformValue[Modernizr.prefixed('transform')] = 'translate3d(-82px, -106px, 2px) scale(0) rotate(200deg)';
} else if ( Modernizr.csstransforms ) {
transformValue[Modernizr.prefixed('transform')] = 'translate(-82px, -106px) scale(0) rotate(200deg)';
} else {
this.form.submit();
return false;
}
var originalImg = $$('.product-image-wrap .product-image img')[0];
originalImg.up('.product-image-wrap').insert(originalImg.clone().addClassName('cloned'));
setTimeout(function () {
$$('.cloned')[0].setStyle(transformValue);
}, 1);
$$('.product-image-wrap .cloned')[0].observe(transEndEventName, function(e) {
this.form.submit();
}.bind(this));
} catch (e) {
}
this.form.action = oldUrl;
if (e) {
throw e;
}
if (button && button != 'undefined') {
button.disabled = true;
}
}
}.bind(productAddToCartForm);
productAddToCartForm.submitLight = function(button, url){
if(this.validator) {
var nv = Validation.methods;
delete Validation.methods['required-entry'];
delete Validation.methods['validate-one-required'];
delete Validation.methods['validate-one-required-by-name'];
if (this.validator.validate()) {
if (url) {
this.form.action = url;
}
this.form.submit();
}
Object.extend(Validation.methods, nv);
}
}.bind(productAddToCartForm);
//]]>
</script>

Related

How to hide country parameters on page load via jquery ajax php

How to hide country parameters on page load via jquery ajax php.
The code below display results from database and its working fine.
I can hide/unhide or toggle all the country values each separately via its id when button is click and is working good.
Here is my issue: How can I hide only the all country values on page load so that I can toggle or hide/unhide it as usual.
here is my coding effort so far
<html>
<head>
<script src="jquery.min.js" type="text/javascript"></script>
</head>
<body>
<script>
//toggle div on click
$(document).ready(function() {
$(".hideunhide_country").click(function(){
var id = this.id;
$("#result_"+id).toggle( 'slow', function(){
});
});
});
/*
//hide unhide div on click
$(document).ready(function(){
$(".hideunhide_country").click(function(){
var id = this.id;
$("#result_"+id).hide();
});
});
*/
</script>
<div class="content">
<?php
include('db.php');
$result = $db->prepare('SELECT * FROM users order by id');
$result->execute();
while ($row = $result->fetch()) {
$id = $row['id'];
$country = $row['country'];
?>
<div class="p">
<h1>Userid: <?php echo $id; ?></h1>
//hide country values on page load
<div id="result_<?php echo $id; ?>">
<h1><?php echo $country; ?></h1>
</div>
<input type="button" value="HideUnhide" id="<?php echo $id; ?>" class="hideunhide_country" />
</div>
<?php
}
?>
</div>
</body>
</html>
Only a qucik fix, you schould refactor your code...
<html>
<head>
<script src="jquery.min.js" type="text/javascript"></script>
</head>
<body>
<script>
//toggle div on click
$(document).ready(function() {
$(".hideunhide_country").click(function(){
var id = this.id;
$("#result_"+id).toggle( 'slow', function(){
});
});
// hide all countries
$("[id^='result_']").hide()
});
/*
//hide unhide div on click
$(document).ready(function(){
$(".hideunhide_country").click(function(){
var id = this.id;
$("#result_"+id).hide();
});
});
*/
</script>
<div class="content">
<?php
include('db.php');
$result = $db->prepare('SELECT * FROM users order by id');
$result->execute();
while ($row = $result->fetch()) {
$id = $row['id'];
$country = $row['country'];
?>
<div class="p">
<h1>Userid: <?php echo $id; ?></h1>
//hide country values on page load
<div id="result_<?php echo $id; ?>">
<h1><?php echo $country; ?></h1>
</div>
<input type="button" value="HideUnhide" id="<?php echo $id; ?>" class="hideunhide_country" />
</div>
<?php
}
?>
</div>
</body>
</html>

Ajax how to disable div or item

I am new to ajax, is there a way you can help me, how to disable all items once click is successful:
Here is my code for ajax:
if (!parent.hasClass('.disabled')) {
// vote up action
if (action == 'click') {
alert("test");
};
//how do i add disabled function on a particular div
// add disabled class with .item
parent.addClass('.disabled');
};
here is my index:
<?php while($row = mysql_fetch_array($query)): ?>
<div class="item" data-postid="<?php echo $row['recipe_id'] ?>" data-score="<?php echo $row['vote'] ?>">
<div class="vote-span"><!-- voting-->
<div class="vote" data-action="up" title="Vote up">
<i class="icon-chevron-up"></i>
</div><!--vote up-->
<div class="vote-score"><?php echo $row['vote'] ?></div>
</div>
<div class="post"><!-- post data -->
<p><?php echo $row['recipe_title'] ?></p>
</div>
</div><!--item-->
i jst want to disable the loop icon-chevron-up class.not just one but all.
Actually no ajax call needed here. I will only be done by using jquery. See the code
$(document).ready(function(){
$('.item').click(function(){
if (!parent.hasClass('.disabled')) {
parent.addClass('.disabled');
}
});
});
Here you have not mentioned, on what click you need the action, so i consider that on the div contains class='item', the action will be performed. I hope it will help.

showing or hiding div with id generated using php and javascript

I am trying to hide or show the div based on the div id from the php function. I am not able to make it work, please help me.
Javascript:
<script>
showOrHide(id) {
var elem=getElementById(id);
if(elem.style.visibility="hidden")
elem.style.visibility="visible";
else
elem.style.visibility="hidden";
}
</script>
PHP Script:
<?php
function display_link($link_id,$upvote_array,$downvote_array,$divid) {
?>
More links
<div id="<?php echo $divid ?>" style="visibility:hidden;">
</div>
<?php } ?>
Here's a cleaned-up version of your function.
function showOrHide(id) {
var elem = document.getElementById(id);
elem.style.visibility = (elem.style.visibility === 'hidden')? 'visible' : 'hidden';
}
<script>
function showOrHide(id){
var elem = getElementById(id);
if(elem.style.visibility=="hidden"){
elem.style.visibility="visible";
} else {
elem.style.visibility="hidden";
}
}
</script>
<?php
function display_link($link_id,$upvote_array,$downvote_array,$divid)
{
?>
More links
<div id="<?php echo $divid ?>" style="visibility:hidden;">
</div>
1 - visibility == 'hidden'
2 - missing ; after visibility="visible" and ="hidden"
3 - href="javascript:showOrHide('')" - missing '' inside your javascript function
Try this
<script>
function showOrHide(id){
var elem = document.getElementById(id);
elem.style.visibility = (elem.style.visibility === 'hidden')? 'visible' : 'hidden';
}
</script>
<?php
function display_link($link_id,$upvote_array,$downvote_array,$divid)
{
?>
More links
<div id="<?php echo $divid ?>" style="display:hidden;">
</div>
<?php
}
?>

Load new page in div part 2

I have managed to load the new page into the div (thanks to everyone for your help) but it looks pretty bad (got menu bar and logo, but I only wanted the content), so instead I need to load only a div from the new page. I tried a new script but got redirected to the new page. Please help.
<script>
jQuery(document).ready(function() {
jQuery('.stil_link_img a').click(function(){
var x = $(this).attr("href") + "#content_eco";
jQuery("#new_page").load(x);
return false;
});
});
</script>
<div id="pachete">
<?php
$result=mysql_query("SELECT* FROM imagini");
while($data=mysql_fetch_row($result)){
if( ($data[3]==1)&&($data[2]==2) ){ ?>
<div class="stil_link_img">
<img src="upload/<?php echo $data[1];?>">
</div>
<?php }
}?>
</div>
<div id="new_page">
//some content which should be replaced with my loaded page
</div>
jQuery(document).ready(function() {
jQuery('.stil_link_img a').click(function(){
var $pageContent = jQuery('<div/>').load($(this).attr("href"));
jQuery("#new_page").html(jQuery("#content_eco",$pageContent).html());
return false;
});
});
I assume #content_eco is the divisions ID in the new page(the url from href attribute).
or you can load just the content from the url and avoid the link postback as
<script>
jQuery(document).ready(function() {
jQuery('.stil_link_img a').click(function(){
var x = $(this).attr("rel") + " #content_eco";
jQuery("#new_page").load(x);
return false;
});
});
</script>
<div id="pachete">
<?php
$result=mysql_query("SELECT* FROM imagini");
while($data=mysql_fetch_row($result)){
if( ($data[3]==1)&&($data[2]==2) ){ ?>
<div class="stil_link_img">
<img src="upload/<?php echo $data[1];?>">
</div>
<?php }
}?>
</div>
<div id="new_page">
//some content which should be replaced with my loaded page
</div>
Hope this helps you.

Output Text Doesn't Show in Correct Format

Here is the JavaScript I use to animate slider (fade effect) of the content I read from database:
<script language="javascript">
jQuery(document).ready(function ()
{
var terms = ["span_1","span_2"];
var i = 0;
function rotateTerm() {
jQuery("#text-content").fadeOut(200, function() {
jQuery(this).text(jQuery('#text-slider .'+terms[i]).html()+i).fadeIn(200);
});
jQuery("#title-content").fadeOut(200, function() {
jQuery(this).text(jQuery('#title-slider .'+terms[i]).html()+i).fadeIn(200);
i == terms.length - 1 ? i=0 : i++;
});
}
rotateTerm();
setInterval(rotateTerm, 1000);
});
</script>
And here is the PHP code I use:
<?php
if (!empty($testLst)) :
$num=1;
foreach($testLst as $key=>$item):
$item->slug = $item->id;
$item->catslug = $item->catid ;
?><div id="hidden-content" style="display:none;">
<div id="title-slider">
<span class="<?php echo 'span_'.$num; ?>">
<h4><a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($item->id, $item->catid)); ?>">
<?php echo $item->title; ?></a>
</h4>
</span>
</div>
<div id="text-slider">
<span class="<?php echo 'span_'.$num; ?>">
<p>
<?php
$concat=array_slice(explode(' ',$item->introtext),0,20);
$concat=implode(' ',$concat);
echo $concat."...";
?>
</p>
</span>
</div></div>
Learn more >></p>
<?php
$num++;
endforeach;
endif;
?>
<div id="title-content">
</div>
<div id="text-content">
</div>
And here is a JSFiddle page reproducing what I would like to do.
My problem is that I am getting data that still has HTML tags, however I would like the output to have my CSS styles.
You could clone the node, and set that to be the new content of the target elements, to keep everything in jQuery objects, but personally, I'd use the .outerHTML property.
I've updated your fiddle to show you what I mean: I've changed the .text(...set content here) to .html(), because we're injecting HTML content. Then, I added [0] at the end of your selector, to return the raw element reference, which gives access to all standard JS properties and methods an element has, and just went ahead and fetched the outerHTML... easy-peasy

Categories