ECcube 無料商品の購入個数を1つのみに制限するハック
ゼロ円商品の購入数に制限を設ける場合の手法です。ゼロ円のサンプル商品を大量に注文されても困りますので。
場所
/data/class/SC_CartSession.php
215行目付近 追加の時に、ゼロ円のものは強制的に1つにしてしまいます。
if ($_SESSION[$this->key][$i]['price'] === "0"){
$_SESSION[$this->key][$i]['quantity'] = 1;
}else{
$_SESSION[$this->key][$i]['quantity']+= $quantity;
}
if(!empty($campaign_id)){
$_SESSION[$this->key][$i]['campaign_id'] = $campaign_id;
$_SESSION[$this->key][$i]['is_campaign'] = true;
}
}
$find = true;
}
}
if(!$find) {
$price ="1";
if ($classcategory_id1 === "0"){
$arrProductsClass = array();
$objQuery = new SC_Query();
$col = "price02";
$table = "vw_cross_products_class";
$where = "product_id = ?";
$objQuery->setorder("rank1 DESC, rank2 DESC");
$arrProductsClass = $objQuery->select($col, $table, $where, array($id[0]));
$price =$arrProductsClass[0]['price02'];
}else if($classcategory_id1 !== "0"){
$arrProductsClass = array();
$objQuery = new SC_Query();
$col = "price02";
$table = "vw_cross_products_class";
$where = "product_id = ? and classcategory_id1 = ?";
$objQuery->setorder("rank1 DESC, rank2 DESC");
$arrProductsClass = $objQuery->select($col, $table, $where, array($id[0],$id[1]));
$price =$arrProductsClass[0]['price02'];
}else if($classcategory_id2 !== "0"){
$arrProductsClass = array();
$objQuery = new SC_Query();
$col = "price02";
$table = "vw_cross_products_class";
$where = "product_id = ? and classcategory_id2 = ?";
$objQuery->setorder("rank1 DESC, rank2 DESC");
$arrProductsClass = $objQuery->select($col, $table, $where, array($id[0],$id[2]));
$price =$arrProductsClass[0]['price02'];
}
$_SESSION[$this->key][$max+1]['id'] = $id;
if ($price === "0"){
$_SESSION[$this->key][$max+1]['quantity'] = 1;
}else{
$_SESSION[$this->key][$max+1]['quantity'] = $quantity;
}
ここの if ($price === "0"){以下の部分を
if ($price === "0"){
$find2 = false;
for($i = 0; $i <= $max; $i++) {
if($_SESSION[$this->key][$i]['price'] === "0") {$find2 = true;}
}
if(!$find2){
$_SESSION[$this->key][$max+1]['id'] = $id;
$_SESSION[$this->key][$max+1]['quantity'] = 1;
$_SESSION[$this->key][$max+1]['cart_no'] = $this->getNextCartID();
if(!empty($campaign_id)){
$_SESSION[$this->key][$max+1]['campaign_id'] = $campaign_id;
$_SESSION[$this->key][$max+1]['is_campaign'] = true;
}
}
}else{
$_SESSION[$this->key][$max+1]['id'] = $id;
$_SESSION[$this->key][$max+1]['quantity'] = $quantity;
$_SESSION[$this->key][$max+1]['cart_no'] = $this->getNextCartID();
if(!empty($campaign_id)){
$_SESSION[$this->key][$max+1]['campaign_id'] = $campaign_id;
$_SESSION[$this->key][$max+1]['is_campaign'] = true;
}
}
こうすれば、他のゼロ円商品がすでにカートにある場合は、新たなゼロ円商品をカートに追加できません。
323行目付近 if文を追加 個数を増加させません。
// 個数の増加
function upQuantity($cart_no) {
$max = $this->getMax();
for($i = 0; $i <= $max; $i++) {
if($_SESSION[$this->key][$i]['cart_no'] == $cart_no) {
if(strlen($_SESSION[$this->key][$i]['quantity'] + 1) <= INT_LEN) {
if ($_SESSION[$this->key][$i]['price'] !== "0"){$_SESSION[$this->key][$i]['quantity']++;}
}
}
}
}
これで、価格がゼロの時には、購入数が1つに限定されます。
もっと洗練された良い方法があるかもしれません。
なお、不具合については特にチェックしていませんのでよくテストをした方が良いと思われます。
ハックの適用は自己責任でお願いします。