Post by dbstr » Thu Sep 10, 2009 10:29 pm

Sorry for my continously bumping of this thread, just wants to tell that I got it to work. Wasn't really that hard. :D

Request Reviews v1.0 released.


Active Member

Posts

Joined
Sun Aug 30, 2009 12:20 am

Post by Qphoria » Thu Sep 10, 2009 10:39 pm

Why not share how you did it

Image
Donate!|OpenCart Basics|GeoZones
Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by dbstr » Thu Sep 10, 2009 10:50 pm

I will. I just have to test it on the standard template, and write a little guide for adding it. ;-)

Request Reviews v1.0 released.


Active Member

Posts

Joined
Sun Aug 30, 2009 12:20 am

Post by BAJR » Fri Sep 11, 2009 5:09 pm

This is what I've been waiting for!! :)

New member

Posts

Joined
Sun Jul 12, 2009 6:17 pm

Post by dbstr » Fri Sep 11, 2009 6:48 pm

Open: catalog/controller/product/category.php

Go to line 143 and find the product array.
Find: 'model' => $result['model'],

After, insert:
'product_id' => $result['product_id'],
'href2' => $this->url->http('checkout/cart'),


Go to line 229 and find: $this->data['order'] = $order;
Insert after: $this->data['button_add_to_cart'] = $this->language->get('button_add_to_cart');

Open: product/category.tpl (in your template folder)

Add the following code, to the image tag (line 42 in standard template)

Code: Select all

id="image_<?php echo $products[$j]['product_id']; ?>"
Add the following code, at around line 51 (standard template):

Code: Select all

<form action="<?php echo $products[$j]['href2']; ?>" method="post" enctype="multipart/form-data" id="category_product_<?php echo $products[$j]['product_id']; ?>"> 
<input type="hidden" name="quantity" size="3" value="1" /> 
<input type="hidden" name="product_id" value="<?php echo $products[$j]['product_id']; ?>" />
<a class="add2cart button" onclick="$('#category_product_<?php echo $products[$j]['product_id']; ?>').submit();" id="category_product_<?php echo $products[$j]['product_id']; ?>"><span><?php echo $button_add_to_cart; ?></span></a>
(Small note: The quantity field is hidden, if you want it to show: change type="hidden" to type="input".
Also, you might want to split this code up, but it's basic html, so it shouldn't be a problem, if you got this far)

Now to the fun part -> Open module/cart.tpl (in your template folder)

At line 59 and 60 you will find:

Code: Select all

    });
});
IN BETWEEN THOSE TWO LINES, insert this: (note: if you are insecure about this part, check the bottom)

Code: Select all

    $('a.add2cart[class]').each(function() {
        var $t = $(this);
        $t.removeAttr('onclick')
    });
 
    $("a[id^='category_product']").click(function () {
    var productIDValSplitter    = (this.id).split("_");
    var productIDVal            = productIDValSplitter[2];
        
        $.ajax({
            type: 'post',
            url: 'index.php?route=module/cart/callback',
            dataType: 'html',
            data: $('#category_product_'+ productIDVal +' :input'),
            success: function (html) {
                $('#module_cart .middle').html(html);
            },    
            complete: function () {
                var image = $('#image_' + productIDVal).offset();
                var cart  = $('#module_cart').offset();
        
                $('#image_' + productIDVal).before('<img src="' + $('#image_' + productIDVal).attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');
        
                params = {
                    top : cart.top + 'px',
                    left : cart.left + 'px',
                    opacity : 0.0,
                    width : $('#module_cart').width(),  
                    heigth : $('#module_cart').height()
                };        
        
                $('#temp').animate(params, 'slow', false, function () {
                    $('#temp').remove();
                });        
            }            
        });            
    }); 

* If you are insecure about where to place the above code, and you haven't made any changes to the original jQuery,
you can replace the whole javascript with this:

Code: Select all

 <script type="text/javascript"><!--
$(document).ready(function () {
    $('#add_to_cart').replaceWith('<a onclick="" id="add_to_cart" class="button">' + $('#add_to_cart').html() + '</a>');

    $('#add_to_cart').click(function () {
        $.ajax({
            type: 'post',
            url: 'index.php?route=module/cart/callback',
            dataType: 'html',
            data: $('#product :input'),
            success: function (html) {
                $('#module_cart .middle').html(html);
            },    
            complete: function () {
                var image = $('#image').offset();
                var cart  = $('#module_cart').offset();
    
                $('#image').before('<img src="' + $('#image').attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');
    
                params = {
                    top : cart.top + 'px',
                    left : cart.left + 'px',
                    opacity : 0.0,
                    width : $('#module_cart').width(),  
                    heigth : $('#module_cart').height()
                };        
    
                $('#temp').animate(params, 'slow', false, function () {
                    $('#temp').remove();
                });        
            }            
        });            
    });
    
    $('a.add2cart[class]').each(function() {
        var $t = $(this);
        $t.removeAttr('onclick')
    });
 
    $("a[id^='category_product']").click(function () {
    var productIDValSplitter    = (this.id).split("_");
    var productIDVal            = productIDValSplitter[2];
        
        $.ajax({
            type: 'post',
            url: 'index.php?route=module/cart/callback',
            dataType: 'html',
            data: $('#category_product_'+ productIDVal +' :input'),
            success: function (html) {
                $('#module_cart .middle').html(html);
            },    
            complete: function () {
                var image = $('#image_' + productIDVal).offset();
                var cart  = $('#module_cart').offset();
        
                $('#image_' + productIDVal).before('<img src="' + $('#image_' + productIDVal).attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');
        
                params = {
                    top : cart.top + 'px',
                    left : cart.left + 'px',
                    opacity : 0.0,
                    width : $('#module_cart').width(),  
                    heigth : $('#module_cart').height()
                };        
        
                $('#temp').animate(params, 'slow', false, function () {
                    $('#temp').remove();
                });        
            }            
        });            
    });            
});
//--></script>

Request Reviews v1.0 released.


Active Member

Posts

Joined
Sun Aug 30, 2009 12:20 am

Post by BAJR » Sat Sep 12, 2009 1:18 am

You are an amazing person!

First time!

Thank you so much... this is something that could be added as standard !

I can't thank you enough...!

And thanks to all that have helped.. and of course Open Cart.! ;D

New member

Posts

Joined
Sun Jul 12, 2009 6:17 pm

Post by madimar » Thu Oct 01, 2009 5:35 am

Hey guys, you are great!
dbstr, just one remark on your great guide in your last post...

</form> is missing!!!!! >:D

I know it's a stupid thing and it is present in previous posts but... I was just following your last instructions and, without </form> closure, things were not obviously well... I was losing my head... then, finally I find out this damned missing part...

many thanks again.

Max

-----------------------------------------------------------------------
My last mods: Partita IVA e CF | Pro EU VAT Number | Sales Agents | Pricelist Pro
-----------------------------------------------------------------------


User avatar
Active Member

Posts

Joined
Thu Sep 24, 2009 6:27 pm


Post by dbstr » Thu Oct 01, 2009 5:46 am

Sorry about that, my bad.

Also, you would want to change the id's, to get it to w3c validate properly. ;-)

Request Reviews v1.0 released.


Active Member

Posts

Joined
Sun Aug 30, 2009 12:20 am

Post by madimar » Thu Oct 01, 2009 6:25 pm

Hi dear dbstr...
I was wondering if you could help me/us again... I would like to insert first lines of product description directly in products list (I already changed the view in order to have only one product for each line, so I have some space to insert description).

What do you think about? Is it something doable easily?

Many thanks in advance, I will be grateful even with some tips.

Max

-----------------------------------------------------------------------
My last mods: Partita IVA e CF | Pro EU VAT Number | Sales Agents | Pricelist Pro
-----------------------------------------------------------------------


User avatar
Active Member

Posts

Joined
Thu Sep 24, 2009 6:27 pm


Post by dbstr » Thu Oct 01, 2009 7:14 pm

Do you want the first N words, or the first N characters?

Either way, you should open and edit catalog/controller/product/category.php, and find the data array for the product listing.

For, let's say, the first 50 chars, you should just add:

Code: Select all

'description' => substr(strip_tags(html_entity_decode($result['description'])),1,50),
But since that's not very nice, you could use a function to return the first 50 words instead.

This is just something I found on google, so I'll just give you this, as it's much easier than starting to explain how my own stuff would work! :D

At the bottom of catalog/controller/product/category.php BEFORE the last } insert this:

Code: Select all

private function shorten_string($string, $wordsreturned)
    /*  Returns the first $wordsreturned out of $string.  If string
    contains more words than $wordsreturned, the entire string
    is returned.*/
    {
    $retval = $string;    //    Just in case of a problem
    $array = explode(" ", $string);
    /*  Already short enough, return the whole thing*/
    if (count($array)<=$wordsreturned)
    {
        $retval = $string;
    }
    /*  Need to chop of some words*/
    else
    {
        array_splice($array, $wordsreturned);
        $retval = implode(" ", $array)." ...";
    }
    return $retval;
    }
And to your data array, add this (line ~143, somewhere)

Code: Select all

'description' => ControllerProductCategory::shorten_string(strip_tags(html_entity_decode($result['description'])),20),
(this returns first 20 words)

In your view file, simply add

Code: Select all

<?php echo $products[$j]['description']; ?>
Hope it works, just did a quick test on a standard installation and it seems to work. Tell me if I forgot something. I got alot of things going on right now (new shipping modules + status sms notification) :D

Request Reviews v1.0 released.


Active Member

Posts

Joined
Sun Aug 30, 2009 12:20 am

Post by madimar » Thu Oct 01, 2009 8:45 pm

I have no words... I love you... :-* ;)
I'm learning a lot of things too...

Many many thanks, I hope I will be able to repay you in the future!
At the moment... I'm quite good O0 with Excel/VBA programming... and I'm available to help in case of need...

ciao

Max

-----------------------------------------------------------------------
My last mods: Partita IVA e CF | Pro EU VAT Number | Sales Agents | Pricelist Pro
-----------------------------------------------------------------------


User avatar
Active Member

Posts

Joined
Thu Sep 24, 2009 6:27 pm


Post by antonio » Fri Jan 08, 2010 6:55 pm

Dear,


I have problem with opencart.

Now I want to have "add to cart" button in Home page in my opencart system.

In home.tpl file, I made changes as below:

*********************************************************************
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="product_<?php echo $j;?>">
<td style="align: left; width: 70%">
<div style="background: #F7F7F7; border: 1px solid #DDDDDD; padding: 10px;"><?php echo $text_qty; ?>
<input type="text" name="quantity" size="3" value="1" />

</td>
<td>
<a onclick="$('#product_<?php echo $j;?>').submit();" id="add_to_cart" class="button"><span><?php echo $button_add_to_cart; ?></span></a></div>
<?php
$product_id = $products[$j]['product_id'];
?>
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>" />
</td>
</form>
********************************************************************

It runs OK from the second product in the list. But the first product, It can not add into the cart because of error as below:

**********************************************

Notice: Undefined index: product_id inC:\xampp\htdocs\qsmart\catalog\controller\module\cart.phpon line 57Notice: Undefined index: quantity inC:\xampp\htdocs\qsmart\catalog\controller\module\cart.phpon line 57

**********************************************

The code in line 57 of C:\xampp\htdocs\qsmart\catalog\controller\module\cart.php file as below:
$this->cart->add($this->request->post['product_id'], $this->request->post['quantity'], $option);


Please advise me and help me to solve this issue. Thank you very much!


Antonio

Newbie

Posts

Joined
Wed Dec 23, 2009 4:36 pm

Post by i2Paq » Fri Jan 08, 2010 7:58 pm

antonio wrote:Antonio
Please open up your own topic but do not hijjack someone elses.

And also DO NOT POST YOUR QUESTION IN MULTIPLE TOPICS!

Norman in 't Veldt
Moderator OpenCart Forums

_________________ READ and Search BEFORE POSTING _________________

Our FREE search: Find your answer FAST!.

[How to] BTW + Verzend + betaal setup.


User avatar
Global Moderator

Posts

Joined
Mon Nov 09, 2009 7:00 pm
Location - Winkel - The Netherlands

Post by antonio » Fri Jan 08, 2010 10:21 pm

Dear i2Paq,


Sorry about the convenience.

By the way, someone can help me for the above issue. Thank you very much!



Thank you
Antonio

Newbie

Posts

Joined
Wed Dec 23, 2009 4:36 pm

Post by deeve » Wed Jan 20, 2010 8:32 pm

Hi dbstr,
don't know if you're still following this thread but I had a go @your mods in 1.3.4 & managed to achieve an 'Add To Cart' button on the first product only which would suggest I've put the code in the wrong place as is not repeating for the rest of the array. Would you be so kind as to tell me where I went wrong & also, was wondering if this could be applied to all product listings, not just in Categories - ie: Latest Products & when there are no Sub-Categories.

Thanks.

My table in category.tpl

Code: Select all

<table class="list">
      <?php for ($i = 0; $i < sizeof($products); $i = $i + 4) { ?>
      <tr>
        <?php for ($j = $i; $j < ($i + 4); $j++) { ?>
        <td width="25%"><?php if (isset($products[$j])) { ?>
          <a href="<?php echo $products[$j]['href']; ?>"><img src="<?php echo $products[$j]['thumb']; ?>" title="<?php echo $products[$j]['name']; ?>" alt="<?php echo $products[$j]['name']; ?>" id="image_<?php echo $products[$j]['product_id']; ?>" /></a><br />
          <a href="<?php echo $products[$j]['href']; ?>"><?php echo $products[$j]['name']; ?></a><br />
          <span style="color: #999; font-size: 11px;"><?php echo $products[$j]['model']; ?></span><br />
          <?php if ($display_price) { ?>
          <?php if (!$products[$j]['special']) { ?>
          <span style="color: #900; font-weight: bold;"><?php echo $products[$j]['price']; ?></span><br />
          <?php } else { ?>
          <span style="color: #900; font-weight: bold; text-decoration: line-through;"><?php echo $products[$j]['price']; ?></span> <span style="color: #F00;"><?php echo $products[$j]['special']; ?></span>
          <form action="<?php echo $products[$j]['href2']; ?>" method="post" enctype="multipart/form-data" id="category_product_<?php echo $products[$j]['product_id']; ?>"><br />Qty: 
<input type="input" name="quantity" size="3" value="1" /> 
<input type="hidden" name="product_id" value="<?php echo $products[$j]['product_id']; ?>" />
<a class="add2cart button" onclick="$('#category_product_<?php echo $products[$j]['product_id']; ?>').submit();" id="category_product_<?php echo $products[$j]['product_id']; ?>"><span><?php echo $button_add_to_cart; ?></span></a></form>
          <?php } ?>
          <?php } ?>
          <?php if ($products[$j]['rating']) { ?>
          <img src="catalog/view/theme/default/image/stars_<?php echo $products[$j]['rating'] . '.png'; ?>" alt="<?php echo $products[$j]['stars']; ?>" />
          <?php } ?>
          <?php } ?></td>
        <?php } ?>
      </tr>
      <?php } ?>
    </table>

Active Member

Posts

Joined
Tue Oct 20, 2009 4:31 pm

Post by dbstr » Wed Jan 20, 2010 8:52 pm

Hi,

Try this - I haven't tested it, but it should work:

Code: Select all

<table class="list">
      <?php for ($i = 0; $i < sizeof($products); $i = $i + 4) { ?>
      <tr>
        <?php for ($j = $i; $j < ($i + 4); $j++) { ?>
        <td width="25%"><?php if (isset($products[$j])) { ?>
          <a href="<?php echo $products[$j]['href']; ?>"><img src="<?php echo $products[$j]['thumb']; ?>" title="<?php echo $products[$j]['name']; ?>" alt="<?php echo $products[$j]['name']; ?>" id="image_<?php echo $products[$j]['product_id']; ?>" /></a><br />
          <a href="<?php echo $products[$j]['href']; ?>"><?php echo $products[$j]['name']; ?></a><br />
          <span style="color: #999; font-size: 11px;"><?php echo $products[$j]['model']; ?></span><br />
          <?php if ($display_price) { ?>
          <?php if (!$products[$j]['special']) { ?>
          <span style="color: #900; font-weight: bold;"><?php echo $products[$j]['price']; ?></span><br />
          <?php } else { ?>
          <span style="color: #900; font-weight: bold; text-decoration: line-through;"><?php echo $products[$j]['price']; ?></span> <span style="color: #F00;"><?php echo $products[$j]['special']; ?></span>
          <?php } ?>
          <?php } ?>
          <?php if ($products[$j]['rating']) { ?>
          <img src="catalog/view/theme/default/image/stars_<?php echo $products[$j]['rating'] . '.png'; ?>" alt="<?php echo $products[$j]['stars']; ?>" />
          <?php } ?>
          <form action="<?php echo $products[$j]['href2']; ?>" method="post" enctype="multipart/form-data" id="category_product_<?php echo $products[$j]['product_id']; ?>"><br />Qty:
            <input type="input" name="quantity" size="3" value="1" />
            <input type="hidden" name="product_id" value="<?php echo $products[$j]['product_id']; ?>" />
            <a class="add2cart button" onclick="$('#category_product_<?php echo $products[$j]['product_id']; ?>').submit();" id="category_product_<?php echo $products[$j]['product_id']; ?>"><span><?php echo $button_add_to_cart; ?></span></a></form>
          <?php } ?></td>
        <?php } ?>
      </tr>
      <?php } ?>
    </table>

Request Reviews v1.0 released.


Active Member

Posts

Joined
Sun Aug 30, 2009 12:20 am

Post by deeve » Wed Jan 20, 2010 9:03 pm

Like a charm, dbstr - many thanks.
Only page it doesn't show up on is Latest Products on homepage, so imagine that needs to be put into home.tpl?

Active Member

Posts

Joined
Tue Oct 20, 2009 4:31 pm

Post by dbstr » Wed Jan 20, 2010 9:27 pm

Yep, I think so.. Give it a try. It's been awhile since I've been messing with this stuff.

Request Reviews v1.0 released.


Active Member

Posts

Joined
Sun Aug 30, 2009 12:20 am

Post by allenshea » Wed Mar 03, 2010 10:21 pm

dbstr wrote:Yep, I think so.. Give it a try. It's been awhile since I've been messing with this stuff.

It works in my 1.4.0, thanks a lot.

I have a question now. Our website is 'Only show prices when a customer is logged in.' How can I hide the Button and "Qty:" if they haven't log in?

Thanks a lot.

Allen

I know nothing about PHP and SQL, but I still try my best to understand it.


Active Member

Posts

Joined
Mon Dec 14, 2009 10:01 pm

Post by Qphoria » Wed Mar 03, 2010 10:24 pm

allenshea wrote: I have a question now. Our website is 'Only show prices when a customer is logged in.' How can I hide the Button and "Qty:" if they haven't log in?
1. EDIT: catalog/view/theme/YOURTHEME/template/product/product.tpl

2. FIND:

Code: Select all

<div style="background: #F7F7F7; border: 1px solid #DDDDDD; padding: 10px;"><?php echo $text_qty; ?>
<input type="text" name="quantity" size="3" value="1" />
<a onclick="$('#product').submit();" id="add_to_cart" class="button"><span><?php echo $button_add_to_cart; ?></span></a></div>
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>" />
<input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
3. REPLACE WITH:

Code: Select all

<?php if ($display_price) { ?>
<div style="background: #F7F7F7; border: 1px solid #DDDDDD; padding: 10px;"><?php echo $text_qty; ?>
                <input type="text" name="quantity" size="3" value="1" />
                <a onclick="$('#product').submit();" id="add_to_cart" class="button"><span><?php echo $button_add_to_cart; ?></span></a></div>
              <input type="hidden" name="product_id" value="<?php echo $product_id; ?>" />
              <input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
<?php } ?>

Image
Donate!|OpenCart Basics|GeoZones
Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am
Who is online

Users browsing this forum: Bing [Bot] and 3 guests