//获取属性
$goods = $GLOBALS['db']->getRow("SELECT goods_sn, shop_price FROM ".$GLOBALS['ecs']->table("goods")." WHERE goods_id=".$goods_id);
$goods_sn = $goods['goods_sn'];
$shop_price = $goods['shop_price'];
$sql = "SELECT goods_attr_id, attr_id FROM ".$GLOBALS['ecs']->table("goods_attr")." WHERE goods_id=".$goods_id;
$attr_ids = $GLOBALS['db']->getAll($sql);
$attr_ids_arr = array();
foreach ($attr_ids as $k => $v) {
$attr_ids_arr[$v['attr_id']][] = $v['goods_attr_id'];
}
rsort($attr_ids_arr);
//获取已有的货品
$old_products_res = $GLOBALS['db']->query("SELECT goods_attr FROM ".$GLOBALS['ecs']->table("products")." WHERE goods_id=".$goods_id);
$old_products = array();
while($row = $GLOBALS['db']->fetchRow($old_products_res)){
$old_products[] = $row['goods_attr'];
}
//属性分组统计
if($attr_ids_arr){
$products = make_str($attr_ids_arr);
if($old_products){
//如果已有并且有新增
foreach ($products as $ok => $ov) {
if(!in_array($ov, $old_products)){
$GLOBALS['db']->query("INSERT INTO ".$GLOBALS['ecs']->table("products")." (goods_id, goods_attr, product_number, product_shop_price) ".
" VALUES(".$goods_id.", '".$ov."', 1000, ".$shop_price.")");
//补齐货品号
$product_id = $GLOBALS['db']->insert_id();
$product_sn = $goods_sn .'g_p'.$product_id;
$GLOBALS['db']->query("UPDATE ".$GLOBALS['ecs']->table("products")." SET product_sn='".$product_sn."' WHERE product_id=".$product_id);
}
}
//如果已有并且有删除
foreach ($old_products as $ok => $ov) {
if(!in_array($ov, $products)){
$GLOBALS['db']->query("DELETE FROM ".$GLOBALS['ecs']->table("products")." WHERE goods_attr='".$ov."' AND goods_id=".$goods_id);
}
}
}else{
//新增
foreach ($products as $k => $v) {
$GLOBALS['db']->query("INSERT INTO ".$GLOBALS['ecs']->table("products")." (goods_id, goods_attr, product_number, product_shop_price) ".
" VALUES(".$goods_id.", '".$v."', 1000, ".$shop_price.")");
//补齐货品号
$product_id = $GLOBALS['db']->insert_id();
$product_sn = $goods_sn .'g_p'.$product_id;
$GLOBALS['db']->query("UPDATE ".$GLOBALS['ecs']->table("products")." SET product_sn='".$product_sn."' WHERE product_id=".$product_id);
}
}
}
//echo json_encode($products);
}
function make_str($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return array();
}
if ($i == count($arrays) - 1) { //最后一个直接返回
return $arrays[$i];
}
$tmp = make_str($arrays, $i + 1); //获取无限下一组的组合
$result = array();
foreach ($arrays[$i] as $v) { // 本数组和下一组进行组合
foreach ($tmp as $t) {
if(is_array($t))
{
$result[] = implode(',',array_merge(array($v), $t));
}
else
{
$result[] = $t."|".$v;
}
}
}
return $result;
}
将 recombination_attribute($goods_id)带入act="insert"里面
然后找到spec_price()
function spec_price($spec)
{
if (!empty($spec))
{
if(is_array($spec))
{
foreach($spec as $key=>$val)
{
$spec[$key]=addslashes($val);
}
}
else
{
$spec=addslashes($spec);
}
$where = db_create_in($spec, 'goods_attr_id');
$sql = 'SELECT SUM(attr_price) AS attr_price FROM ' . $GLOBALS['ecs']->table('goods_attr') . " WHERE $where";
$price = floatval($GLOBALS['db']->getOne($sql));
}
else
{
$price = 0;
}
return $price;
}
修改为:
function spec_price($spec, $goods_id)
{
if (!empty($spec))
{
if(is_array($spec))
{
foreach($spec as $key=>$val)
{
$spec[$key]=addslashes($val);
}
$spec_str = implode("|", $spec);
}
else
{
$spec=addslashes($spec);
$spec_str = $spec;
}
/*$where = db_create_in($spec, 'goods_attr_id');
$sql = 'SELECT SUM(attr_price) AS attr_price FROM ' . $GLOBALS['ecs']->table('goods_attr') . " WHERE $where";
*/
//2016-9-3 属性价格改变
$shop_price = $GLOBALS['db']->getOne("SELECT shop_price FROM ".$GLOBALS['ecs']->table("goods")." WHERE goods_id=".$goods_id);
$product_shop_price = $GLOBALS['db']->getOne("SELECT product_shop_price FROM ".$GLOBALS['ecs']->table("products")." WHERE goods_id=".$goods_id." AND goods_attr='".$spec_str."' ");
$price = $product_shop_price - $shop_price;
//2016-9-3 属性价格改变
}
else
{
$price = 0;
}
return $price;
}
再然后所有用到改方法的地方改为spec_price($spec, $goods_id)