OSDN Git Service

Eコマース機能更新。
authornaoki hirata <naoki@magic3.org>
Mon, 25 Dec 2017 04:03:38 +0000 (13:03 +0900)
committernaoki hirata <naoki@magic3.org>
Mon, 25 Dec 2017 04:03:38 +0000 (13:03 +0900)
include/addons/eclib/ecLib.php
include/addons/eclib/ecLibDb.php
include/wp/commerceApi.php
include/wp/contentApi.php
include/wp/wcPaymentGateway.php [new file with mode: 0644]

index 63be046..89b2742 100644 (file)
@@ -274,9 +274,21 @@ class ecLib
         * @param function      $callback                       コールバック関数
         * @return                      なし
         */
-       function getActiveDelivMethod($langId, $callback)
+//     function getActiveDelivMethod($langId, $callback)
+//     {
+//             $this->db->getActiveDelivMethod($langId, 0/*セットID*/, $callback);
+//     }
+       /**
+        * 実行可能な支払方法を取得
+        *
+        * @param string $langId                言語
+        * @param array  $rows                  取得レコード
+        * @return bool                                 1行以上取得 = true, 取得なし= false
+        */
+       function getActivePayMethodRows($langId, &$rows)
        {
-               $this->db->getActiveDelivMethod($langId, 0/*セットID*/, $callback);
+               $status = $this->db->getActivePayMethodRows($langId, 0/*セットID*/, $rows);
+               return $status;
        }
        /**
         * 商品情報を取得
index 8d95ecf..90a7f3c 100644 (file)
@@ -612,7 +612,7 @@ class ecLibDb extends BaseDb
                $queryStr .=   'AND do_visible = true ';                        // 表示状態
                $queryStr .=   'AND do_language_id = ? ';
                $queryStr .=   'AND do_set_id = ? ';
-               $queryStr .=   'ORDER BY do_index ';
+               $queryStr .= 'ORDER BY do_index ';
                $retValue = $this->selectRecords($queryStr, array($langId, $setId), $rows);
                return $retValue;
        }
@@ -624,7 +624,7 @@ class ecLibDb extends BaseDb
         * @param function      $callback                       コールバック関数
         * @return                      なし
         */
-       function getActiveDelivMethod($langId, $setId, $callback)
+/*     function getActiveDelivMethod($langId, $setId, $callback)
        {
                $queryStr = 'SELECT * FROM delivery_method_def ';
                $queryStr .=  'WHERE do_deleted = false ';
@@ -633,6 +633,25 @@ class ecLibDb extends BaseDb
                $queryStr .=  'AND do_set_id = ? ';
                $queryStr .=  'ORDER BY do_index ';
                $this->selectLoop($queryStr, array($langId, $setId), $callback);
+       }*/
+       /**
+        * 実行可能な支払方法を取得
+        *
+        * @param string $langId                言語
+        * @param int    $setId                 セットID
+        * @param array  $rows                  取得レコード
+        * @return bool                                 1行以上取得 = true, 取得なし= false
+        */
+       function getActivePayMethodRows($langId, $setId, &$rows)
+       {
+               $queryStr  = 'SELECT * FROM pay_method_def LEFT JOIN _iwidgets ON po_iwidget_id = iw_id AND iw_deleted = false ';
+               $queryStr .= 'WHERE po_deleted = false ';
+               $queryStr .=   'AND po_visible = true ';                        // 表示状態
+               $queryStr .=   'AND po_language_id = ? ';
+               $queryStr .=   'AND po_set_id = ? ';
+               $queryStr .= 'ORDER BY po_index ';
+               $retValue = $this->selectRecords($queryStr, array($langId, $setId), $rows);
+               return $retValue;
        }
        /**
         * 商品情報を取得
index f7c8162..6151915 100644 (file)
@@ -16,6 +16,8 @@
  * @link       http://www.magic3.org
  */
 require_once(M3_SYSTEM_INCLUDE_PATH . '/common/baseApi.php');
+require_once($gEnvManaber->getWordpressRootPath() . '/wcShippingMethod.php');          // 配送方法クラス
+require_once($gEnvManaber->getWordpressRootPath() . '/wcPaymentGateway.php');          // 支払い方法クラス
 
 class CommerceApi extends BaseApi
 {
@@ -24,8 +26,10 @@ class CommerceApi extends BaseApi
        private $langId;                                // コンテンツの言語
        private $userId;                                // 現在のユーザID
        private $delivMethodRows;               // 配送方法レコード
+       private $payMethodRows;                 // 支払方法レコード
        const ADDON_OBJ_COMMERCE = 'eclib';             // EコマースアドオンオブジェクトID
        const DELIVERY_METHOD_CLASS = 'WCShippingMethod';                       // 配送方法クラス名(共通)
+       const PAY_METHOD_CLASS = 'WCPaymentGateway';                    // 支払方法クラス名(共通)
        
        /**
         * コンストラクタ
@@ -59,6 +63,20 @@ class CommerceApi extends BaseApi
                return $this->delivMethodRows;
        }
        /**
+        * 支払方法の取得
+        *
+        * @return array                支払方法レコード
+        */
+       function _getPayMethodRows()
+       {
+               if (!isset($this->payMethodRows)){
+                       $this->payMethodRows = array();
+                       $status = $this->ecObj->getActivePayMethodRows($this->langId, $rows);
+                       if ($status) $this->payMethodRows = $rows;
+               }
+               return $this->payMethodRows;
+       }
+       /**
         * 配送方法の総数取得
         *
         * @return int                                  配送方法数
@@ -174,6 +192,22 @@ class CommerceApi extends BaseApi
                return $cost;
        }
        /**
+        * 支払方法クラスを取得
+        *
+        * @return array                                支払方法、支払方法クラスの連想配列
+        */
+       function getPayMethodClass()
+       {
+               $methodArray = array();
+               $rows = $this->_getPayMethodRows();
+               $rowCount = count($rows);
+               for ($i = 0; $i < $rowCount; $i++){
+                       $row = $rows[$i];
+                       $methodArray[$row['po_id']] = self::PAY_METHOD_CLASS/*支払方法クラス名(共通)*/;
+               }
+               return $methodArray;
+       }
+       /**
         * インナーウィジェットを出力を取得
         *
         * @param string $id            ウィジェットID+インナーウィジェットID
index 55e1dbd..62f9a3a 100644 (file)
@@ -179,25 +179,22 @@ class ContentApi extends BaseApi
                                add_filter('woocommerce_return_to_shop_redirect', array($this, 'getShopUrl'));          // ショップホーム(product)画面へのURL取得
                                
                                require_once($this->gEnv->getWordpressRootPath() . '/plugins/woocommerce/woocommerce.php');
-                               require_once($this->gEnv->getWordpressRootPath() . '/wcShippingMethod.php');            // 配送方法クラス
+//                             require_once($this->gEnv->getWordpressRootPath() . '/wcShippingMethod.php');            // 配送方法クラス
                                                        
-                               // セッションデータを保存
+                               // セッションデータ保存処理を登録
                                $this->gRequest->addSessionCloseEventCallback(function (){
                                        global $gRequestManager;
                                        global $woocommerce;
                                        
                                        $sessionObj = $woocommerce->session->getSessionObj();
-                               //      $sessionObj = array_filter($sessionObj);                // 空要素削除
                                        if (empty($sessionObj)){                // データがない場合はセッションデータを削除
-                                               //$this->gRequest->unsetSessionValue(M3_WC_SESSION);
                                                $gRequestManager->unsetSessionValue(M3_WC_SESSION);
                                        } else {
-                                               //$this->gRequest->setSessionValueWithSerialize(M3_WC_SESSION, $sessionObj);
                                                $gRequestManager->setSessionValueWithSerialize(M3_WC_SESSION, $sessionObj);
                                        }
                                });
                                
-                               // EコマースAPIオブジェクト作成
+                               // Eコマース関連クラス読み込み、EコマースAPIオブジェクト作成
                                require_once($this->gEnv->getWordpressRootPath() . '/commerceApi.php'); // EコマースAPI
                                $gCommerceApi = new CommerceApi();
                                
diff --git a/include/wp/wcPaymentGateway.php b/include/wp/wcPaymentGateway.php
new file mode 100644 (file)
index 0000000..c5294b3
--- /dev/null
@@ -0,0 +1,266 @@
+<?php
+
+if ( ! defined( 'ABSPATH' ) ) {
+       exit; // Exit if accessed directly
+}
+
+/**
+ * Cash on Delivery Gateway.
+ *
+ * Provides a Cash on Delivery Payment Gateway.
+ *
+ * @class              WC_Gateway_COD
+ * @extends            WC_Payment_Gateway
+ * @version            2.1.0
+ * @package            WooCommerce/Classes/Payment
+ * @author             WooThemes
+ */
+class WCPaymentGateway extends WC_Payment_Gateway {
+
+       /**
+        * Constructor for the gateway.
+        */
+       public function __construct() {
+               // Setup general properties
+               $this->setup_properties();
+
+               // Load the settings
+               $this->init_form_fields();
+               $this->init_settings();
+
+               // Get settings
+               $this->title              = $this->get_option( 'title' );
+               $this->description        = $this->get_option( 'description' );
+               $this->instructions       = $this->get_option( 'instructions' );
+               $this->enable_for_methods = $this->get_option( 'enable_for_methods', array() );
+               $this->enable_for_virtual = $this->get_option( 'enable_for_virtual', 'yes' ) === 'yes' ? true : false;
+
+               add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
+               add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) );
+               add_filter( 'woocommerce_payment_complete_order_status', array( $this, 'change_payment_complete_order_status' ), 10, 3 );
+
+               // Customer Emails
+               add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
+       }
+
+       /**
+        * Setup general properties for the gateway.
+        */
+       protected function setup_properties() {
+               $this->id                 = 'cod';
+               $this->icon               = apply_filters( 'woocommerce_cod_icon', '' );
+               $this->method_title       = __( 'Cash on delivery', 'woocommerce' );
+               $this->method_description = __( 'Have your customers pay with cash (or by other means) upon delivery.', 'woocommerce' );
+               $this->has_fields         = false;
+       }
+
+       /**
+        * Initialise Gateway Settings Form Fields.
+        */
+       public function init_form_fields() {
+               $shipping_methods = array();
+
+               foreach ( WC()->shipping()->load_shipping_methods() as $method ) {
+                       $shipping_methods[ $method->id ] = $method->get_method_title();
+               }
+
+               $this->form_fields = array(
+                       'enabled' => array(
+                               'title'       => __( 'Enable/Disable', 'woocommerce' ),
+                               'label'       => __( 'Enable cash on delivery', 'woocommerce' ),
+                               'type'        => 'checkbox',
+                               'description' => '',
+                               'default'     => 'no',
+                       ),
+                       'title' => array(
+                               'title'       => __( 'Title', 'woocommerce' ),
+                               'type'        => 'text',
+                               'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ),
+                               'default'     => __( 'Cash on delivery', 'woocommerce' ),
+                               'desc_tip'    => true,
+                       ),
+                       'description' => array(
+                               'title'       => __( 'Description', 'woocommerce' ),
+                               'type'        => 'textarea',
+                               'description' => __( 'Payment method description that the customer will see on your website.', 'woocommerce' ),
+                               'default'     => __( 'Pay with cash upon delivery.', 'woocommerce' ),
+                               'desc_tip'    => true,
+                       ),
+                       'instructions' => array(
+                               'title'       => __( 'Instructions', 'woocommerce' ),
+                               'type'        => 'textarea',
+                               'description' => __( 'Instructions that will be added to the thank you page.', 'woocommerce' ),
+                               'default'     => __( 'Pay with cash upon delivery.', 'woocommerce' ),
+                               'desc_tip'    => true,
+                       ),
+                       'enable_for_methods' => array(
+                               'title'             => __( 'Enable for shipping methods', 'woocommerce' ),
+                               'type'              => 'multiselect',
+                               'class'             => 'wc-enhanced-select',
+                               'css'               => 'width: 400px;',
+                               'default'           => '',
+                               'description'       => __( 'If COD is only available for certain methods, set it up here. Leave blank to enable for all methods.', 'woocommerce' ),
+                               'options'           => $shipping_methods,
+                               'desc_tip'          => true,
+                               'custom_attributes' => array(
+                                       'data-placeholder' => __( 'Select shipping methods', 'woocommerce' ),
+                               ),
+                       ),
+                       'enable_for_virtual' => array(
+                               'title'             => __( 'Accept for virtual orders', 'woocommerce' ),
+                               'label'             => __( 'Accept COD if the order is virtual', 'woocommerce' ),
+                               'type'              => 'checkbox',
+                               'default'           => 'yes',
+                       ),
+          );
+       }
+
+       /**
+        * Check If The Gateway Is Available For Use.
+        *
+        * @return bool
+        */
+       public function is_available() {
+               $order          = null;
+               $needs_shipping = false;
+
+               // Test if shipping is needed first
+               if ( WC()->cart && WC()->cart->needs_shipping() ) {
+                       $needs_shipping = true;
+               } elseif ( is_page( wc_get_page_id( 'checkout' ) ) && 0 < get_query_var( 'order-pay' ) ) {
+                       $order_id = absint( get_query_var( 'order-pay' ) );
+                       $order    = wc_get_order( $order_id );
+
+                       // Test if order needs shipping.
+                       if ( 0 < sizeof( $order->get_items() ) ) {
+                               foreach ( $order->get_items() as $item ) {
+                                       $_product = $item->get_product();
+                                       if ( $_product && $_product->needs_shipping() ) {
+                                               $needs_shipping = true;
+                                               break;
+                                       }
+                               }
+                       }
+               }
+
+               $needs_shipping = apply_filters( 'woocommerce_cart_needs_shipping', $needs_shipping );
+
+               // Virtual order, with virtual disabled
+               if ( ! $this->enable_for_virtual && ! $needs_shipping ) {
+                       return false;
+               }
+
+               // Check methods
+               if ( ! empty( $this->enable_for_methods ) && $needs_shipping ) {
+
+                       // Only apply if all packages are being shipped via chosen methods, or order is virtual
+                       $chosen_shipping_methods_session = WC()->session->get( 'chosen_shipping_methods' );
+
+                       if ( isset( $chosen_shipping_methods_session ) ) {
+                               $chosen_shipping_methods = array_unique( $chosen_shipping_methods_session );
+                       } else {
+                               $chosen_shipping_methods = array();
+                       }
+
+                       $check_method = false;
+
+                       if ( is_object( $order ) ) {
+                               if ( $order->get_shipping_method() ) {
+                                       $check_method = $order->get_shipping_method();
+                               }
+                       } elseif ( empty( $chosen_shipping_methods ) || sizeof( $chosen_shipping_methods ) > 1 ) {
+                               $check_method = false;
+                       } elseif ( sizeof( $chosen_shipping_methods ) == 1 ) {
+                               $check_method = $chosen_shipping_methods[0];
+                       }
+
+                       if ( ! $check_method ) {
+                               return false;
+                       }
+
+                       if ( strstr( $check_method, ':' ) ) {
+                               $check_method = current( explode( ':', $check_method ) );
+                       }
+
+                       $found = false;
+
+                       foreach ( $this->enable_for_methods as $method_id ) {
+                               if ( $check_method === $method_id ) {
+                                       $found = true;
+                                       break;
+                               }
+                       }
+
+                       if ( ! $found ) {
+                               return false;
+                       }
+               }
+
+               return parent::is_available();
+       }
+
+
+       /**
+        * Process the payment and return the result.
+        *
+        * @param int $order_id
+        * @return array
+        */
+       public function process_payment( $order_id ) {
+               $order = wc_get_order( $order_id );
+
+               // Mark as processing or on-hold (payment won't be taken until delivery)
+               $order->update_status( apply_filters( 'woocommerce_cod_process_payment_order_status', $order->has_downloadable_item() ? 'on-hold' : 'processing', $order ), __( 'Payment to be made upon delivery.', 'woocommerce' ) );
+
+               // Reduce stock levels
+               wc_reduce_stock_levels( $order_id );
+
+               // Remove cart
+               WC()->cart->empty_cart();
+
+               // Return thankyou redirect
+               return array(
+                       'result'        => 'success',
+                       'redirect'      => $this->get_return_url( $order ),
+               );
+       }
+
+       /**
+        * Output for the order received page.
+        */
+       public function thankyou_page() {
+               if ( $this->instructions ) {
+                       echo wpautop( wptexturize( $this->instructions ) );
+               }
+       }
+
+       /**
+        * Change payment complete order status to completed for COD orders.
+        *
+        * @since  3.1.0
+        * @param  string $status
+        * @param  int $order_id
+        * @param  WC_Order $order
+        * @return string
+        */
+       public function change_payment_complete_order_status( $status, $order_id = 0, $order = false ) {
+               if ( $order && 'cod' === $order->get_payment_method() ) {
+                       $status = 'completed';
+               }
+               return $status;
+       }
+
+       /**
+        * Add content to the WC emails.
+        *
+        * @access public
+        * @param WC_Order $order
+        * @param bool $sent_to_admin
+        * @param bool $plain_text
+        */
+       public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
+               if ( $this->instructions && ! $sent_to_admin && $this->id === $order->get_payment_method() ) {
+                       echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
+               }
+       }
+}