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:
Code:
/*
* 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:
Code:
if (zen_has_product_attributes($listing->fields['products_id'], 'false', TRUE) > 1 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:
Code:
if (isset($_POST['products_id']) && is_numeric($_POST['products_id'])) {
// 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'];
}
}
// verify attributes and quantity first
line 1673 change this:
Code:
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:
Code:
function actionBuyNow($goto, $parameters) {
global $messageStack;
if (isset($_GET['products_id'])) {
$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;
} 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:
Code:
if (zen_has_product_attributes($products_new->fields['products_id'], 'false', TRUE) > 2) {
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:
Code:
if (zen_has_product_attributes($check_products_all->fields['products_id'], 'false', TRUE) > 1) {
and line 55:
Code:
if (zen_has_product_attributes($check_products_all->fields['products_id'], 'false', TRUE) < 2) {
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
Bookmarks