Zen Cart Logo
Forums / Setting Up Categories, Products, Attributes / HOW CAN I AUTO-SELECT SINGLE-ATTRIBUTE CHOICES DURING ADD-TO-CART process?

HOW CAN I AUTO-SELECT SINGLE-ATTRIBUTE CHOICES DURING ADD-TO-CART process?

Locked

Views: 4,059

Results 1 to 7 of 7
This thread is locked. New replies are disabled.
19 Nov 2009, 4:50 PM
#1
wweyland avatar

wweyland

New Zenner

Join Date:
Sep 2009
Posts:
15
Plugin Contributions:
0

HOW CAN I AUTO-SELECT SINGLE-ATTRIBUTE CHOICES DURING ADD-TO-CART process?

I have configured a downloadable product by using product attributes

So in brief I have just one attribute which is always the same. I have configured my store to show Add to cart -buttons next to the products in the product listing page.

However, with my downloadable products, add to cart -buttons are not showing. There is just text more information. Can I somehow make the only attribute the default so that zen cart would allow showing the add to cart button also next to my downloadable products?

My Zen cart version is 1.3.8.

Thanks in advance!

19 Nov 2009, 5:19 PM
#2
schoolboy avatar

schoolboy

Totally Zenned

Join Date:
Jun 2005
Location:
Cumbria, UK
Posts:
10,327
Plugin Contributions:
0

Re: HOW CAN I AUTO-SELECT SINGLE-ATTRIBUTE CHOICES DURING ADD-TO-CART process?

I'm sure this was raised a couple of years ago... but I don't recall if the thread offered a solution...

Search about a bit... maybe you'll find it.

19 Nov 2009, 5:50 PM
#3
wweyland avatar

wweyland

New Zenner

Join Date:
Sep 2009
Posts:
15
Plugin Contributions:
0

Re: HOW CAN I AUTO-SELECT SINGLE-ATTRIBUTE CHOICES DURING ADD-TO-CART process?

Well, I found this one:
http://www.zen-cart.com/forum/showthread.php?t=98641&page=3

I'm not sure if this is what you ment. This deals with showing the dropdown box next to the product and has a partly working mod. It doesn't fit for the need since it requires quantity box to be showed and that won't work with downloadable products since there's no point in downloading multiple copies of the same pdf for example..

20 Nov 2009, 1:41 AM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: HOW CAN I AUTO-SELECT SINGLE-ATTRIBUTE CHOICES DURING ADD-TO-CART process?

Unlike the addon you linked to earlier, the following code changes do NOT add attribute choices to the product listing. Instead, they simply force the cart to add the only one single attribute defined for the given product.
If a product has only ONE attribute, then a buy-now button will appear. If more than one (including choice between DOC vs PDF formats for example), then the default "more info..." will show.

There are quite a few edits to make:
/includes/functions/functions_lookups.php
around line 226, replace the ENTIRE zen_has_product_attributes function declaration with this:```
/*

  • Check if product has attributes
    */
    function zen_has_product_attributes($products_id, $not_readonly = 'true', $returnCount = FALSE) {
    global $db;

    if (PRODUCTS_OPTIONS_TYPE_READONLY_IGNORED == '1' and $not_readonly == 'true') {
    // don't include READONLY attributes to determine if attributes must be selected to add to cart
    $attributes_query = "select pa.products_attributes_id
    from " . TABLE_PRODUCTS_ATTRIBUTES . " pa left join " . TABLE_PRODUCTS_OPTIONS . " po on pa.options_id = po.products_options_id
    where pa.products_id = '" . (int)$products_id . "' and po.products_options_type != '" . PRODUCTS_OPTIONS_TYPE_READONLY . "'";
    } else {
    // regardless of READONLY attributes no add to cart buttons
    $attributes_query = "select pa.products_attributes_id
    from " . TABLE_PRODUCTS_ATTRIBUTES . " pa
    where pa.products_id = '" . (int)$products_id . "'";
    }
    if ($returnCount == FALSE) {
    $attributes_query .= " limit 1";
    } else {
    $attributes_query = str_replace('select pa.products_attributes_id', 'select count(pa.products_attributes_id) as count', $attributes_query);
    }

//die($attributes_query);
$attributes = $db->Execute($attributes_query);
//if ($products_id == 179) die(print_r($attributes->fields, TRUE));
if ($returnCount == TRUE) {
return $attributes->fields['count'];
}
if ($attributes->recordCount() > 0 && $attributes->fields['products_attributes_id'] > 0) {
return true;
} else {
return false;
}
}


/includes/modules/NAME_OF_YOUR_TEMPLATE_IF_ANY/product_listing.php, line 114, edit as shown:```
        if (zen_has_product_attributes($listing->fields['products_id'][B], 'false', TRUE) > 1[/B] or PRODUCT_LIST_PRICE_BUY_NOW == '0') {

/includes/classes/shopping_cart.php
in function actionAddProduct(), line 1568 (v1.3.8a), insert the new lines as shown:```
if (isset($_POST['products_id']) && is_numeric($_POST['products_id'])) {
[B] // Add default attribute if not specified and only one attrib exists for this product
if (!isset($_POST['id']) && zen_has_product_attributes($_POST['products_id'], 'true', TRUE) == 1) {
$sql = "select distinct popt.products_options_id, popt.products_options_name
from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib
where patrib.products_id='" . (int)$_POST['products_id'] . "'
and patrib.options_id = popt.products_options_id
and popt.language_id = '" . (int)$_SESSION['languages_id'] . "' LIMIT 1";
$products_options_names = $db->Execute($sql);

    $sql = "select    pov.products_options_values_id, pov.products_options_values_name
          from      " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov
          where     pa.products_id = '" . (int)$_POST['products_id'] . "'
          and       pa.options_id = '" . (int)$products_options_names->fields['products_options_id'] . "'
          and       pa.options_values_id = pov.products_options_values_id
          and       pov.language_id = '" . (int)$_SESSION['languages_id'] . "' LIMIT 1 ";
    $products_options = $db->Execute($sql);
    if ($products_options->RecordCount() == 1) {
      $_POST['id'][$products_options_names->fields['products_options_id']] = $products_options->fields['products_options_values_id'];
    }
  }[/B]
  // verify attributes and quantity first

line 1673 change this:
function actionBuyNow($goto, $parameters) {
global $messageStack;
if (isset($_GET['products_id'])) {
if (zen_has_product_attributes($_GET['products_id'])) {
zen_redirect(zen_href_link(zen_get_info_page($_GET['products_id']), 'products_id=' . $_GET['products_id']));

} else {
$add_max = zen_get_products_quantity_order_max($_GET['products_id']);

to this:
function actionBuyNow($goto, $parameters) {
global $messageStack;
if (isset($_GET['products_id'])) {
[B] $attribCount = zen_has_product_attributes($_GET['products_id'], 'false', TRUE);
// if product has more than one attrib, go to product page. If has only one, treat as a regular actionAddProduct call
if ($attribCount > 1) {
zen_redirect(zen_href_link(zen_get_info_page($_GET['products_id']), 'products_id=' . $_GET['products_id']));
} elseif ($attribCount == 1) {
$_GET['action'] = 'add_product';
$_POST['products_id'] = $_GET['products_id'];
$_POST['cart_quantity'] = 1;
$this->actionAddProduct($goto, $parameters);
return FALSE;
[/B]
} else {
$add_max = zen_get_products_quantity_order_max($_GET['products_id']);


The above edits deal with the main product listing.

If you also want to adjust the products_new, products_all, and products_featured pages, you'll need to make a couple more edits to each of those, respectively:

/includes/templates/NAME_OF_YOUR_TEMPLATE/templates/tpl_modules_products_new_listing.php
line 85:```
        if (zen_has_product_attributes($products_new->fields['products_id'][B], 'false', TRUE) > 2[/B]) {

Same with tpl_modules_products_featured_listing.php and tpl_modules_products_all_listing.php

/includes/modules/pages/products_new/header_php.php
line 47:if (zen_has_product_attributes($check_products_all->fields['products_id'][B], 'false', TRUE) > 1[/B]) {and line 55:```
if (zen_has_product_attributes($check_products_all->fields['products_id'][B], 'false', TRUE) < 2[/B]) {

Same with the header_php.php file for the products_all and products_featured folders.

These same edits can be applied to v1.3.9 and v1.5.0; just use care since the line numbers have changed in most cases.

This functionality will be included in v2.0.0
20 Nov 2009, 9:45 AM
#5
schoolboy avatar

schoolboy

Totally Zenned

Join Date:
Jun 2005
Location:
Cumbria, UK
Posts:
10,327
Plugin Contributions:
0

Re: HOW CAN I AUTO-SELECT SINGLE-ATTRIBUTE CHOICES DURING ADD-TO-CART process?

DrByte - this is brilliant of you to put this up. I know how busy you must be with 2.0, so the time taken to publish this info is truly appreciated.

I'll certainly bookmark this page.

20 Nov 2009, 4:04 PM
#6
wweyland avatar

wweyland

New Zenner

Join Date:
Sep 2009
Posts:
15
Plugin Contributions:
0

Re: HOW CAN I AUTO-SELECT SINGLE-ATTRIBUTE CHOICES DURING ADD-TO-CART process?

Thank you very much! I haven't tested the code yet but I will do that and report how it went.

1 Dec 2009, 1:06 AM
#7
olivierk avatar

olivierk

New Zenner

Join Date:
Jan 2006
Posts:
5
Plugin Contributions:
0

Re: HOW CAN I AUTO-SELECT SINGLE-ATTRIBUTE CHOICES DURING ADD-TO-CART process?

THANK YOU Dr Byte !!!

Just spent more than 2 hours searching the forums and the net for the solution to removing the "more detail" to basic downloadable products and your mods do exactly this.

May I suggest this becomes an option in a future release, it's invaluable and much much more user friendly...

--Olivier