SOY Shopプラグインの解説 クーポン
ネットショップを運営するにあたってクーポンを利用することが多々あります。
SOY Shopではクーポンはプラグイン形式をとっており、
soyshop.discount.phpという拡張ポイントを使ってプラグインを開発します。
クーポンに関してはクーポン自由設定プラグイン(ID : discount_free_coupon)を見ていきます。
まずは構造ですが、

となっており、
拡張ポイントのほとんどがすでに話題に挙がっているため省略します。
SOY Shopプラグインの解説 独自のDAOのクラスを持つ
クーポンに関わる拡張ポイントは、
soyshop.discount.phpとなります。
/soyshop/webapp/src/logic/plugin/extensions/soyshop.discount.phpを開くと
class SOYShopDiscount implements SOY2PluginAction{
private $cart;
/**
* 割引金額の計算とモジュールの登録
*/
function doPost($param){
}
/**
* 注文処理時にクーポンコードを使用済にする
*/
function order(){
}
/**
* エラーチェック
* @return Boolean
*/
function hasError($param){
return false;
}
/*
* エラーメッセージ
* @return string
*/
function getError(){
return "";
}
/*
* カートで表示するモジュール名
* (空にするとそのモジュールは丸ごと表示されない)
*/
function getName(){
return "";
}
/*
* カートで表示するフォーム
* name="discount_module[***]"
*/
function getDescription(){
return "";
}
function getCart() {
return $this->cart;
}
function setCart($cart) {
$this->cart = $cart;
}
/**
* 割引対象とするチェック
* @return boolean
*/
function checkAddList(){
return true;
}
}
doPostからgetDiscription関数までは
簡易ポイント支払いモジュール(ID : common_point_payment)の
soyshop.point.payment.phpの拡張ポイントと同じで

getName、getDescriptionでフォームを形成し、
次へをクリックした時、
hasError、doPostと読み込まれ、
hasErrorでエラーを返した場合はdoPostが読み込まれずに同じページに遷移し、
getErrorでエラーの表記を返す。
function doPost($param){
$cart = $this->getCart();
$code = trim($param["coupon_codes"][0]);
if(isset($code)){
try{
$coupon = $this->dao->getByCouponCodeAndNoDelete($code);
}catch(Exception $e){
return;
}
$couponId = $coupon->getId();
if(isset($couponId)){
$module = new SOYShop_ItemModule();
$module->setId("discount_free_coupon");
$module->setName("クーポン");
$module->setType("discount_module");
//クーポンのタイプにより、割引額を変える
$couponType = $coupon->getCouponType();
//値引き額
if($couponType == SOYShop_Coupon::TYPE_PRICE){
$discount = $coupon->getDiscount();
//割引金額:商品合計より大きくはならない。
$discount = min($discount, $cart->getItemPrice());
//値引き率
}elseif($couponType == SOYShop_Coupon::TYPE_PERCENT){
$discount = $cart->getItemPrice() * $coupon->getDiscountPercent() / 100;
//念のため
}else{
$discount = 0;
}
$module->setPrice(0 - $discount);//負の値
if($discount > 0){
$cart->addModule($module);
}else{
$cart->removeModule("discount_free_coupon");
}
//属性の登録
$cart->setAttribute("discount_coupon.code", $code);
$cart->setOrderAttribute("discount_coupon.code", "クーポンコード", $code);
}
}
}
doPostではモジュールの登録と値引き額とコードをセッションに入れてます。
残りのcheckAllListはdiscountの拡張ポイントが読まれた時、
各ポイントの関数を読み込む前に処理を行うかどうかを指示する関数で、
function checkAllList(){
//CartLogicからセッションに放り込んでいるデータ等を調べる
//処理の結果をboolean値で返す。
return true;
}
カートにある商品を入れた場合はクーポンは使えないという処理を行うことができます。
2014.03.07 | Comments(0) | Trackback(0)
トラックバック -