Totally Zenned
- Join Date:
- Jun 2005
- Location:
- Kelowna, BC Canada
- Posts:
- 1,033
- Plugin Contributions:
- 4
Stock For Product With Different Attributes (OLD)
Couple of small mods you might be interesed in:
First... this 2-liner will pre-fill the quantity box when you are editing a stock level. This seems logical to me so that you have a visual reminder of what stock is currently entered.
in /admin/includes/products_with_attributes_stock.php, Find (Line 349 of a fresh copy)
echo '<p><strong>Quantity: </strong>'.zen_draw_input_field('quantity').'</p>'."\n";
And replace with```
echo '<p><strong>Quantity: </strong>'.zen_draw_input_field('quantity', $_GET['q']).'</p>'."\n"; //s_mack:prefill_quantity
And find (Line 458 of a fresh copy)
echo '<a href="'.zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, "action=edit&products_id=".$products->fields['products_id'].'&attributes='.$attribute_products->fields['stock_attributes'], 'NONSSL').'">Edit Quantity</a>';
And replace with
echo '<a href="'.zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, "action=edit&products_id=".$products->fields['products_id'].'&attributes='.$attribute_products->fields['stock_attributes'].'&q='.$attribute_products->fields['quantity'], 'NONSSL').'">Edit Quantity</a>'; //s_mack:prefill_quantity
The second mod is a little more involved. This skips the confirmation step for add and edit. It bugged me when I was trying to change the quantity on several items - its kinda useless anyway. The changes are to the same file as the above mod... its too sporatic to show the changes individually, so here is the file in its entirety - look for lines that end with "s_mack:noconfirm".
<?php
//
// +----------------------------------------------------------------------+
// |zen-cart Open Source E-commerce |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003 The zen-cart developers |
// | |
// | http://www.zen-cart.com/index.php |
// | |
// | Portions Copyright (c) 2003 osCommerce |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the GPL license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at the following url: |
// | http://www.zen-cart.com/license/2_0.txt. |
// | If you did not receive a copy of the zen-cart license and are unable |
// | to obtain it through the world-wide-web, please send a note to |
// | [email][email protected][/email] so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// $Id: orders.php 528 2004-11-10 15:56:02Z wilt $
//
/*
CREATE TABLE `zen_products_with_attributes_stock` (
`stock_id` INT NOT NULL AUTO_INCREMENT ,
`products_id` INT NOT NULL ,
`stock_attributes` VARCHAR( 255 ) NOT NULL ,
`quantity` INT NOT NULL ,
PRIMARY KEY ( `stock_id` )
)
*/
require('includes/application_top.php');
require(DIR_WS_CLASSES . 'currencies.php');
require(DIR_WS_CLASSES . 'products_with_attributes_stock.php');
$stock = new products_with_attributes_stock;
if(isset($_SESSION['language_id'])){ $language_id = $_SESSION['language_id'];} else { $language_id=1;}
if(isset($_GET['action']))
{
$action = $_GET['action'];
}
else
{
$action = '';
}
switch($action)
{
case 'add':
if(isset($_GET['products_id']) and is_numeric((int)$_GET['products_id']))
{
$products_id = (int)$_GET['products_id'];
}
if(isset($_POST['products_id']) and is_numeric((int)$_POST['products_id']))
{
$products_id = (int)$_POST['products_id'];
}
if(isset($products_id))
{
if(zen_products_id_valid($products_id))
{
$product_name = zen_get_products_name($products_id);
$product_attributes = $stock->get_products_attributes($products_id);
$hidden_form .= zen_draw_hidden_field('products_id',$products_id)."\n";
}
else
{
zen_redirect(zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, zen_get_all_get_params(array('action')), 'NONSSL'));
}
}
else
{
$query = 'SELECT DISTINCT
attrib.products_id, description.products_name
FROM
'.TABLE_PRODUCTS_ATTRIBUTES.' attrib, '.TABLE_PRODUCTS_DESCRIPTION.' description
WHERE
attrib.products_id = description.products_id and description.language_id='.$language_id.' order by description.products_name';
$products = $db->execute($query);
while(!$products->EOF)
{
$products_array_list[] = array(
'id' => $products->fields['products_id'],
'text' => $products->fields['products_name']
);
$products->MoveNext();
}
}
break;
case 'edit':
$hidden_form = '';
if(isset($_GET['products_id']) and is_numeric((int)$_GET['products_id']))
{
$products_id = $_GET['products_id'];
}
if(isset($_GET['attributes']))
{
$attributes = $_GET['attributes'];
}
if(isset($products_id) and isset($attributes))
{
$attributes = explode(',',$attributes);
foreach($attributes as $attribute_id){
$hidden_form .= zen_draw_hidden_field('attributes[]',$attribute_id)."\n";
$attributes_list[] = $stock->get_attributes_name($attribute_id);
}
$hidden_form .= zen_draw_hidden_field('products_id',$products_id)."\n";
}
else
{
zen_redirect(zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, zen_get_all_get_params(array('action')), 'NONSSL'));
}
break;
case 'confirm':
if(isset($_POST['products_id']) and is_numeric((int)$_POST['products_id']))
{
if(!isset($_POST['quantity']) || !is_numeric($_POST['quantity']))
{
zen_redirect(zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, zen_get_all_get_params(array('action')), 'NONSSL'));
}
$products_id = $_POST['products_id'];
$product_name = zen_get_products_name($products_id);
if(is_numeric($_POST['quantity']))
{
$quantity = $_POST['quantity'];
}
$attributes = $_POST['attributes'];
foreach($attributes as $attribute_id)
{
$hidden_form .= zen_draw_hidden_field('attributes[]',$attribute_id)."\n";
$attributes_list[] = $stock->get_attributes_name($attribute_id);
$s_mack_noconfirm .="attributes[]=" . $attribute_id . "&"; //s_mack:noconfirm
}
$hidden_form .= zen_draw_hidden_field('products_id',$products_id)."\n";
$hidden_form .= zen_draw_hidden_field('quantity',$quantity)."\n";
$s_mack_noconfirm .="products_id=" . $products_id . "&"; //s_mack:noconfirm
$s_mack_noconfirm .="quantity=" . $quantity . "&"; //s_mack:noconfirm
if(sizeof($attributes) > 1)
{
sort($attributes);
$stock_attributes = implode(',',$attributes);
}
else
{
$stock_attributes = $attributes[0];
}
$query = 'select * from '.TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK.' where products_id = '.$products_id.' and stock_attributes="'.$stock_attributes.'"';
$stock_check = $db->Execute($query);
if(!$stock_check->EOF)
{
$hidden_form .= zen_draw_hidden_field('add_edit','edit');
$hidden_form .= zen_draw_hidden_field('stock_id',$stock_check->fields['stock_id']);
$s_mack_noconfirm .="stock_id=" . $stock_check->fields['stock_id'] . "&"; //s_mack:noconfirm
$s_mack_noconfirm .="add_edit=edit&"; //s_mack:noconfirm
$add_edit = 'edit';
}
else
{
$hidden_form .= zen_draw_hidden_field('add_edit','add')."\n";
$s_mack_noconfirm .="add_edit=add&"; //s_mack:noconfirm
}
}
else
{
zen_redirect(zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, zen_get_all_get_params(array('action')), 'NONSSL'));
}
zen_redirect(zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, $s_mack_noconfirm . "action=execute", 'NONSSL')); //s_mack:noconfirm
break;
case 'execute':
$attributes = $_POST['attributes'];
if ($_GET['attributes']) { $attributes = $_GET['attributes']; } //s_mack:noconfirm
$products_id = $_POST['products_id'];
if ($_GET['products_id']) { $products_id = $_GET['products_id']; } //s_mack:noconfirm
/* s_mack:noconfirm
if(is_numeric((int)$_POST['quantity']))
{
$quantity = (int)$_POST['quantity'];
}
else
*/ s_mack:noconfirm
$quantity = $_GET['quantity']; //s_mack:noconfirm
if ($_GET['quantity']) { $quantity = $_GET['quantity']; } //s_mack:noconfirm
if(!is_numeric((int)$quantity)) //s_mack:noconfirm
{
zen_redirect(zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, zen_get_all_get_params(array('action')), 'NONSSL'));
}
if(sizeof($attributes) > 1)
{
sort($attributes);
$stock_attributes = implode(',',$attributes);
}
else
{
$stock_attributes = $attributes[0];
}
//if($_POST['add_edit'] == 'add')
if(($_POST['add_edit'] == 'add') || ($_GET['add_edit'] == 'add')) //s_mack:noconfirm
{
$query = 'insert into `'.TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK.'` (`products_id`,`stock_attributes`,`quantity`) values ('.$products_id.',"'.$stock_attributes.'",'.$quantity.')';
}
elseif(($_POST['add_edit'] == 'edit') || ($_GET['add_edit'] == 'edit')) //s_mack:noconfirm
//elseif($_POST['add_edit'] == 'edit')
{
/* s_mack:noconfirm
if(is_numeric((int)$_POST['stock_id']))
{
$stock_id = (int)$_POST['stock_id'];
}
else
*/ s_mack:noconfirm
$stock_id = $_POST['stock_id']; //s_mack:noconfirm
if ($_GET['stock_id']) { $stock_id = $_GET['stock_id']; } //s_mack:noconfirm
if(!is_numeric((int)$stock_id)) //s_mack:noconfirm
{
zen_redirect(zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, zen_get_all_get_params(array('action')), 'NONSSL'));
}
$query = 'update `'.TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK.'` set quantity='.$quantity.' where stock_id='.$stock_id.' limit 1';
}
$db->Execute($query);
$stock->update_parent_products_stock($products_id);
$messageStack->add_session('Product successfully updated', 'success');
zen_redirect(zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, '', 'NONSSL'));
break;
case 'delete':
if(!isset($_POST['confirm']))
{
// do nothing
}
else
{
// delete it
if($_POST['confirm'] == 'Yes'){
$query = 'delete from '.TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK.' where products_id="'.$_POST['products_id'].'" and stock_attributes="'.$_POST['attributes'].'" limit 1';
$db->Execute($query);
$stock->update_parent_products_stock((int)$_POST['products_id']);
$messageStack->add_session('Product Variant was deleted', 'failure');
zen_redirect(zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, '', 'NONSSL'));
} else {
zen_redirect(zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, '', 'NONSSL'));
}
}
break;
case 'resync':
if(is_numeric((int)$_GET['products_id'])){
$stock->update_parent_products_stock((int)$_GET['products_id']);
$messageStack->add_session('Parent Product Quantity Updated', 'success');
zen_redirect(zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, '', 'NONSSL'));
} else {
zen_redirect(zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, '', 'NONSSL'));
}
break;
default:
// Show a list of the products
break;
}
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<link rel="stylesheet" type="text/css" href="includes/cssjsmenuhover.css" media="all" id="hoverJS">
<script language="javascript" type="text/javascript" src="includes/menu.js"></script>
<script language="javascript" type="text/javascript" src="includes/general.js"></script>
<script type="text/javascript">
<!--
function init()
{
cssjsmenu('navbar');
if (document.getElementById)
{
var kill = document.getElementById('hoverJS');
kill.disabled = true;
}
}
// -->
</script>
</head>
<body onload="init()">
<!-- header //-->
<?php
require(DIR_WS_INCLUDES . 'header.php');
?>
<!-- header_eof //-->
<div style="padding: 20px;">
<!-- body_text_eof //-->
<!-- body_eof //-->
<?
switch($action)
{
case 'add':
if(isset($products_id))
{
echo zen_draw_form('final_refund_exchange', FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, 'action=confirm', 'post', '', true)."\n";
echo $hidden_form;
echo '<p><strong>'.$product_name.'</strong></p>';
foreach($product_attributes as $option_name => $options)
{
echo '<p><strong>'.$option_name.': </strong>';
echo zen_draw_pull_down_menu('attributes[]',$options).'</p>'."\n";
}
echo '<p><strong>Quantity: </strong>'.zen_draw_input_field('quantity').'</p>'."\n";
}
else
{
echo zen_draw_form('final_refund_exchange', FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, 'action=add', 'post', '', true)."\n";
echo zen_draw_pull_down_menu('products_id',$products_array_list)."\n";
}
?>
<p><input type="submit" value="submit"></p>
</form>
<?
break;
case 'edit':
echo zen_draw_form('final_refund_exchange', FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, 'action=confirm', 'post', '', true)."\n";
echo '<h3>'.zen_get_products_name($products_id).'</h3>';
foreach($attributes_list as $attributes)
{
echo '<p><strong>'.$attributes['option'].': </strong>'.$attributes['value'].'</p>';
}
echo $hidden_form;
echo '<p><strong>Quantity: </strong>'.zen_draw_input_field('quantity').'</p>'."\n";
?>
<p><input type="submit" value="submit"></p>
</form>
<?
break;
case 'delete':
if(!isset($_POST['confirm']))
{
echo zen_draw_form('final_refund_exchange', FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, 'action=delete', 'post', '', true)."\n";
echo 'Are you sure you want to delete this product?';
foreach($_GET as $key=>$value)
{
echo zen_draw_hidden_field($key,$value);
}
?>
<p><input type="submit" value="Yes" name="confirm"> * <input type="submit" value="No" name="confirm"></p>
</form>
<?
}
break;
case 'confirm':
echo '<h3>Confirm '.$product_name.'</h3>';
foreach($attributes_list as $attributes)
{
echo '<p><strong>'.$attributes['option'].': </strong>'.$attributes['value'].'</p>';
}
echo '<p><strong>Quantity</strong>'.$quantity.'</p>';
echo zen_draw_form('final_refund_exchange', FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, 'action=execute', 'post', '', true)."\n";
echo $hidden_form;
?>
<p><input type="submit" value="submit"></p>
</form>
<?
break;
default:
?>
<table width="100%" style="text-align: left">
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Quantity of all variants</th>
<th>Add Quantity For Product Variant</th>
<th>Sync Quantities</th>
</tr>
<?
// Show a list of the products
$query = 'select distinct
attrib.products_id, description.products_name, products.products_quantity
FROM
'.TABLE_PRODUCTS_ATTRIBUTES.' attrib, '.TABLE_PRODUCTS_DESCRIPTION.' description, '.TABLE_PRODUCTS.' products
WHERE
attrib.products_id = description.products_id and
attrib.products_id = products.products_id and description.language_id='.$language_id.' order by description.products_name ';
$products = $db->Execute($query);
while(!$products->EOF)
{
echo '<tr style="background: #ccc">'."\n";
echo '<td>'.$products->fields['products_id'].'</td>';
echo '<td>'.$products->fields['products_name'].'</td>';
echo '<td>'.$products->fields['products_quantity'].'</td>';
echo '<td><a href="'.zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, "action=add&products_id=".$products->fields['products_id'], 'NONSSL').'">Add Quantity For Product Variant</a></td>';
echo '<td><a href="'.zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, "action=resync&products_id=".$products->fields['products_id'], 'NONSSL').'">Sync Quantities</a></td>';
echo '</tr>'."\n";
$query = 'select * from '.TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK.' where products_id="'.$products->fields['products_id'].'"';
$attribute_products = $db->Execute($query);
if($attribute_products->RecordCount() > 0)
{
echo '<tr>'."\n";
echo '<td colspan="5" style="padding:10px;">'."\n";
echo '<table width="100%" style="text-align: left">';
echo '<tr>';
echo '<th style="border-bottom: 1px solid #ccc">Stock id</th><th style="border-bottom: 1px solid #ccc">Variant</th><th style="border-bottom: 1px solid #ccc">Quantity in stock</th><th style="border-bottom: 1px solid #ccc">Edit</th><th style="border-bottom: 1px solid #ccc">Delete</th>';
echo '</tr>';
while(!$attribute_products->EOF)
{
echo '<tr>';
echo '<td style="border-bottom: 1px dashed #ccc">'."\n";
echo $attribute_products->fields['stock_id'];
echo '</td>'."\n";
echo '<td style="border-bottom: 1px dashed #ccc">'."\n";
$attributes_of_stock = explode(',',$attribute_products->fields['stock_attributes']);
$attributes_output = array();
foreach($attributes_of_stock as $attri_id)
{
$stock_attribute = $stock->get_attributes_name($attri_id);
$attributes_output[] = '<strong>'.$stock_attribute['option'].':</strong> '.$stock_attribute['value'].'<br/>';
}
sort($attributes_output);
echo implode("\n",$attributes_output);
echo '</td>'."\n";
echo '<td style="border-bottom: 1px dashed #ccc">'."\n";
echo $attribute_products->fields['quantity'];
echo '</td>'."\n";
echo '<td style="border-bottom: 1px dashed #ccc">'."\n";
echo '<a href="'.zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, "action=edit&products_id=".$products->fields['products_id'].'&attributes='.$attribute_products->fields['stock_attributes'], 'NONSSL').'">Edit Quantity</a>';
echo '</td>'."\n";
echo '<td style="border-bottom: 1px dashed #ccc">'."\n";
echo '<a href="'.zen_href_link(FILENAME_PRODUCTS_WITH_ATTRIBUTES_STOCK, "action=delete&products_id=".$products->fields['products_id'].'&attributes='.$attribute_products->fields['stock_attributes'], 'NONSSL').'">Delete Variant</a>';
echo '</td>'."\n";
echo '</tr>';
$attribute_products->MoveNext();
}
echo '</table>';
echo '</td>'."\n";
echo '</tr>'."\n";
}
$products->MoveNext();
}
?>
</table>
- Steven