$usces->get_order_point

注文での獲得ポイント数を取得(計算)するメソッド。

主な使われている場所

usces_get_confirm_rows() > $usces->set_cart_fees() > $usces->get_order_point()
wc_confirm_page.phpで、カートにある商品明細を表示する際の基礎データをセットする処理($usces->set_cart_fees)の1つとして獲得ポイント数を計算している。

$usces->use_point() > $usces->point_check() > $usces->set_cart_fees() > $usces->get_order_point()
wc_confirm_page.phpで、注文者が「ポイントを使う」ボタンを押した時のPOST送信処理($usces->use_point)において、使用ポイント数が適正な数値かをチェックする際に使われている。

<?php
/**
 * Welcart e-Commerce ver2.11.10
 * 
 * @param string $mem_id 会員ID
 * @param string $display_mode 
 *               - Promotionsale   : キャンペーン中
 *               - Usualsale       : 通常営業中
 *               - Maintenancemode : メンテナンス中
 * @param array $cart  カート内商品の配列
 * @return float $point  獲得ポイント数
 */
public function get_order_point( $mem_id = '', $display_mode = '', $cart = array() ) {
	if ( '' == $mem_id || 'deactivate' == $this->options['membersystem_state'] || 'deactivate' == $this->options['membersystem_point'] ) { // 会員IDが空、または、会員システム非利用、または、会員ポイント非利用なら
		return 0; //ポイント数0を返す
	}

	if ( empty( $cart ) ) { // 引数$cartが空なら
		$cart = $this->cart->get_cart(); // usces_cartクラスのget_cart()で取得
	}
	if ( empty( $display_mode ) ) { // 引数$display_modeが空なら
		$display_mode = $this->options['display_mode']; // optionsデータから取得
	}
	$point = 0; // 戻り値$pointを初期化
	$total = $this->get_total_price( $cart ); // 商品代金合計金額を取得
	if ( 'Promotionsale' == $display_mode ) { // display_modeがキャンペーン中の場合の処理
		if ( 'discount' == $this->options['campaign_privilege'] ) { // キャンペーン特典が「値引き」の場合の処理
			foreach ( $cart as $rows ) { // カート内商品について、順番に獲得ポイント数を計算して加算していく
				$cats = $this->get_post_term_ids( $rows['post_id'], 'category' ); // 商品に設定されているカテゴリーidを配列で取得
				if ( ! in_array( $this->options['campaign_category'], $cats ) ) { // 商品がキャンペーン対象カテゴリーでないなら(※否定していることに注意)。
					$product = wel_get_product( $rows['post_id'] ); // 商品データを取得
					$rate    = (float) $product['itemPointrate']; // 商品に設定されているポイント率を取得
					$price   = (float) $rows['price'] * (float) $rows['quantity'];
					$point   = (float) sprintf( '%.3f', $point + ( $price * $rate / 100 ) ); // 商品に設定されたポイント率で獲得ポイント数を計算して加算する
				}
			}
		} elseif ( 'point' == $this->options['campaign_privilege'] ) { // キャンペーン特典が「ポイント」の場合の処理
			foreach ( $cart as $rows ) {
				$product = wel_get_product( $rows['post_id'] );
				$rate    = (float) $product['itemPointrate'];
				$price   = (float) $rows['price'] * (float) $rows['quantity'];
				$cats    = $this->get_post_term_ids( $rows['post_id'], 'category' );
				if ( in_array( $this->options['campaign_category'], $cats ) ) {
					$point = (float) sprintf( '%.3f', $point + ( $price * $rate / 100 * (float) $this->options['privilege_point'] ) );
				} else {
					$point = (float) sprintf( '%.3f', $point + ( $price * $rate / 100 ) );
				}
			}
		}
	} else { // display_modeがキャンペーン中以外の場合の処理
		foreach ( $cart as $rows ) { // カート内商品について、順番に獲得ポイント数を計算して加算していく
			$product = wel_get_product( $rows['post_id'] ); // 商品データを取得
			$rate    = (float) $product['itemPointrate']; // 商品に設定されているポイント率を取得
			$price   = (float) $rows['price'] * (float) $rows['quantity'];
			$point   = (float) sprintf( '%.3f', $point + ( $price * $rate / 100 ) ); // 商品に設定されたポイント率で獲得ポイント数を計算して加算する
		}
	}

	$entry     = $this->cart->get_entry(); // $_SESSION['usces_entry']に設定されたデータを取得
	$use_point = isset( $entry['order']['usedpoint'] ) ? (int) $entry['order']['usedpoint'] : 0; // 使用ポイント数を取得
	if ( 0 < $use_point ) { // ポイントを使っている場合の処理
		$point = (float) sprintf( '%.3f', $point - ( $point * $use_point / $total ) ); // 総獲得ポイント数から使用ポイント分を差し引く。
		$point = ceil( $point ); // 獲得ポイント数の小数点以下を切り上げ
		if ( 0 > $point ) { // 獲得ポイント数が負数なら0を代入する
			$point = 0;
		}
	} else { // ポイント不使用の場合の処理
		if ( 0 < $point ) { // 獲得ポイント数があれば
			$point = ceil( $point ); // 獲得ポイント数の小数点以下を切り上げ
		}
	}

	return apply_filters( 'usces_filter_get_order_point', $point, $mem_id, $display_mode, $cart );
}

プログラムの流れ

例外(ポイント計算不要の場合)処理

引数バリデーション

関数内で使う変数セット

獲得ポイント数計算処理
 キャンペーン中の場合の処理
  キャンペーン特典が「値引き」の場合の処理
  キャンペーン特典が「ポイント」の場合の処理
 キャンペーン中以外の場合の処理

使用ポイント数を取得

獲得ポイント数から使用ポイント数分を差し引く
 ポイントを使っている場合の処理
 ポイント不使用の場合の処理

獲得ポイント数を返す

注釈

30行目
否定していること(false)に注意。「商品がキャンペーン対象カテゴリーでないなら」という条件である。
つまり、キャンペーン特典が「値引き」の場合、キャンペーン中は、キャンペーン対象カテゴリー商品はポイントを獲得できない。
「値引きしてるんだからポイントはあげないよ」という考え方だ。

62行目
分かりにくい式だが、総獲得ポイント数から使用ポイント分を差し引いている。
welcartは商品ごと個別にポイント率を設定することもできる。商品によってポイント率が異なる可能性があるので、この計算式になっている。
$point * $use_point / $total

$use_point / 1 * $point / $total

$use_point * $point / $total
となり、総獲得ポイント数の中の使用ポイント分を求めている。
結果として、「商品合計金額-使用ポイント数」に対してポイント率を適用したことになる。

hook

usces_filter_get_order_point
戻り値(獲得ポイント数)を返す局面でのfilter。再計算してカスタマイズできる。



コメント

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です