code
stringlengths
15
9.96M
docstring
stringlengths
1
10.1k
func_name
stringlengths
1
124
language
stringclasses
1 value
repo
stringlengths
7
63
path
stringlengths
6
186
url
stringlengths
50
236
license
stringclasses
4 values
protected function breadcrumbs($name) { if (Yii::$app->controller->module->params['account_center_breadcrumbs']) { Yii::$service->page->breadcrumbs->addItems(['name' => $name]); } else { Yii::$service->page->breadcrumbs->active = false; } }
面包屑导航
breadcrumbs
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/block/account/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/block/account/Index.php
BSD-3-Clause
public function init() { parent::init(); }
protected $_registerSuccessRedirectUrlKey = 'customer/account';
init
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/ProductreviewController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/ProductreviewController.php
BSD-3-Clause
public function actionLoginv() { Yii::$service->session->set('logintype', 'google'); $thirdLogin = Yii::$service->store->thirdLogin; global $googleapiinfo; $googleapiinfo['GOOGLE_CLIENT_ID'] = isset($thirdLogin['google']['CLIENT_ID']) ? $thirdLogin['google']['CLIENT_ID'] : ''; $googleapiinfo['GOOGLE_CLIENT_SECRET'] = isset($thirdLogin['google']['CLIENT_SECRET']) ? $thirdLogin['google']['CLIENT_SECRET'] : ''; $lib_google_base = Yii::getAlias('@fecshop/lib/google'); include $lib_google_base.'/Social.php'; $urlKey = 'customer/google/loginv'; $redirectUrl = Yii::$service->url->getUrl($urlKey); $Social_obj = new \Social($redirectUrl); $user = $Social_obj->google(); // 服务器放到国外才行。不然上面无法返回数据。 if (is_array($user) && !empty($user)) { $fullname = $user['name']; $email = $user['email']; if ($email) { $this->accountLogin($fullname, $email); } } }
google登录确认成功后,返回的url 通过下面,得到用户的email,first_name,last_name 然后登录。 由于阿里云是国内服务器,暂时还没有具体测试,这个需要 用国外的服务器才可以。因为需要服务器方面访问google的接口。国内服务器会被墙的。
actionLoginv
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/GoogleController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/GoogleController.php
BSD-3-Clause
public function accountLogin($full_name, $email) { $name_arr = explode(' ', $full_name); $first_name = $name_arr[0]; $last_name = $name_arr[1]; $user = [ 'first_name' =>$first_name, 'last_name' =>$last_name, 'email' =>$email, ]; Yii::$service->customer->registerThirdPartyAccountAndLogin($user, 'google'); echo '<script> window.close(); window.opener.location.reload(); </script>'; exit; }
google账户登录.
accountLogin
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/GoogleController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/GoogleController.php
BSD-3-Clause
public function actionIndex() { $result_arr = []; if (Yii::$app->request->isAjax) { $result_arr['loginStatus'] = false; $result_arr['favorite'] = false; $result_arr['favorite_product_count'] = 0; $product_id = Yii::$app->request->get('product_id'); $customer_name = ''; if (!Yii::$app->user->isGuest) { $identity = Yii::$app->user->identity; $customer_name = $identity['firstname'].' '.$identity['lastname']; $result_arr['customer_name'] = $customer_name; $result_arr['favorite_product_count'] = $identity['favorite_product_count'] ? $identity['favorite_product_count'] : 0; $result_arr['loginStatus'] = true; if ($product_id) { $favorite = Yii::$service->product->favorite->getByProductIdAndUserId($product_id); $favorite ? ($result_arr['favorite'] = true) : ''; } } if ($product_id) { // 添加csrf数据 //$csrfName = \fec\helpers\CRequest::getCsrfName(); //$csrfVal = \fec\helpers\CRequest::getCsrfValue(); //$result_arr['csrfName'] = $csrfName; //$result_arr['csrfVal'] = $csrfVal; $result_arr['product_id'] = $product_id; } $cartQty = Yii::$service->cart->getCartItemQty(); $result_arr['cart_qty'] = $cartQty; } echo json_encode($result_arr); exit; }
ajax 请求 ,得到是否登录账户的信息.
actionIndex
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/AjaxController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/AjaxController.php
BSD-3-Clause
public function actionLoginv() { //Yii::$service->session->set('fbs', 1); $thirdLogin = Yii::$service->store->thirdLogin; $facebook_app_id = isset($thirdLogin['facebook']['facebook_app_id']) ? $thirdLogin['facebook']['facebook_app_id'] : ''; $facebook_app_secret = isset($thirdLogin['facebook']['facebook_app_secret']) ? $thirdLogin['facebook']['facebook_app_secret'] : ''; $fb = new \Facebook\Facebook([ 'app_id' => $facebook_app_id, 'app_secret' => $facebook_app_secret, 'default_graph_version' => 'v2.10', ]); $helper = $fb->getRedirectLoginHelper(); if (isset($_GET['state'])) { $helper->getPersistentDataHandler()->set('state', $_GET['state']); } try { $accessToken = $helper->getAccessToken(); } catch(\Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch(\Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } if (! isset($accessToken)) { if ($helper->getError()) { header('HTTP/1.0 401 Unauthorized'); echo "Error: " . $helper->getError() . "\n"; echo "Error Code: " . $helper->getErrorCode() . "\n"; echo "Error Reason: " . $helper->getErrorReason() . "\n"; echo "Error Description: " . $helper->getErrorDescription() . "\n"; } else { header('HTTP/1.0 400 Bad Request'); echo 'Bad request'; } exit; } $fb->setDefaultAccessToken($accessToken->getValue()); $response = $fb->get('/me?locale=en_US&fields=name,email'); $userNode = $response->getGraphUser(); $email = $userNode->getField('email'); $name = $userNode['name']; $fbid = $userNode['id']; if ($email) { $this->accountLogin($fbid,$name,$email); exit; } else { $loginUrl = $helper->getLoginUrl(); header('Location: '.$loginUrl); } }
facebook 账号在facebook确认后,返回网站的url地址。
actionLoginv
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/FacebookController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/FacebookController.php
BSD-3-Clause
public function accountLogin($fbid,$name,$email) { $name_arr = explode(' ', $name); $first_name = $name_arr[0]; $last_name = $name_arr[1]; $user = [ 'first_name' =>$first_name, 'last_name' =>$last_name, 'email' =>$email, ]; Yii::$service->customer->registerThirdPartyAccountAndLogin($user, 'facebook'); echo '<script> window.close(); window.opener.location.reload(); </script>'; exit; }
facebook账户登录
accountLogin
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/FacebookController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/FacebookController.php
BSD-3-Clause
public function actionIndex() { if (Yii::$app->user->isGuest) { return Yii::$service->url->redirectByUrlKey('customer/account/login'); } $data = $this->getBlock()->getLastData(); return $this->render($this->action->id, $data); }
账户中心.
actionIndex
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/AccountController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/AccountController.php
BSD-3-Clause
public function actionLogin() { if (Yii::$service->store->isAppServerMobile()) { $urlPath = 'customer/account/login'; Yii::$service->store->redirectAppServerMobile($urlPath); } if (!Yii::$app->user->isGuest) { return Yii::$service->url->redirectByUrlKey('customer/account'); } $param = Yii::$app->request->post('editForm'); if (!empty($param) && is_array($param)) { $param = \Yii::$service->helper->htmlEncode($param); $this->getBlock()->login($param); if (!Yii::$app->user->isGuest) { return Yii::$service->customer->loginSuccessRedirect('customer/account'); } } $data = $this->getBlock()->getLastData($param); return $this->render($this->action->id, $data); }
登录.
actionLogin
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/AccountController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/AccountController.php
BSD-3-Clause
public function actionLogout() { $rt = Yii::$app->request->get('rt'); if (!Yii::$app->user->isGuest) { Yii::$app->user->logout(); Yii::$service->cart->clearCart(); } if ($rt) { $redirectUrl = base64_decode($rt); Yii::$service->url->redirect($redirectUrl); } else { Yii::$service->url->redirect(Yii::$service->url->HomeUrl()); } }
登出账户.
actionLogout
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/AccountController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/AccountController.php
BSD-3-Clause
public function actionLogininfo() { if (!Yii::$app->user->isGuest) { echo json_encode([ 'loginStatus' => true, ]); exit; } }
ajax 请求 ,得到是否登录账户的信息.
actionLogininfo
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/AccountController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/AccountController.php
BSD-3-Clause
public function actionForgotpassword() { if (!Yii::$app->user->isGuest) { return Yii::$service->url->redirectByUrlKey('customer/account'); } $data = $this->getBlock()->getLastData(); return $this->render($this->action->id, $data); }
忘记密码?
actionForgotpassword
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/AccountController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/AccountController.php
BSD-3-Clause
protected function breadcrumbs($name) { if (Yii::$app->controller->module->params['forgot_reset_password_submit_breadcrumbs']) { Yii::$service->page->breadcrumbs->addItems(['name' => $name]); } else { Yii::$service->page->breadcrumbs->active = false; } }
面包屑导航
breadcrumbs
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/AccountController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/AccountController.php
BSD-3-Clause
public function actionRegisterenable() { $data = $this->getBlock()->getLastData(); return $this->render($this->action->id, $data); }
registerenable?enableToken
actionRegisterenable
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/AccountController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/AccountController.php
BSD-3-Clause
public function init() { parent::init(); }
protected $_registerSuccessRedirectUrlKey = 'customer/account';
init
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/NewsletterController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/NewsletterController.php
BSD-3-Clause
public function init() { parent::init(); }
protected $_registerSuccessRedirectUrlKey = 'customer/account';
init
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/OrderController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/OrderController.php
BSD-3-Clause
public function init() { parent::init(); }
protected $_registerSuccessRedirectUrlKey = 'customer/account';
init
php
fecshop/yii2_fecshop
app/appfront/modules/Customer/controllers/PointController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Customer/controllers/PointController.php
BSD-3-Clause
public function actionIndex() { $data = $this->getBlock()->getLastData(); if (!is_array($data) && empty($data)) { return Yii::$service->url->redirect404(); } return $this->render($this->action->id, $data); }
网站信息管理
actionIndex
php
fecshop/yii2_fecshop
app/appfront/modules/Cms/controllers/ArticleController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Cms/controllers/ArticleController.php
BSD-3-Clause
public function actionIndex() { $data = $this->getBlock()->getLastData(); return $this->render($this->action->id, $data); }
网站信息管理
actionIndex
php
fecshop/yii2_fecshop
app/appfront/modules/Cms/controllers/HomeController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Cms/controllers/HomeController.php
BSD-3-Clause
public function doCheckoutPayment($token) { $methodName_ = 'DoExpressCheckoutPayment'; $nvpStr_ = Yii::$service->payment->paypal->getCheckoutPaymentNvpStr($token); //echo $nvpStr_;exit; $doCheckoutReturn = Yii::$service->payment->paypal->PPHttpPost5($methodName_, $nvpStr_); //var_dump($doCheckoutReturn); //exit; if (strstr(strtolower($doCheckoutReturn['ACK']), 'success')) { return $doCheckoutReturn; } else { if ($doCheckoutReturn['ACK'] == 'Failure') { $message = $doCheckoutReturn['L_LONGMESSAGE0']; // 添加报错信息。 //Message::error($message); Yii::$service->helper->errors->add($message); } else { Yii::$service->helper->errors->add('paypal express payment error.'); } return false; } }
@param $token | String 通过paypal的api接口,进行支付下单
doCheckoutPayment
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/block/paypal/express/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/block/paypal/express/Placeorder.php
BSD-3-Clause
public function updateAddress($post) { return Yii::$service->cart->updateGuestCart($this->_billing, $this->_shipping_method, $this->_payment_method); }
@param $post | Array 登录用户,保存货运地址到customer address ,然后把生成的 address_id 写入到cart中。 shipping method写入到cart中 payment method 写入到cart中 updateCart
updateAddress
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/block/paypal/express/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/block/paypal/express/Placeorder.php
BSD-3-Clause
public function checkOrderInfoAndInit($post) { $address_one = ''; $billing = isset($post['billing']) ? $post['billing'] : ''; if (!Yii::$service->order->checkRequiredAddressAttr($billing)) { return false; } $this->_billing = $billing; $shipping_method = isset($post['shipping_method']) ? $post['shipping_method'] : ''; $payment_method = isset($post['payment_method']) ? $post['payment_method'] : ''; // 验证货运方式 if (!$shipping_method) { Yii::$service->helper->errors->add('shipping method can not empty'); return false; } else { //if (!Yii::$service->shipping->ifIsCorrect($shipping_method)) { // Yii::$service->helper->errors->add('shipping method is not correct'); //// // return false; //} } // 订单备注信息不能超过1500字符 $orderRemarkStrMaxLen = Yii::$service->order->orderRemarkStrMaxLen; $order_remark = isset($post['order_remark']) ? $post['order_remark'] : ''; if ($order_remark && $orderRemarkStrMaxLen) { $order_remark_strlen = strlen($order_remark); if ($order_remark_strlen > $orderRemarkStrMaxLen) { Yii::$service->helper->errors->add('order remark string length can not gt {orderRemarkStrMaxLen}', ['orderRemarkStrMaxLen' => $orderRemarkStrMaxLen]); return false; } else { // 去掉xss攻击字符,关于防止xss攻击的yii文档参看:http://www.yiichina.com/doc/guide/2.0/security-best-practices#fang-zhi-xss-gong-ji $this->_order_remark = $order_remark; } } $this->_shipping_method = $shipping_method; $this->_payment_method = $payment_method; Yii::$service->payment->setPaymentMethod($this->_payment_method); return true; }
@param $post | Array @return bool 检查前台传递的信息是否正确。同时初始化一部分类变量
checkOrderInfoAndInit
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/block/paypal/express/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/block/paypal/express/Placeorder.php
BSD-3-Clause
public function initAddress() { $this->_address_list = Yii::$service->customer->address->currentAddressList(); if (is_array($this->_address_list) && !empty($this->_address_list)) { // 用户存在地址列表,但是,cart中没有customer_address_id // 这种情况下,从用户地址列表中取出来默认地址,然后设置成当前的地址。 foreach ($this->_address_list as $adss_id => $info) { if ($info['is_default'] == 1) { $this->_address_id = $adss_id; //$this->_address_view_file = 'checkout/onepage/index/address_select.php'; $addressModel = Yii::$service->customer->address->getByPrimaryKey($this->_address_id); if ($addressModel['country']) { $this->_country = $addressModel['country']; $this->_address['country'] = $this->_country; } if ($addressModel['state']) { $this->_state = $addressModel['state']; $this->_address['state'] = $this->_state; } if ($addressModel['first_name']) { $this->_address['first_name'] = $addressModel['first_name']; } if ($addressModel['last_name']) { $this->_address['last_name'] = $addressModel['last_name']; } if ($addressModel['email']) { $this->_address['email'] = $addressModel['email']; } if ($addressModel['telephone']) { $this->_address['telephone'] = $addressModel['telephone']; } if ($addressModel['street1']) { $this->_address['street1'] = $addressModel['street1']; } if ($addressModel['street2']) { $this->_address['street2'] = $addressModel['street2']; } if ($addressModel['city']) { $this->_address['city'] = $addressModel['city']; } if ($addressModel['zip']) { $this->_address['zip'] = $addressModel['zip']; } break; } } } else { $cart = Yii::$service->cart->quote->getCart(); $address_info = []; if (!Yii::$app->user->isGuest) { $identity = Yii::$app->user->identity; $address_info['email'] = $identity['email']; $address_info['first_name'] = $identity['firstname']; $address_info['last_name'] = $identity['lastname']; } if (isset($cart['customer_email']) && !empty($cart['customer_email'])) { $address_info['email'] = $cart['customer_email']; } if (isset($cart['customer_firstname']) && !empty($cart['customer_firstname'])) { $address_info['first_name'] = $cart['customer_firstname']; } if (isset($cart['customer_lastname']) && !empty($cart['customer_lastname'])) { $address_info['last_name'] = $cart['customer_lastname']; } if (isset($cart['customer_telephone']) && !empty($cart['customer_telephone'])) { $address_info['telephone'] = $cart['customer_telephone']; } if (isset($cart['customer_address_country']) && !empty($cart['customer_address_country'])) { $address_info['country'] = $cart['customer_address_country']; $this->_country = $address_info['country']; } if (isset($cart['customer_address_state']) && !empty($cart['customer_address_state'])) { $address_info['state'] = $cart['customer_address_state']; } if (isset($cart['customer_address_city']) && !empty($cart['customer_address_city'])) { $address_info['city'] = $cart['customer_address_city']; } if (isset($cart['customer_address_zip']) && !empty($cart['customer_address_zip'])) { $address_info['zip'] = $cart['customer_address_zip']; } if (isset($cart['customer_address_street1']) && !empty($cart['customer_address_street1'])) { $address_info['street1'] = $cart['customer_address_street1']; } if (isset($cart['customer_address_street2']) && !empty($cart['customer_address_street2'])) { $address_info['street2'] = $cart['customer_address_street2']; } $this->_address = $address_info; } if (!$this->_country) { $this->_country = Yii::$service->helper->country->getDefaultCountry(); $this->_address['country'] = $this->_country; } }
初始化地址信息,首先从当前用户里面取值,然后从cart表中取数据覆盖 1. 初始化 $this->_address,里面保存的各个地址信息。 2. 如果是登录用户,而且.
initAddress
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function initCountry() { $this->_countrySelect = Yii::$service->helper->country->getAllCountryOptions('', '', $this->_country); }
初始化国家下拉条。
initCountry
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function initState($country = '') { $state = isset($this->_address['state']) ? $this->_address['state'] : ''; if (!$country) { $country = $this->_country; } $stateHtml = Yii::$service->helper->country->getStateOptionsByContryCode($country, $state); if (!$stateHtml) { $stateHtml = '<input id="state" name="billing[state]" value="'.$state.'" title="State" class="address_state input-text" style="" type="text">'; } else { $stateHtml = '<select id="address:state" class="address_state validate-select" title="State" name="billing[state]"> <option value="">Please select region, state or province</option>' .$stateHtml.'</select>'; } $this->_stateHtml = $stateHtml; }
初始化省市
initState
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function ajaxChangecountry() { $country = Yii::$app->request->get('country'); $country = \Yii::$service->helper->htmlEncode($country); $state = $this->initState($country); echo json_encode([ 'state' => $this->_stateHtml, ]); exit; }
当改变国家的时候,ajax获取省市信息.
ajaxChangecountry
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function getProductOptions($product_one, $custom_option_sku) { $custom_option_info_arr = []; $custom_option = isset($product_one['custom_option']) ? $product_one['custom_option'] : ''; $custom_option_sku = $product_one['custom_option_sku']; if (isset($custom_option[$custom_option_sku]) && !empty($custom_option[$custom_option_sku])) { $custom_option_info = $custom_option[$custom_option_sku]; foreach ($custom_option_info as $attr=>$val) { if (!in_array($attr, ['qty', 'sku', 'price', 'image'])) { $attr = str_replace('_', ' ', $attr); $attr = ucfirst($attr); $custom_option_info_arr[$attr] = $val; } } } $spu_options = $product_one['spu_options']; if (is_array($spu_options) && !empty($spu_options)) { foreach ($spu_options as $label => $val) { $custom_option_info_arr[$label] = $val; } } return $custom_option_info_arr; }
将产品页面选择的颜色尺码等显示出来,包括custom option 和spu options部分的数据.
getProductOptions
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function getShippings($custom_shipping_method = '') { $country = $this->_country; if (!$this->_state) { $region = '*'; } else { $region = $this->_state; } $cartProductInfo = Yii::$service->cart->quoteItem->getCartProductInfo(); $product_weight = $cartProductInfo['product_weight']; $product_volume_weight = $cartProductInfo['product_volume_weight']; $product_final_weight = max($product_weight, $product_volume_weight); $cartShippingMethod = $this->_cart_info['shipping_method']; // 当前的货运方式 $current_shipping_method = Yii::$service->shipping->getCurrentShippingMethod($custom_shipping_method, $cartShippingMethod, $country, $region, $product_final_weight); $this->_shipping_method = $current_shipping_method; // 得到所有,有效的shipping method $shippingArr = $this->getShippingArr($product_final_weight, $current_shipping_method, $country, $region = '*'); return $shippingArr; }
@param $current_shipping_method | String 当前选择的货运方式 @return Array,数据格式为: [ 'method'=> $method, 'label' => $label, 'name' => $name, 'cost' => $symbol.$currentCurrencyCost, 'check' => $check, 'shipping_i' => $shipping_i, ] 根据选择的货运方式,得到费用等信息。
getShippings
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function getPayment() { $paymentArr = Yii::$service->payment->getStandardPaymentArr(); $pArr = []; if (!$this->_payment_method) { if (isset($this->_cart_info['payment_method']) && !empty($this->_cart_info['payment_method'])) { $this->_payment_method = $this->_cart_info['payment_method']; } //echo $this->_payment_method; if (!$this->_payment_method) { $i = 0; foreach ($paymentArr as $k => $v) { $i++; if ($i == 1) { $this->_payment_method = $k; $v['checked'] = true; } $pArr[$k] = $v; } } else { foreach ($paymentArr as $k => $v) { if ($this->_payment_method == $k) { $v['checked'] = true; } $pArr[$k] = $v; } //var_dump($paymentArr); } } return $pArr; }
@return 得到所有的支付方式 在获取的同时,判断$this->_payment_method 是否存在,不存在则取 第一个支付方式,作为$this->_payment_method的值。
getPayment
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function setValue($getExpressCheckoutReturn) { if ($getExpressCheckoutReturn['FIRSTNAME']) { $this->_address['first_name'] = $getExpressCheckoutReturn['FIRSTNAME']; } if ($getExpressCheckoutReturn['LASTNAME']) { $this->_address['last_name'] = $getExpressCheckoutReturn['LASTNAME']; } if ($getExpressCheckoutReturn['EMAIL']) { $this->_address['email'] = $getExpressCheckoutReturn['EMAIL']; } if ($getExpressCheckoutReturn['SHIPTOCOUNTRYCODE']) { $this->_address['country'] = $getExpressCheckoutReturn['SHIPTOCOUNTRYCODE']; if ($this->_address['country'] == 'C2') { $this->_address['country'] = 'CN'; } $this->_country = $this->_address['country']; if ($this->_address['country']) { $this->_address['country_name'] = Yii::$service->helper->country->getCountryNameByKey($this->_address['country']); } } if ($getExpressCheckoutReturn['SHIPTOSTATE']) { $this->_address['state'] = $getExpressCheckoutReturn['SHIPTOSTATE']; } if ($getExpressCheckoutReturn['SHIPTOCITY']) { $this->_address['city'] = $getExpressCheckoutReturn['SHIPTOCITY']; } if ($getExpressCheckoutReturn['SHIPTOSTREET']) { $this->_address['street1'] = $getExpressCheckoutReturn['SHIPTOSTREET']; } if ($getExpressCheckoutReturn['SHIPTOSTREET2']) { $this->_address['street2'] = $getExpressCheckoutReturn['SHIPTOSTREET2']; } if ($getExpressCheckoutReturn['SHIPTOZIP']) { $this->_address['zip'] = $getExpressCheckoutReturn['SHIPTOZIP']; } }
初始化信息。
setValue
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function doCheckoutPayment($token) { $methodName_ = 'DoExpressCheckoutPayment'; $nvpStr_ = Yii::$service->payment->paypal->getCheckoutPaymentNvpStr($token); //echo '<br/>nvpStr_:<br/>"'.$nvpStr_.'<br/><br/>'; $doCheckoutReturn = Yii::$service->payment->paypal->PPHttpPost5($methodName_, $nvpStr_); //echo '<br/>doCheckoutReturn <br/><br/>'; //var_dump($doCheckoutReturn); //echo '<br/>doCheckoutReturn <br/><br/>'; //exit; if (strstr(strtolower($doCheckoutReturn['ACK']), 'success')) { return $doCheckoutReturn; } else { if ($doCheckoutReturn['ACK'] == 'Failure') { $message = $doCheckoutReturn['L_LONGMESSAGE0']; // 添加报错信息。 //Message::error($message); Yii::$service->helper->errors->add($message); } else { Yii::$service->helper->errors->add('paypal express payment error.'); } return false; } }
@param $token | String 通过paypal的api接口,进行支付下单
doCheckoutPayment
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/block/paypal/standard/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/block/paypal/standard/Placeorder.php
BSD-3-Clause
public function updateAddress($post) { return Yii::$service->cart->updateGuestCart($this->_billing, $this->_shipping_method, $this->_payment_method); }
@param $post | Array 登录用户,保存货运地址到customer address ,然后把生成的 address_id 写入到cart中。 shipping method写入到cart中 payment method 写入到cart中 updateCart
updateAddress
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/block/paypal/standard/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/block/paypal/standard/Placeorder.php
BSD-3-Clause
public function checkOrderInfoAndInit($post) { $address_one = ''; $billing = isset($post['billing']) ? $post['billing'] : ''; if (!Yii::$service->order->checkRequiredAddressAttr($billing)) { return false; } $this->_billing = $billing; $shipping_method = isset($post['shipping_method']) ? $post['shipping_method'] : ''; $payment_method = isset($post['payment_method']) ? $post['payment_method'] : ''; // 验证货运方式 if (!$shipping_method) { Yii::$service->helper->errors->add('shipping method can not empty'); return false; } else { if (!Yii::$service->shipping->ifIsCorrect($shipping_method)) { Yii::$service->helper->errors->add('shipping method is not correct'); return false; } } $this->_shipping_method = $shipping_method; $this->_payment_method = $payment_method; Yii::$service->payment->setPaymentMethod($this->_payment_method); return true; }
@param $post | Array @return bool 检查前台传递的信息是否正确。同时初始化一部分类变量
checkOrderInfoAndInit
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/block/paypal/standard/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/block/paypal/standard/Placeorder.php
BSD-3-Clause
public function actionStart() { $payment_method = isset($this->_order_model['payment_method']) ? $this->_order_model['payment_method'] : ''; if ($payment_method) { $complateUrl = Yii::$service->payment->getStandardSuccessRedirectUrl($payment_method); if ($complateUrl) { // 登录用户,在支付前清空购物车。 //if(!Yii::$app->user->isGuest){ // Yii::$service->cart->clearCartProductAndCoupon(); //} // 清空购物车 Yii::$service->cart->clearCartProductAndCoupon(); Yii::$service->url->redirect($complateUrl); exit; } } $homeUrl = Yii::$service->url->homeUrl(); Yii::$service->url->redirect($homeUrl); }
支付开始页面.
actionStart
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/controllers/CheckmoneyController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/controllers/CheckmoneyController.php
BSD-3-Clause
public function actionSuccess() { $data = [ 'increment_id' => $this->_increment_id, ]; // 清理购物车中的产品。(游客用户的购物车在成功页面清空) if (Yii::$app->user->isGuest) { Yii::$service->cart->clearCartProductAndCoupon(); } // 清理session中的当前的increment_id Yii::$service->order->removeSessionIncrementId(); return $this->render('../../payment/checkmoney/success', $data); }
废弃 成功支付页面.
actionSuccess
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/controllers/CheckmoneyController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/controllers/CheckmoneyController.php
BSD-3-Clause
public function actionIpn() { }
IPN消息推送地址 IPN过来后,不清除session中的 increment_id ,也不清除购物车 仅仅是更改订单支付状态。
actionIpn
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/controllers/CheckmoneyController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/controllers/CheckmoneyController.php
BSD-3-Clause
public function actionStart() { Yii::$service->page->theme->layoutFile = 'wxpay_start.php'; /** * 下面的 $startData 返回的数据格式为: *[ * 'scanCodeImgUrl' => $scanCodeImgUrl, * 'increment_id' => $trade_info['increment_id'], * 'total_amount' => $trade_info['total_amount'], * 'subject' => $trade_info['subject'], * 'coupon_code' => $trade_info['coupon_code'], * 'product_ids' => $trade_info['product_ids'], *]; */ $startData = Yii::$service->payment->wxpay->getScanCodeStart(); //echo $startData;exit; if(!$startData){ //Yii::$service->url->redirectByUrlKey('/checkout/onepage'); return; } if(!Yii::$app->user->isGuest){ $startData['customer_email'] = Yii::$app->user->identity->email; } $startData['trace_success_url'] = Yii::$service->url->getUrl('/payment/wxpay/standard/tradesuccess',['out_trade_no' => $startData['increment_id']]); $startData['expireTime'] = Yii::$service->payment->wxpay->expireTime; return $this->render($this->action->id, $startData); }
在下单页面中选择微信支付方式后, 为微信支付做的准备工作。
actionStart
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/controllers/wxpay/StandardController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/controllers/wxpay/StandardController.php
BSD-3-Clause
public function actionIpn() { Yii::$service->payment->wxpay->ipn(); }
废弃 IPN,微信消息接收部分 pc扫码通过js轮询查询支付状态,不需要ipn接受异步支付消息
actionIpn
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/controllers/wxpay/StandardController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/controllers/wxpay/StandardController.php
BSD-3-Clause
public function actionQrcode() { error_reporting(E_ERROR); $phpqrcodeFile = Yii::getAlias('@fecshop/lib/wxpay/example/phpqrcode/phpqrcode.php'); require_once($phpqrcodeFile); $url = urldecode($_GET["data"]); \QRcode::png($url); }
生成扫码支付的二维码
actionQrcode
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/controllers/wxpay/StandardController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/controllers/wxpay/StandardController.php
BSD-3-Clause
public function actionTradesuccess() { $out_trade_no = Yii::$app->request->get('out_trade_no'); if (!$out_trade_no) { return json_encode([ 'code'=> 300, 'data'=> 'out_trade_no is empty', ]); } $res = Yii::$service->payment->wxpay->scanCodeCheckTradeIsSuccess($out_trade_no); if ($res === true) { return json_encode([ 'code'=> 200, 'data'=> 'success', ]); } else { return json_encode([ 'code'=> 301, 'data'=> 'fail', ]); } }
判断微信支付是否成功,只要一个参数不匹配即判断为不成功 @param unknown $out_trade_no
actionTradesuccess
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/controllers/wxpay/StandardController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/controllers/wxpay/StandardController.php
BSD-3-Clause
public function actionReview() { $payment_method = Yii::$service->payment->paypal->express_payment_method; Yii::$service->payment->setPaymentMethod($payment_method); $_csrf = Yii::$app->request->post('_csrf'); if ($_csrf) { $status = $this->getBlock('placeorder')->getLastData(); if ($status) { return; } } $data = $this->getBlock()->getLastData(); if (is_array($data) && !empty($data)) { return $this->render($this->action->id, $data); } else { return $data; } }
2.Review 从paypal确认后返回
actionReview
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/controllers/paypal/ExpressController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/controllers/paypal/ExpressController.php
BSD-3-Clause
public function actionReview() { $payment_method = Yii::$service->payment->paypal->standard_payment_method; Yii::$service->payment->setPaymentMethod($payment_method); $this->getBlock('placeorder')->getLastData(); }
2.Review 从paypal确认后返回
actionReview
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/controllers/paypal/StandardController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/controllers/paypal/StandardController.php
BSD-3-Clause
public function actionStart() { //$AopSdkFile = Yii::getAlias('@fecshop/lib/alipay/AopSdk.php'); //require($AopSdkFile); // echo '支付宝支付跳转中...'; return '支付宝支付跳转中...'.Yii::$service->payment->alipay->start(); }
在网站下单页面,选择支付宝支付方式后, 跳转到支付宝支付页面前准备的部分。
actionStart
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/controllers/alipay/StandardController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/controllers/alipay/StandardController.php
BSD-3-Clause
public function actionReview() { $reviewStatus = Yii::$service->payment->alipay->review(); if($reviewStatus){ $successRedirectUrl = Yii::$service->payment->getStandardSuccessRedirectUrl(); return Yii::$service->url->redirect($successRedirectUrl); }else{ echo Yii::$service->helper->errors->get('<br/>'); return; } }
从支付宝支付成功后,跳转返回 fec-shop 的部分
actionReview
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/controllers/alipay/StandardController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/controllers/alipay/StandardController.php
BSD-3-Clause
public function actionIpn() { \Yii::info('alipay ipn begin', 'fecshop_debug'); $post = Yii::$app->request->post(); if (is_array($post) && !empty($post)) { \Yii::info('', 'fecshop_debug'); $post = \Yii::$service->helper->htmlEncode($post); ob_start(); ob_implicit_flush(false); var_dump($post); $post_log = ob_get_clean(); \Yii::info($post_log, 'fecshop_debug'); $ipnStatus = Yii::$service->payment->alipay->receiveIpn($post); if($ipnStatus){ echo 'success'; return; } } }
IPN,支付宝消息接收部分
actionIpn
php
fecshop/yii2_fecshop
app/appfront/modules/Payment/controllers/alipay/StandardController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Payment/controllers/alipay/StandardController.php
BSD-3-Clause
protected function breadcrumbs($name) { if (Yii::$app->controller->module->params['checkout_cart_breadcrumbs']) { Yii::$service->page->breadcrumbs->addItems(['name' => $name]); } else { Yii::$service->page->breadcrumbs->active = false; } }
面包屑导航
breadcrumbs
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/block/cart/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/block/cart/Index.php
BSD-3-Clause
public function getProductOptions($product_one) { $custom_option_info_arr = []; $custom_option = isset($product_one['custom_option']) ? $product_one['custom_option'] : ''; $custom_option_sku = $product_one['custom_option_sku']; if (isset($custom_option[$custom_option_sku]) && !empty($custom_option[$custom_option_sku])) { $custom_option_info = $custom_option[$custom_option_sku]; foreach ($custom_option_info as $attr=>$val) { if (!in_array($attr, ['qty', 'sku', 'price', 'image'])) { $attr = str_replace('_', ' ', $attr); $attr = ucfirst($attr); $custom_option_info_arr[$attr] = $val; } } } $spu_options = $product_one['spu_options']; if (is_array($spu_options) && !empty($spu_options)) { foreach ($spu_options as $label => $val) { $custom_option_info_arr[$label] = $val; } } return $custom_option_info_arr; }
将产品页面选择的颜色尺码等显示出来,包括custom option 和spu options部分的数据.
getProductOptions
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/block/cart/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/block/cart/Index.php
BSD-3-Clause
public function guestCreateAndLoginAccount($post) { $create_account = $post['create_account']; $billing = $post['billing']; if (!is_array($billing) || empty($billing)) { Yii::$service->helper->errors->add('billing must be array and can not empty'); return false; } if ($create_account) { $customer_password = $billing['customer_password']; $confirm_password = $billing['confirm_password']; if ($customer_password != $confirm_password) { Yii::$service->helper->errors->add('the passwords are inconsistent'); return false; } $passMin = Yii::$service->customer->getRegisterPassMinLength(); $passMax = Yii::$service->customer->getRegisterPassMaxLength(); if (strlen($customer_password) < $passMin) { Yii::$service->helper->errors->add('password must Greater than {min_password}', ['min_password' => $passMin]); return false; } if (strlen($customer_password) > $passMax) { Yii::$service->helper->errors->add('password must less than {max_password}', ['max_password' => $passMax]); return false; } $param['email'] = $billing['email']; $param['password'] = $billing['customer_password']; $param['firstname'] = $billing['first_name']; $param['lastname'] = $billing['last_name']; if (!Yii::$service->customer->register($param)) { return false; } else { Yii::$service->customer->Login([ 'email' => $billing['email'], 'password' => $billing['customer_password'], ]); } } return true; }
@param $post|Array,前台传递参数数组。 如果游客选择了创建账户,并且输入了密码,则使用address email作为账号, 进行账号的注册和登录。
guestCreateAndLoginAccount
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/block/onepage/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/block/onepage/Placeorder.php
BSD-3-Clause
public function updateAddress($post) { if (!Yii::$app->user->isGuest) { $billing = $post['billing']; $address_id = $post['address_id']; if (!$address_id) { $identity = Yii::$app->user->identity; $customer_id = $identity['id']; $one = [ 'first_name' => $billing['first_name'], 'last_name' => $billing['last_name'], 'email' => $billing['email'], 'company' => '', 'telephone' => $billing['telephone'], 'fax' => '', 'street1' => $billing['street1'], 'street2' => $billing['street2'], 'city' => $billing['city'], 'state' => $billing['state'], 'zip' => $billing['zip'], 'country' => $billing['country'], 'customer_id' => $customer_id, 'is_default' => 1, ]; $address_id = Yii::$service->customer->address->save($one); $this->_address_id = $address_id; if (!$address_id) { Yii::$service->helper->errors->add('new customer address save fail'); return false; } //echo "$address_id,$this->_shipping_method,$this->_payment_method"; } return Yii::$service->cart->updateLoginCart($this->_address_id, $this->_shipping_method, $this->_payment_method); } else { return Yii::$service->cart->updateGuestCart($this->_billing, $this->_shipping_method, $this->_payment_method); } return true; }
@param $post | Array 登录用户,保存货运地址到customer address ,然后把生成的 address_id 写入到cart中。 shipping method写入到cart中 payment method 写入到cart中 updateCart
updateAddress
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/block/onepage/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/block/onepage/Placeorder.php
BSD-3-Clause
public function checkOrderInfoAndInit($post) { $address_one = ''; $address_id = isset($post['address_id']) ? $post['address_id'] : ''; $billing = isset($post['billing']) ? $post['billing'] : ''; if ($address_id) { $this->_address_id = $address_id; if (Yii::$app->user->isGuest) { Yii::$service->helper->errors->add('address id can not use for guest'); return false; // address_id 这种情况,必须是登录用户。 } else { $customer_id = Yii::$app->user->identity->id; if (!$customer_id) { Yii::$service->helper->errors->add('customer id is empty'); return false; } else { $address_one = Yii::$service->customer->address->getAddressByIdAndCustomerId($address_id, $customer_id); if (!$address_one) { Yii::$service->helper->errors->add('current address id is not belong to current user'); return false; } else { // 从address_id中取出来的字段,查看是否满足必写的要求。 if (!Yii::$service->order->checkRequiredAddressAttr($address_one)) { return false; } $arr['customer_id'] = $customer_id; foreach ($address_one as $k=>$v) { $arr[$k] = $v; } $this->_billing = $arr; } } } } elseif ($billing && is_array($billing)) { // 检查address的必写字段是否都存在 //var_dump($billing);exit; if (!Yii::$service->order->checkRequiredAddressAttr($billing)) { return false; } $this->_billing = $billing; } $shipping_method = isset($post['shipping_method']) ? $post['shipping_method'] : ''; $payment_method = isset($post['payment_method']) ? $post['payment_method'] : ''; // 验证货运方式 if (!$shipping_method) { Yii::$service->helper->errors->add('shipping method can not empty'); return false; } else { //if (!Yii::$service->shipping->ifIsCorrect($shipping_method)) { // Yii::$service->helper->errors->add('shipping method is not correct'); // // return false; //} } // 验证支付方式 if (!$payment_method) { Yii::$service->helper->errors->add('payment method can not empty'); return false; } else { if (!Yii::$service->payment->ifIsCorrectStandard($payment_method)) { Yii::$service->helper->errors->add('payment method is not correct'); return false; } } // 增加event $beforeEventName = 'event_place_order_check_order'; Yii::$service->event->trigger($beforeEventName, $post); $eventCheckErr = Yii::$service->event->getErrStr(); // 事件检查结果,是否存在报错,得到报错信息 if ($eventCheckErr) { Yii::$service->helper->errors->add($eventCheckErr); return false; } // 订单备注信息不能超过1500字符 $orderRemarkStrMaxLen = Yii::$service->order->orderRemarkStrMaxLen; $order_remark = isset($post['order_remark']) ? $post['order_remark'] : ''; if ($order_remark && $orderRemarkStrMaxLen) { $order_remark_strlen = strlen($order_remark); if ($order_remark_strlen > $orderRemarkStrMaxLen) { Yii::$service->helper->errors->add('order remark string length can not gt {orderRemarkStrMaxLen}', ['orderRemarkStrMaxLen' => $orderRemarkStrMaxLen]); return false; } else { // 去掉xss攻击字符,关于防止xss攻击的yii文档参看:http://www.yiichina.com/doc/guide/2.0/security-best-practices#fang-zhi-xss-gong-ji $this->_order_remark = $order_remark; } } $this->_shipping_method = $shipping_method; $this->_payment_method = $payment_method; Yii::$service->payment->setPaymentMethod($this->_payment_method); return true; }
@param $post | Array @return bool 检查前台传递的信息是否正确。同时初始化一部分类变量
checkOrderInfoAndInit
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/block/onepage/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/block/onepage/Placeorder.php
BSD-3-Clause
protected function breadcrumbs($name) { if (Yii::$app->controller->module->params['checkout_onepage_breadcrumbs']) { Yii::$service->page->breadcrumbs->addItems(['name' => $name]); } else { Yii::$service->page->breadcrumbs->active = false; } }
面包屑导航
breadcrumbs
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/block/onepage/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/block/onepage/Index.php
BSD-3-Clause
public function initAddress() { //$this->_cart_address = Yii::$service->cart->quote->getCartAddress(); $cart = Yii::$service->cart->quote->getCart(); $address_id = $cart['customer_address_id']; $address_info = []; if (!Yii::$app->user->isGuest) { $identity = Yii::$app->user->identity; $address_info['email'] = $identity['email']; $address_info['first_name'] = $identity['firstname']; $address_info['last_name'] = $identity['lastname']; } if (isset($cart['customer_email']) && !empty($cart['customer_email'])) { $address_info['email'] = $cart['customer_email']; } if (isset($cart['customer_firstname']) && !empty($cart['customer_firstname'])) { $address_info['first_name'] = $cart['customer_firstname']; } if (isset($cart['customer_lastname']) && !empty($cart['customer_lastname'])) { $address_info['last_name'] = $cart['customer_lastname']; } if (isset($cart['customer_telephone']) && !empty($cart['customer_telephone'])) { $address_info['telephone'] = $cart['customer_telephone']; } if (isset($cart['customer_address_country']) && !empty($cart['customer_address_country'])) { $address_info['country'] = $cart['customer_address_country']; $this->_country = $address_info['country']; } if (isset($cart['customer_address_state']) && !empty($cart['customer_address_state'])) { $address_info['state'] = $cart['customer_address_state']; } if (isset($cart['customer_address_city']) && !empty($cart['customer_address_city'])) { $address_info['city'] = $cart['customer_address_city']; } if (isset($cart['customer_address_zip']) && !empty($cart['customer_address_zip'])) { $address_info['zip'] = $cart['customer_address_zip']; } if (isset($cart['customer_address_street1']) && !empty($cart['customer_address_street1'])) { $address_info['street1'] = $cart['customer_address_street1']; } if (isset($cart['customer_address_street2']) && !empty($cart['customer_address_street2'])) { $address_info['street2'] = $cart['customer_address_street2']; } $this->_address = $address_info; $this->_address_list = Yii::$service->customer->address->currentAddressList(); //var_dump($this->_address_list); // 如果购物车存在customer_address_id,而且用户地址中也存在customer_address_id // 则执行if{}内代码。 if ($address_id && isset($this->_address_list[$address_id]) && !empty($this->_address_list[$address_id])) { $this->_address_id = $address_id; $this->_address_view_file = 'checkout/onepage/index/address_select.php'; $addressModel = Yii::$service->customer->address->getByPrimaryKey($this->_address_id); if ($addressModel['country']) { $this->_country = $addressModel['country']; $this->_address['country'] = $this->_country; } if ($addressModel['state']) { $this->_state = $addressModel['state']; $this->_address['state'] = $this->_state; } if ($addressModel['first_name']) { $this->_address['first_name'] = $addressModel['first_name']; } if ($addressModel['last_name']) { $this->_address['last_name'] = $addressModel['last_name']; } if ($addressModel['email']) { $this->_address['email'] = $addressModel['email']; } if ($addressModel['telephone']) { $this->_address['telephone'] = $addressModel['telephone']; } if ($addressModel['street1']) { $this->_address['street1'] = $addressModel['street1']; } if ($addressModel['street2']) { $this->_address['street2'] = $addressModel['street2']; } if ($addressModel['city']) { $this->_address['city'] = $addressModel['city']; } if ($addressModel['zip']) { $this->_address['zip'] = $addressModel['zip']; } } elseif (is_array($this->_address_list) && !empty($this->_address_list)) { // 用户存在地址列表,但是,cart中没有customer_address_id // 这种情况下,从用户地址列表中取出来默认地址,然后设置成当前的地址。 foreach ($this->_address_list as $adss_id => $info) { if ($info['is_default'] == 1) { $this->_address_id = $adss_id; $this->_address_view_file = 'checkout/onepage/index/address_select.php'; $addressModel = Yii::$service->customer->address->getByPrimaryKey($this->_address_id); if ($addressModel['country']) { $this->_country = $addressModel['country']; $this->_address['country'] = $this->_country; } if ($addressModel['state']) { $this->_state = $addressModel['state']; $this->_address['state'] = $this->_state; } if ($addressModel['first_name']) { $this->_address['first_name'] = $addressModel['first_name']; } if ($addressModel['last_name']) { $this->_address['last_name'] = $addressModel['last_name']; } if ($addressModel['email']) { $this->_address['email'] = $addressModel['email']; } if ($addressModel['telephone']) { $this->_address['telephone'] = $addressModel['telephone']; } if ($addressModel['street1']) { $this->_address['street1'] = $addressModel['street1']; } if ($addressModel['street2']) { $this->_address['street2'] = $addressModel['street2']; } if ($addressModel['city']) { $this->_address['city'] = $addressModel['city']; } if ($addressModel['zip']) { $this->_address['zip'] = $addressModel['zip']; } break; } } } else { $this->_address_view_file = 'checkout/onepage/index/address.php'; // 从购物车里面取出来数据。 $_cartAddress //$cart_info = Yii::$service->cart->getCartInfo(); } if (!$this->_country) { $this->_country = Yii::$service->helper->country->getDefaultCountry(); $this->_address['country'] = $this->_country; } }
初始化地址信息,首先从当前用户里面取值,然后从cart表中取数据覆盖 1. 初始化 $this->_address,里面保存的各个地址信息。 2. 如果是登录用户,而且.
initAddress
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/block/onepage/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/block/onepage/Index.php
BSD-3-Clause
public function initCountry() { $this->_countrySelect = Yii::$service->helper->country->getAllCountryOptions('', '', $this->_country); }
初始化国家下拉条。
initCountry
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/block/onepage/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/block/onepage/Index.php
BSD-3-Clause
public function initState($country = '') { $state = isset($this->_address['state']) ? $this->_address['state'] : ''; if (!$country) { $country = $this->_country; } $stateHtml = Yii::$service->helper->country->getStateOptionsByContryCode($country, $state); if (!$stateHtml) { $stateHtml = '<input id="state" placeholder="'. Yii::$service->page->translate->__('Your State').'" name="billing[state]" value="'.$state.'" title="State" class="address_state input-text" style="" type="text">'; } else { $stateHtml = '<select id="address:state" class="address_state validate-select" title="State" name="billing[state]"> <option value="">Please select region, state or province</option>' .$stateHtml.'</select>'; } $this->_stateHtml = $stateHtml; }
初始化省市
initState
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/block/onepage/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/block/onepage/Index.php
BSD-3-Clause
public function ajaxChangecountry() { $country = Yii::$app->request->get('country'); $country = \Yii::$service->helper->htmlEncode($country); $state = $this->initState($country); echo json_encode([ 'state' => $this->_stateHtml, ]); exit; }
当改变国家的时候,ajax获取省市信息.
ajaxChangecountry
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/block/onepage/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/block/onepage/Index.php
BSD-3-Clause
public function getProductOptions($product_one, $custom_option_sku) { $custom_option_info_arr = []; $custom_option = isset($product_one['custom_option']) ? $product_one['custom_option'] : ''; $custom_option_sku = $product_one['custom_option_sku']; if (isset($custom_option[$custom_option_sku]) && !empty($custom_option[$custom_option_sku])) { $custom_option_info = $custom_option[$custom_option_sku]; foreach ($custom_option_info as $attr=>$val) { if (!in_array($attr, ['qty', 'sku', 'price', 'image'])) { $attr = str_replace('_', ' ', $attr); $attr = ucfirst($attr); $custom_option_info_arr[$attr] = $val; } } } $spu_options = $product_one['spu_options']; if (is_array($spu_options) && !empty($spu_options)) { foreach ($spu_options as $label => $val) { $custom_option_info_arr[$label] = $val; } } return $custom_option_info_arr; }
将产品页面选择的颜色尺码等显示出来,包括custom option 和spu options部分的数据.
getProductOptions
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/block/onepage/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/block/onepage/Index.php
BSD-3-Clause
public function getShippings($custom_shipping_method = '') { $country = $this->_country; if (!$this->_state) { $region = '*'; } else { $region = $this->_state; } $cartProductInfo = Yii::$service->cart->quoteItem->getCartProductInfo(); $product_weight = $cartProductInfo['product_weight']; $product_volume_weight = $cartProductInfo['product_volume_weight']; $product_final_weight = max($product_weight, $product_volume_weight); $cartShippingMethod = $this->_cart_info['shipping_method']; // 当前的货运方式 $current_shipping_method = Yii::$service->shipping->getCurrentShippingMethod($custom_shipping_method, $cartShippingMethod, $country, $region, $product_final_weight); $this->_shipping_method = $current_shipping_method; // 得到所有,有效的shipping method $shippingArr = $this->getShippingArr($product_final_weight, $current_shipping_method, $country, $region = '*'); return $shippingArr; }
@param $current_shipping_method | String 当前选择的货运方式 @return Array,数据格式为: [ 'method'=> $method, 'label' => $label, 'name' => $name, 'cost' => $symbol.$currentCurrencyCost, 'check' => $check, 'shipping_i' => $shipping_i, ] 得到所有的,有效shipping method数组。
getShippings
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/block/onepage/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/block/onepage/Index.php
BSD-3-Clause
public function getPayment() { $paymentArr = Yii::$service->payment->getStandardPaymentArr(); $pArr = []; if (!$this->_payment_method) { if (isset($this->_cart_info['payment_method']) && !empty($this->_cart_info['payment_method'])) { $this->_payment_method = $this->_cart_info['payment_method']; } //echo $this->_payment_method; if (!$this->_payment_method) { $i = 0; foreach ($paymentArr as $k => $v) { $i++; if ($i == 1) { $this->_payment_method = $k; $v['checked'] = true; } $pArr[$k] = $v; } } else { $checked_payment = 0; foreach ($paymentArr as $k => $v) { if ($this->_payment_method == $k) { $v['checked'] = true; $checked_payment = 1; } $pArr[$k] = $v; } if (!$checked_payment) { foreach ($paymentArr as $k => $v) { $this->_payment_method = $k; $pArr[$k]['checked'] = true; break; } } //var_dump($paymentArr); } } return $pArr; }
@return 得到所有的支付方式 在获取的同时,判断$this->_payment_method 是否存在,不存在则取 第一个支付方式,作为$this->_payment_method的值。
getPayment
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/block/onepage/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/block/onepage/Index.php
BSD-3-Clause
public function ajaxUpdateOrderAndShipping() { $country = Yii::$app->request->get('country'); $shipping_method = Yii::$app->request->get('shipping_method'); $address_id = Yii::$app->request->get('address_id'); $state = Yii::$app->request->get('state'); $country = \Yii::$service->helper->htmlEncode($country); $shipping_method = \Yii::$service->helper->htmlEncode($shipping_method); $address_id = \Yii::$service->helper->htmlEncode($address_id); $state = \Yii::$service->helper->htmlEncode($state); if ($address_id) { $this->_address_id = $address_id; $addressModel = Yii::$service->customer->address->getByPrimaryKey($this->_address_id); if ($addressModel['country']) { $country = $addressModel['country']; $this->_country = $addressModel['country']; } if ($addressModel['state']) { $state = $addressModel['state']; $this->_state = $addressModel['state']; } } elseif ($country) { $this->_country = $country; if (!$state) { $state = '*'; } $this->_state = $state; } if ($this->_country && $this->_state) { $shippings = $this->getShippings($shipping_method); $payments = $this->getPayment(); /** * 下面是Fecshop的widget,通过一个一个数据数组+第一个view文件 * 组合得到对应的html代码,返回给$shippingHtml * 由于fecshop多多模板系统,预先从高级别的模板路径中依次查找view文件,存在则使用该view文件. */ $shippingView = [ 'view' => 'checkout/onepage/index/shipping.php', ]; $shippingParam = [ 'shippings' => $shippings, ]; $shippingHtml = Yii::$service->page->widget->render($shippingView, $shippingParam); /** * 先通过item计算出来重量,得到运费 然后setShippingCost($shippingCost),将当前根据传递参数计算 * 出来的运费结果set到quote.shippingCost中, * 原因:这里是用户勾选切换国家地址进行的运费计算反馈给用户,但是该信息不更新到数据库,仅仅显示出来 * 相应的费用给用户看,因此,shippingCost通过传递的参数计算出来设置到quote中,而不是通过数据库中保存 * 的信息计算。 */ $quoteItem = Yii::$service->cart->quoteItem->getCartProductInfo(); $product_weight = $quoteItem['product_weight']; // 计算运费。 $avaiable_method = Yii::$service->shipping->getAvailableShippingMethods($country,$region,$product_weight); $shippingInfo = $avaiable_method[$shipping_method]; $shippingCost = Yii::$service->shipping->getShippingCost($shipping_method, $shippingInfo, $product_weight, $country, $state); Yii::$service->cart->quote->setShippingCost($shippingCost); /** * 下面通过当前的货币,购物车信息等数组数据,+上view文件 * 返回order部分的html内容。 */ // 得到当前货币 $currency_info = Yii::$service->page->currency->getCurrencyInfo(); $reviewOrderView = [ 'view' => 'checkout/onepage/index/review_order.php', ]; $cart_info = $this->getCartInfo(true, $shipping_method, $this->_country, $this->_state); $reviewOrderParam = [ 'cart_info' => $cart_info, 'currency_info' => $currency_info, ]; $reviewOrderHtml = Yii::$service->page->widget->render($reviewOrderView, $reviewOrderParam); echo json_encode([ 'status' => 'success', 'shippingHtml' => $shippingHtml, 'reviewOrderHtml' => $reviewOrderHtml, ]); exit; } }
js函数 ajaxreflush() 执行后,就会执行这个函数 在 1.切换address list, 2.取消coupon, 3.切换国家和省市信息, 4.更改货运方式等 集中情况下,就会触发执行当前函数, 该函数会根据传递的参数,重新计算shipping 和order 部分信息,返回 给前端。 @proeprty Array, @return json_encode(Array),Array格式如下: [ 'status' => 'success', 'shippingHtml' => $shippingHtml, 'reviewOrderHtml' => $reviewOrderHtml, ] 返回给js后,js根据数据将信息更新到相应的部分。
ajaxUpdateOrderAndShipping
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/block/onepage/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/block/onepage/Index.php
BSD-3-Clause
public function actionAdd() { $custom_option = Yii::$app->request->post('custom_option'); $product_id = Yii::$app->request->post('product_id'); $qty = Yii::$app->request->post('qty'); //$custom_option = \Yii::$service->helper->htmlEncode($custom_option); $product_id = \Yii::$service->helper->htmlEncode($product_id); $qty = \Yii::$service->helper->htmlEncode($qty); $qty = abs(ceil((int) $qty)); if ($qty && $product_id) { if ($custom_option) { $custom_option_sku = json_decode($custom_option, true); } if (empty($custom_option_sku)) { $custom_option_sku = null; } $item = [ 'product_id' => $product_id, 'qty' => $qty, 'custom_option_sku' => $custom_option_sku, ]; $innerTransaction = Yii::$app->db->beginTransaction(); try { $addToCart = Yii::$service->cart->addProductToCart($item); if ($addToCart) { echo json_encode([ 'status' => 'success', 'items_count' => Yii::$service->cart->quote->getCartItemCount(), ]); $innerTransaction->commit(); exit; } else { $errors = Yii::$service->helper->errors->get(','); echo json_encode([ 'status' => 'fail', 'content'=> Yii::$service->page->translate->__($errors), //'items_count' => Yii::$service->cart->quote->getCartItemCount(), ]); $innerTransaction->rollBack(); exit; } } catch (\Exception $e) { $innerTransaction->rollBack(); } } }
把产品加入到购物车.
actionAdd
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/controllers/CartController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/controllers/CartController.php
BSD-3-Clause
public function actionAddcoupon() { if (Yii::$app->user->isGuest) { // 记忆一下登录成功返回购物车页面 $cartUrl = Yii::$service->url->getUrl('checkout/cart'); Yii::$service->customer->setLoginSuccessRedirectUrl($cartUrl); echo json_encode([ 'status' => 'fail', 'content'=> 'nologin', ]); exit; } $coupon_code = trim(Yii::$app->request->post('coupon_code')); $coupon_code = \Yii::$service->helper->htmlEncode($coupon_code); if ($coupon_code) { $innerTransaction = Yii::$app->db->beginTransaction(); try { if (Yii::$service->cart->coupon->addCoupon($coupon_code)) { $innerTransaction->commit(); } else { $innerTransaction->rollBack(); } } catch (\Exception $e) { $innerTransaction->rollBack(); } $error_arr = Yii::$service->helper->errors->get(true); if (!empty($error_arr)) { if (is_array($error_arr)) { $error_str = implode(',', $error_arr); } else { $error_str = $error_arr; } echo json_encode([ 'status' => 'fail', 'content'=> Yii::$service->page->translate->__($error_str), ]); exit; } else { echo json_encode([ 'status' => 'success', 'content'=> Yii::$service->page->translate->__('add coupon success'), ]); exit; } } else { echo json_encode([ 'status' => 'fail', 'content'=> Yii::$service->page->translate->__('coupon is empty'), ]); exit; } }
购物车中添加优惠券.
actionAddcoupon
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/controllers/CartController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/controllers/CartController.php
BSD-3-Clause
public function actionCancelcoupon() { if (Yii::$app->user->isGuest) { // 记忆一下登录成功返回购物车页面 $cartUrl = Yii::$service->url->getUrl('checkout/cart'); Yii::$service->customer->setLoginSuccessRedirectUrl($cartUrl); echo json_encode([ 'status' => 'fail', 'content'=> 'nologin', ]); exit; } $coupon_code = trim(Yii::$app->request->post('coupon_code')); if ($coupon_code) { $innerTransaction = Yii::$app->db->beginTransaction(); try { $cancelStatus = Yii::$service->cart->coupon->cancelCoupon($coupon_code); if (!$cancelStatus) { echo json_encode([ 'status' => 'fail', 'content'=> Yii::$service->page->translate->__('coupon is not exist'), ]); $innerTransaction->rollBack(); exit; } $error_arr = Yii::$service->helper->errors->get(true); if (!empty($error_arr)) { $error_str = implode(',', $error_arr); echo json_encode([ 'status' => 'fail', 'content'=> $error_str, ]); $innerTransaction->rollBack(); exit; } else { echo json_encode([ 'status' => 'success', 'content'=> Yii::$service->page->translate->__('cacle coupon success'), ]); $innerTransaction->commit(); exit; } } catch (\Exception $e) { $innerTransaction->rollBack(); } } else { echo json_encode([ 'status' => 'fail', 'content'=> Yii::$service->page->translate->__('coupon is empty'), ]); exit; } }
购物车中取消优惠券.
actionCancelcoupon
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/controllers/CartController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/controllers/CartController.php
BSD-3-Clause
public function errorMessage($message) { Yii::$service->page->message->addError($message); return Yii::$service->url->redirectByUrlKey('checkout/cart'); }
@param $message | String 添加报错信息
errorMessage
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/controllers/ReorderController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/controllers/ReorderController.php
BSD-3-Clause
public function actionIndex() { if (Yii::$service->store->isAppServerMobile()) { $urlPath = 'checkout/onepage'; Yii::$service->store->redirectAppServerMobile($urlPath); } $guestOrder = Yii::$app->store->get('order', 'guestOrder'); if($guestOrder != Yii::$app->store->enable && Yii::$app->user->isGuest){ $checkoutOrderUrl = Yii::$service->url->getUrl('checkout/onepage/index'); Yii::$service->customer->setLoginSuccessRedirectUrl($checkoutOrderUrl); return Yii::$service->url->redirectByUrlKey('customer/account/login'); } $_csrf = Yii::$app->request->post('_csrf'); if ($_csrf) { $status = $this->getBlock('placeorder')->getLastData(); if (!$status) { //var_dump(Yii::$service->helper->errors->get()); //exit; } } $data = $this->getBlock()->getLastData(); if (is_array($data) && !empty($data)) { return $this->render($this->action->id, $data); } else { return $data; } }
public function init(){ Yii::$service->page->theme->layoutFile = 'one_step_checkout.php'; }
actionIndex
php
fecshop/yii2_fecshop
app/appfront/modules/Checkout/controllers/OnepageController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Checkout/controllers/OnepageController.php
BSD-3-Clause
protected function getProductPage() { $productNumPerPage = $this->getNumPerPage(); $productCount = $this->_productCount; $pageNum = $this->getPageNum(); //echo $productCount; $config = [ 'class' => 'fecshop\app\appfront\widgets\Page', 'view' => 'widgets/page.php', 'pageNum' => $pageNum, 'numPerPage' => $productNumPerPage, 'countTotal' => $productCount, 'page' => $this->_page, ]; return Yii::$service->page->widget->renderContent('category_product_page', $config); }
得到toolbar的分页部分
getProductPage
php
fecshop/yii2_fecshop
app/appfront/modules/Catalogsearch/block/index/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalogsearch/block/index/Index.php
BSD-3-Clause
protected function getFilterAttr() { if (!$this->_filter_attr) { $this->_filter_attr = $filterAttr = Yii::$service->search->filterAttr; } return $this->_filter_attr; }
得到侧栏属性过滤属性
getFilterAttr
php
fecshop/yii2_fecshop
app/appfront/modules/Catalogsearch/block/index/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalogsearch/block/index/Index.php
BSD-3-Clause
protected function getFormatFilterPrice($price_item) { list($f_price, $l_price) = explode('-', $price_item); $str = ''; if ($f_price == '0' || $f_price) { $f_price = Yii::$service->product->price->formatPrice($f_price); $str .= $f_price['symbol'].$f_price['value'].'---'; } if ($l_price) { $l_price = Yii::$service->product->price->formatPrice($l_price); $str .= $l_price['symbol'].$l_price['value']; } return $str; }
产品价格显示格式处理
getFormatFilterPrice
php
fecshop/yii2_fecshop
app/appfront/modules/Catalogsearch/block/index/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalogsearch/block/index/Index.php
BSD-3-Clause
protected function getOrderBy() { $primaryKey = Yii::$service->category->getPrimaryKey(); $sort = Yii::$app->request->get($this->_sort); $direction = Yii::$app->request->get($this->_direction); $category_query_config = Yii::$app->controller->module->params['category_query']; if (isset($category_query_config['sort'])) { $sortConfig = $category_query_config['sort']; if (is_array($sortConfig)) { //return $category_query_config['numPerPage'][0]; if ($sort && isset($sortConfig[$sort])) { $orderInfo = $sortConfig[$sort]; } else { foreach ($sortConfig as $k => $v) { $orderInfo = $v; if (!$direction) { $direction = $v['direction']; } break; } } $db_columns = $orderInfo['db_columns']; if ($direction == 'desc') { $direction = -1; } else { $direction = 1; } //var_dump([$db_columns => $direction]); return [$db_columns => $direction]; } } }
得到排序数组,用于查询。
getOrderBy
php
fecshop/yii2_fecshop
app/appfront/modules/Catalogsearch/block/index/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalogsearch/block/index/Index.php
BSD-3-Clause
protected function getNumPerPage() { if (!$this->_numPerPageVal) { $numPerPage = Yii::$app->request->get($this->_numPerPage); $category_query_config = Yii::$app->controller->module->params['search_query']; if (!$numPerPage) { if (isset($category_query_config['numPerPage'])) { if (is_array($category_query_config['numPerPage'])) { $this->_numPerPageVal = $category_query_config['numPerPage'][0]; } } } elseif (!$this->_numPerPageVal) { if (isset($category_query_config['numPerPage']) && is_array($category_query_config['numPerPage'])) { $numPerPageArr = $category_query_config['numPerPage']; if (in_array((int) $numPerPage, $numPerPageArr)) { $this->_numPerPageVal = $numPerPage; } else { throw new InvalidValueException('Incorrect numPerPage value:'.$numPerPage); } } } } return $this->_numPerPageVal; }
得到每页显示的产品的个数。
getNumPerPage
php
fecshop/yii2_fecshop
app/appfront/modules/Catalogsearch/block/index/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalogsearch/block/index/Index.php
BSD-3-Clause
protected function getPageNum() { $numPerPage = Yii::$app->request->get($this->_page); return $numPerPage ? (int) $numPerPage : 1; }
得到第几页
getPageNum
php
fecshop/yii2_fecshop
app/appfront/modules/Catalogsearch/block/index/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalogsearch/block/index/Index.php
BSD-3-Clause
protected function getSearchProductColl() { $select = [ 'product_id','sku', 'spu', 'name', 'image', 'price', 'special_price', 'special_from', 'special_to', 'brand_id','is_in_stock', 'url_key', 'score', 'reviw_rate_star_average', 'review_count' ]; $where = $this->_where; $search_text = Yii::$app->controller->module->params['search_query']; $pageNum = $this->getPageNum(); $numPerPage = $this->getNumPerPage(); $product_search_max_count = Yii::$app->controller->module->params['product_search_max_count']; $filterAttr = $this->getFilterAttr(); return Yii::$service->search->getSearchProductColl($select, $where, $pageNum, $numPerPage, $product_search_max_count, $filterAttr); }
得到搜索的产品collection
getSearchProductColl
php
fecshop/yii2_fecshop
app/appfront/modules/Catalogsearch/block/index/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalogsearch/block/index/Index.php
BSD-3-Clause
protected function initSearch() { //$primaryKey = Yii::$service->category->getPrimaryKey(); //$primaryVal = Yii::$app->request->get($primaryKey); //$this->_primaryVal = $primaryVal; //$category = Yii::$service->category->getByPrimaryKey($primaryVal); //$this->_category = $category ; $searchText = Yii::$app->request->get('q'); $searchText = \Yii::$service->helper->htmlEncode($searchText); $this->_searchText = $searchText; $search_page_title_format = Yii::$app->controller->module->params['search_page_title_format']; $search_page_meta_keywords_format = Yii::$app->controller->module->params['search_page_meta_keywords_format']; $search_page_meta_description_format = Yii::$app->controller->module->params['search_page_meta_description_format']; $this->breadcrumbs(); if ($search_page_title_format) { $title = str_replace('%s', $searchText, $search_page_title_format); } else { $title = $searchText; } if ($search_page_meta_keywords_format) { $meta_keywords = str_replace('%s', $searchText, $search_page_meta_keywords_format); } else { $meta_keywords = $searchText; } if ($search_page_meta_description_format) { $meta_description = str_replace('%s', $searchText, $search_page_meta_description_format); } else { $meta_description = $searchText; } Yii::$app->view->registerMetaTag([ 'name' => 'keywords', 'content' => $meta_keywords, ]); Yii::$app->view->registerMetaTag([ 'name' => 'description', 'content' => $meta_description, ]); $this->_title = $title; Yii::$app->view->title = $this->_title; $this->_where = $this->initWhere(); }
初始化部分
initSearch
php
fecshop/yii2_fecshop
app/appfront/modules/Catalogsearch/block/index/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalogsearch/block/index/Index.php
BSD-3-Clause
protected function breadcrumbs() { if (Yii::$app->controller->module->params['search_breadcrumbs']) { Yii::$service->page->breadcrumbs->addItems(['name' => $this->_searchText]); } else { Yii::$service->page->breadcrumbs->active = false; } }
面包屑导航
breadcrumbs
php
fecshop/yii2_fecshop
app/appfront/modules/Catalogsearch/block/index/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalogsearch/block/index/Index.php
BSD-3-Clause
public function getLastData() { $product_id = Yii::$app->request->post('product_id'); //没有登录的用户跳转到登录页面 if (Yii::$app->user->isGuest) { $product = Yii::$service->product->getByPrimaryKey($product_id); $url = Yii::$service->url->getUrl($product['url_key']); Yii::$service->customer->setLoginSuccessRedirectUrl($url); return Yii::$service->url->redirectByUrlKey('customer/account/login'); } $identity = Yii::$app->user->identity; $user_id = $identity->id; $addStatus = Yii::$service->product->favorite->add($product_id, $user_id); if (!$addStatus) { Yii::$service->page->message->addByHelperErrors(); } //$favoriteParam = Yii::$app->getModule('catalog')->params['favorite']; $appName = Yii::$service->helper->getAppName(); $category_breadcrumbs = Yii::$app->store->get($appName.'_catalog','favorite_addSuccessRedirectFavoriteList'); // 跳转。 if ($category_breadcrumbs == Yii::$app->store->enable) { return Yii::$service->url->redirectByUrlKey('customer/productfavorite'); } else { $product = Yii::$service->product->getByPrimaryKey($product_id); $urlKey = $product['url_key']; return Yii::$service->url->redirectByUrlKey($urlKey); } }
用户添加收藏产品
getLastData
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/favoriteproduct/Add.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/favoriteproduct/Add.php
BSD-3-Clause
public function getAddCaptcha() { if (!$this->_add_captcha) { $appName = Yii::$service->helper->getAppName(); $addCaptcha = Yii::$app->store->get($appName.'_catalog','review_add_captcha'); // $reviewParam = Yii::$app->getModule('catalog')->params['review']; $this->_add_captcha = ($addCaptcha == Yii::$app->store->enable) ? true : false; } return $this->_add_captcha; }
@return boolean , review页面是否开启验证码验证。
getAddCaptcha
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/reviewproduct/Add.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/reviewproduct/Add.php
BSD-3-Clause
protected function getProductPriceInfo($product) { $price = $product['price']; $special_price = $product['special_price']; $special_from = $product['special_from']; $special_to = $product['special_to']; return Yii::$service->product->price->getCurrentCurrencyProductPriceInfo($price, $special_price, $special_from, $special_to); }
@param $product | String Or Object 得到产品的价格信息
getProductPriceInfo
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/reviewproduct/Add.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/reviewproduct/Add.php
BSD-3-Clause
protected function getSpuData() { $spu = $this->_product['spu']; $filter = [ 'select' => ['size'], 'where' => [ ['spu' => $spu], ], 'asArray' => true, ]; $coll = Yii::$service->product->coll($filter); if (is_array($coll['coll']) && !empty($coll['coll'])) { foreach ($coll['coll'] as $one) { $spu = $one['spu']; } } }
废弃
getSpuData
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/reviewproduct/Add.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/reviewproduct/Add.php
BSD-3-Clause
protected function getProductPage($countTotal) { if ($countTotal <= $this->numPerPage) { return ''; } $config = [ 'class' => 'fecshop\app\appfront\widgets\Page', 'view' => 'widgets/page.php', 'pageNum' => $this->pageNum, 'numPerPage' => $this->numPerPage, 'countTotal' => $countTotal, 'page' => $this->_page, ]; return Yii::$service->page->widget->renderContent('category_product_page', $config); }
@param $countTotal | Int 得到toolbar的分页部分
getProductPage
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/reviewproduct/Lists.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/reviewproduct/Lists.php
BSD-3-Clause
public function initParam() { $this->pageNum = Yii::$app->request->get($this->_page); $this->pageNum = $this->pageNum ? $this->pageNum : 1; $this->spu = Yii::$app->request->get('spu'); $this->product_id = Yii::$app->request->get('_id'); // $review = Yii::$app->getModule('catalog')->params['review']; $appName = Yii::$service->helper->getAppName(); $reviewPageReviewCount = Yii::$app->store->get($appName.'_catalog','review_reviewPageReviewCount'); //$productPageReviewCount = $reviewPageReviewCount ? $reviewPageReviewCount : 10; $this->numPerPage = $reviewPageReviewCount ? $reviewPageReviewCount : $this->numPerPage; }
初始化参数
initParam
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/reviewproduct/Lists.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/reviewproduct/Lists.php
BSD-3-Clause
protected function getProductPriceInfo($product) { $price = $product['price']; $special_price = $product['special_price']; $special_from = $product['special_from']; $special_to = $product['special_to']; return Yii::$service->product->price->getCurrentCurrencyProductPriceInfo($price, $special_price, $special_from, $special_to); }
产品价格信息
getProductPriceInfo
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/reviewproduct/Lists.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/reviewproduct/Lists.php
BSD-3-Clause
public function getAllItems() { $custom_option_attr_info = Yii::$service->product->getCustomOptionAttrInfo($this->attr_group); //######### $my_arr = []; $arr = []; //#在custom_option里面第一个属性 $img_attr = ''; if (is_array($custom_option_attr_info) && !empty($custom_option_attr_info)) { foreach ($custom_option_attr_info as $attr => $info) { if (isset($info['showAsImg']) && $info['showAsImg']) { $img_attr = $attr; break; } } } $img_arr = []; if (is_array($this->custom_option) && (!empty($this->custom_option))) { foreach ($this->custom_option as $option) { $qty = $option['qty']; if ($qty > 0) { $this->_custom_option_arr[] = $option; if (isset($option[$img_attr])) { $val = $option[$img_attr]; $img_arr[$val] = $option['image']; } foreach ($option as $k=>$v) { $my_arr[$k][] = $v; } } } } if (is_array($custom_option_attr_info) && !empty($custom_option_attr_info)) { foreach ($custom_option_attr_info as $attr => $info) { if (isset($info['display']['type']) && ($info['display']['type'] == 'select')) { if (isset($info['display']['data']) && is_array($info['display']['data'])) { foreach ($info['display']['data'] as $val) { if (is_array($my_arr[$attr]) && in_array($val, $my_arr[$attr])) { $t_arr = [ 'key' => $val, 'val' => $val, ]; $require = isset($info['require']) ? $info['require'] : 0; if (isset($info['showAsImg']) && $info['showAsImg']) { if (isset($img_arr[$val])) { $t_arr['image'] = $img_arr[$val]; } } $arr[$attr]['info'][] = $t_arr; $arr[$attr]['require'] = $require; } } } } } } return $arr; }
得到custom option 部分
getAllItems
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/product/CustomOption.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/product/CustomOption.php
BSD-3-Clause
public function getLastData() { if (!$this->spu || !$this->product_id) { return; } if ($this->filterBySpu) { $data = $this->getReviewsBySpu($this->spu); $count = $data['count']; $coll = $data['coll']; return [ '_id' => $this->product_id, 'spu' => $this->spu, 'coll' => $coll, 'noActiveStatus'=> Yii::$service->product->review->noActiveStatus(), 'reviw_rate_star_info' => $this->reviw_rate_star_info, 'review_count' => $this->review_count, 'reviw_rate_star_average' => $this->reviw_rate_star_average, ]; } }
得到当前spu下面的所有评论信息。
getLastData
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/product/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/product/Review.php
BSD-3-Clause
public function filterProductImg($product_images){ $this->_image_thumbnails = $product_images; //$this->_image_detail['gallery'] = $product_images['gallery']; if (isset($product_images['main']['is_detail']) && $product_images['main']['is_detail'] == 1 ) { $this->_image_detail[] = $product_images['main']; } if (isset($product_images['gallery']) && is_array($product_images['gallery'])) { $thumbnails_arr = []; //$detail_arr = []; foreach ($product_images['gallery'] as $one) { $is_thumbnails = $one['is_thumbnails']; $is_detail = $one['is_detail']; if($is_thumbnails == 1){ $thumbnails_arr[] = $one; } if($is_detail == 1){ $this->_image_detail[] = $one; } } $this->_image_thumbnails['gallery'] = $thumbnails_arr; //$this->_image_detail = $detail_arr; } }
@param $product_images | Array ,产品的图片属性 根据图片数组,得到橱窗图,和描述图 橱窗图:在产品详细页面顶部,放大镜显示部分的产品列表 描述图,在产品description文字描述后面显示的产品图片。
filterProductImg
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/product/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/product/Index.php
BSD-3-Clause
protected function getSpuData($select) { $spu = $this->_product['spu']; return Yii::$service->product->spuCollData($select, $this->_productSpuAttrArr, $spu); }
@param $select | Array , 需要查询的字段。 得到当前spu下面的所有的sku的数组、 这个是为了产品详细页面的spu下面的产品切换,譬如同一spu下的不同的颜色尺码切换。
getSpuData
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/product/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/product/Index.php
BSD-3-Clause
protected function getSpuAttrInfo($spuAttr, $attrVal, $reverse_val_spu) { $current = $this->_currentSpuAttrValArr; $active = false; if (isset($this->_currentSpuAttrValArr[$spuAttr])) { if ($attrVal != $this->_currentSpuAttrValArr[$spuAttr]) { $current[$spuAttr] = $attrVal; } else { $active = true; } } $reverse_key = $this->generateSpuReverseKey($current); $return = []; $return['attr_val'] = $attrVal; $return['active'] = 'noactive'; if (isset($reverse_val_spu[$reverse_key]) && is_array($reverse_val_spu[$reverse_key])) { $return['active'] = 'active'; $arr = $reverse_val_spu[$reverse_key]; foreach ($arr as $k=>$v) { $return[$k] = $v; } if ($spuAttr == $this->_spuAttrShowAsImg) { $return['show_as_img'] = $arr['main_img']; } } else if ($this->_spuAttrShowAsImg){ // 如果是图片,不存在,则使用备用的。 if ($spuAttr == $this->_spuAttrShowAsImg) { $return['active'] = 'active'; $arr = $this->_spuAttrShowAsImgArr[$attrVal]; if (is_array($arr) && !empty($arr)) { foreach ($arr as $k=>$v) { $return[$k] = $v; } } $return['show_as_img'] = $arr['main_img']; } } else if ($this->_spuAttrShowAsTop){ if ($spuAttr == $this->_spuAttrShowAsTop) { $return['active'] = 'active'; $arr = $this->_spuAttrShowAsTopArr[$attrVal]; if (is_array($arr) && !empty($arr)) { foreach ($arr as $k=>$v) { $return[$k] = $v; } } } } if ($active) { $return['active'] = 'current'; } return $return; }
spu属性部分
getSpuAttrInfo
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/product/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/product/Index.php
BSD-3-Clause
protected function sortSpuAttr($spuAttr, $data) { // 对size排序一下 $size = []; $attr_group = $this->_product['attr_group']; $attrInfo = Yii::$service->product->getGroupAttrInfo($attr_group); $size_arr = isset($attrInfo[$spuAttr]['display']['data']) ? $attrInfo[$spuAttr]['display']['data'] : ''; $d_arr = []; if (is_array($size_arr) && !empty($size_arr)) { foreach ($size_arr as $size) { if (isset($data[$size])) { $d_arr[$size] = $data[$size]; } } if (!empty($d_arr)) { // 不在里面的规格属性(新建产品添加的规格属性),添加进去 foreach ($data as $size=>$d) { if (!isset($d_arr[$size])) { $d_arr[$size] = $data[$size]; } } return $d_arr; } } return $data; }
@param $data | Array 各个尺码对应的产品数组 @return array 排序后的数组 该函数,按照在配置中的size的顺序,将$data中的数据进行排序,让其按照尺码的由小到大的顺序 排列,譬如 :s,m,l,xl,xxl,xxxl等
sortSpuAttr
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/product/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/product/Index.php
BSD-3-Clause
protected function getProductPriceInfo() { $price = $this->_product['price']; $special_price = $this->_product['special_price']; $special_from = $this->_product['special_from']; $special_to = $this->_product['special_to']; return Yii::$service->product->price->getCurrentCurrencyProductPriceInfo($price, $special_price, $special_from, $special_to); }
@return array 得到当前货币状态下的产品的价格和特价信息。
getProductPriceInfo
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/product/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/product/Index.php
BSD-3-Clause
protected function initProduct() { $primaryKey = Yii::$service->product->getPrimaryKey(); $primaryVal = Yii::$app->request->get($primaryKey); $this->_primaryVal = $primaryVal; $product = Yii::$service->product->getByPrimaryKey($primaryVal); if ($product) { $enableStatus = Yii::$service->product->getEnableStatus(); if ($product['status'] != $enableStatus){ return false; } } else { return false; } $this->_product = $product; Yii::$app->view->registerMetaTag([ 'name' => 'keywords', 'content' => Yii::$service->store->getStoreAttrVal($product['meta_keywords'], 'meta_keywords'), ]); Yii::$app->view->registerMetaTag([ 'name' => 'description', 'content' => Yii::$service->store->getStoreAttrVal($product['meta_description'], 'meta_description'), ]); $this->_title = Yii::$service->store->getStoreAttrVal($product['meta_title'], 'meta_title'); $name = Yii::$service->store->getStoreAttrVal($product['name'], 'name'); $this->breadcrumbs($name); $this->_title = $this->_title ? $this->_title : $name; Yii::$app->view->title = $this->_title; //$this->_where = $this->initWhere(); // 通过上面查询的属性组,得到属性组对应的属性列表 // 然后重新查询产品 $attr_group = $this->_product['attr_group']; Yii::$service->product->addGroupAttrs($attr_group); // 重新查询产品信息。 $product = Yii::$service->product->getByPrimaryKey($primaryVal); $this->_product = $product; return true; }
初始化数据包括 主键值:$this->_primaryVal 当前产品对象:$this->_product Meta keywords , Meta Description等信息的设置。 Title的设置。 根据当前产品的attr_group(属性组信息)重新给Product Model增加相应的属性组信息 然后,重新获取当前产品对象:$this->_product,此时加入了配置中属性组对应的属性。
initProduct
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/product/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/product/Index.php
BSD-3-Clause
protected function breadcrumbs($name) { $appName = Yii::$service->helper->getAppName(); $category_breadcrumbs = Yii::$app->store->get($appName.'_catalog','product_breadcrumbs'); if ($category_breadcrumbs == Yii::$app->store->enable) { Yii::$service->page->breadcrumbs->addItems(['name' => $name]); } else { Yii::$service->page->breadcrumbs->active = false; } }
面包屑导航
breadcrumbs
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/product/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/product/Index.php
BSD-3-Clause
protected function getProductBuyAlsoBuy() { $buy_also_buy_sku = $this->_product['buy_also_buy_sku']; if ($buy_also_buy_sku) { $skus = explode(',', $buy_also_buy_sku); if (is_array($skus) && !empty($skus)) { $filter['select'] = [ 'sku', 'spu', 'name', 'image', 'price', 'special_price', 'special_from', 'special_to', 'url_key', 'score', 'reviw_rate_star_average', 'review_count' ]; $filter['where'] = ['in', 'sku', $skus]; $products = Yii::$service->product->getProducts($filter); //var_dump($products); $products = Yii::$service->category->product->convertToCategoryInfo($products); return $products; } } }
买了的人还买了什么,通过产品字段取出来sku,然后查询得到。
getProductBuyAlsoBuy
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/product/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/product/Index.php
BSD-3-Clause
protected function getFilterCategory() { $category_id = $this->_primaryVal; $parent_id = $this->_category['parent_id']; $filter_category = Yii::$service->category->getFilterCategory($category_id, $parent_id); return $filter_category; }
得到子分类,如果子分类不存在,则返回同级分类。
getFilterCategory
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getProductMiniPage() { $productNumPerPage = $this->getNumPerPage(); $productCount = $this->_productCount; $pageNum = $this->getPageNum(); $config = [ 'class' => 'fecshop\app\appfront\widgets\Page', 'view' => 'widgets/page_mini.php', 'method' => 'getMiniBar', 'pageNum' => $pageNum, 'numPerPage' => $productNumPerPage, 'countTotal' => $productCount, 'page' => $this->_page, ]; return Yii::$service->page->widget->renderContent('category_product_page', $config); }
得到产品页面的toolbar部分 也就是分类页面的分页工具条部分。
getProductMiniPage
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getProductPage() { $productNumPerPage = $this->getNumPerPage(); $productCount = $this->_productCount; $pageNum = $this->getPageNum(); $config = [ 'class' => 'fecshop\app\appfront\widgets\Page', 'view' => 'widgets/page.php', 'pageNum' => $pageNum, 'numPerPage' => $productNumPerPage, 'countTotal' => $productCount, 'page' => $this->_page, ]; return Yii::$service->page->widget->renderContent('category_product_page', $config); }
得到产品页面的toolbar部分 也就是分类页面的分页工具条部分。
getProductPage
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getFilterAttr() { return Yii::$service->category->getFilterAttr($this->_category); }
@return Array 得到当前分类,侧栏用于过滤的属性数组,由三部分计算得出 1.全局默认属性过滤(catalog module 配置文件中配置 category_filter_attr), 2.当前分类属性过滤,也就是分类表的 filter_product_attr_selected 字段 3.当前分类去除的属性过滤,也就是分类表的 filter_product_attr_unselected 最终出来一个当前分类,用于过滤的属性数组。
getFilterAttr
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getFormatFilterPrice($price_item) { list($f_price, $l_price) = explode('-', $price_item); $str = ''; if ($f_price == '0' || $f_price) { $f_price = Yii::$service->product->price->formatPrice($f_price); $str .= $f_price['symbol'].$f_price['value'].'---'; } if ($l_price) { $l_price = Yii::$service->product->price->formatPrice($l_price); $str .= $l_price['symbol'].$l_price['value']; } return $str; }
格式化价格格式,侧栏价格过滤部分
getFormatFilterPrice
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getOrderBy() { $primaryKey = Yii::$service->category->getPrimaryKey(); $sort = Yii::$app->request->get($this->_sort); $direction = Yii::$app->request->get($this->_direction); $sortConfig = $this->_sort_items; if (is_array($sortConfig)) { if ($sort && isset($sortConfig[$sort])) { $orderInfo = $sortConfig[$sort]; } else { foreach ($sortConfig as $k => $v) { $orderInfo = $v; if (!$direction) { $direction = $v['direction']; } break; } } $db_columns = $orderInfo['db_columns']; $storageName = Yii::$service->product->serviceStorageName(); if ($direction == 'desc') { $direction = $storageName == 'mongodb' ? -1 : SORT_DESC; } else { $direction = $storageName == 'mongodb' ? 1 :SORT_ASC; } return [$db_columns => $direction]; } }
用于搜索条件的排序部分
getOrderBy
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getNumPerPage() { if (!$this->_numPerPageVal) { $numPerPage = Yii::$app->request->get($this->_numPerPage); //$category_query_config = Yii::$app->getModule('catalog')->params['category_query']; $appName = Yii::$service->helper->getAppName(); $categoryConfigNumPerPage = Yii::$app->store->get($appName.'_catalog','category_query_numPerPage'); $category_query_config['numPerPage'] = explode(',',$categoryConfigNumPerPage); if (!$numPerPage) { if (isset($category_query_config['numPerPage'])) { if (is_array($category_query_config['numPerPage'])) { $this->_numPerPageVal = $category_query_config['numPerPage'][0]; } } } elseif (!$this->_numPerPageVal) { if (isset($category_query_config['numPerPage']) && is_array($category_query_config['numPerPage'])) { $numPerPageArr = $category_query_config['numPerPage']; if (in_array((int) $numPerPage, $numPerPageArr)) { $this->_numPerPageVal = $numPerPage; } else { throw new InvalidValueException('Incorrect numPerPage value:'.$numPerPage); } } } } return $this->_numPerPageVal; }
分类页面的产品,每页显示的产品个数。 对于前端传递的个数参数,在后台验证一下是否是合法的个数(配置里面有一个分类产品个数列表) 如果不合法,则报异常 这个功能是为了防止分页攻击,伪造大量的不同个数的url,绕过缓存。
getNumPerPage
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getPageNum() { $numPerPage = Yii::$app->request->get($this->_page); return $numPerPage ? (int) $numPerPage : 1; }
得到当前第几页
getPageNum
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getCategoryProductColl() { $productPrimaryKey = Yii::$service->product->getPrimaryKey(); $select = [ $productPrimaryKey, 'sku', 'spu', 'name', 'image', 'price', 'special_price', 'special_from', 'special_to','is_in_stock', 'url_key', 'score', 'reviw_rate_star_average', 'review_count' ]; if (is_array($this->_sort_items)) { foreach ($this->_sort_items as $sort_item) { $select[] = $sort_item['db_columns']; } } $filter = [ 'pageNum' => $this->getPageNum(), 'numPerPage' => $this->getNumPerPage(), 'orderBy' => $this->getOrderBy(), 'where' => $this->_where, 'select' => $select, ]; //var_dump($filter);exit; return Yii::$service->category->product->getFrontList($filter); }
得到当前分类的产品
getCategoryProductColl
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function initCategory() { $primaryKey = Yii::$service->category->getPrimaryKey(); $primaryVal = Yii::$app->request->get($primaryKey); $this->_primaryVal = $primaryVal; $category = Yii::$service->category->getByPrimaryKey($primaryVal); if ($category) { $enableStatus = Yii::$service->category->getCategoryEnableStatus(); if ($category['status'] != $enableStatus){ return false; } } else { return false; } $this->_category = $category; Yii::$app->view->registerMetaTag([ 'name' => 'keywords', 'content' => Yii::$service->store->getStoreAttrVal($category['meta_keywords'], 'meta_keywords'), ]); Yii::$app->view->registerMetaTag([ 'name' => 'description', 'content' => Yii::$service->store->getStoreAttrVal($category['meta_description'], 'meta_description'), ]); $this->_title = Yii::$service->store->getStoreAttrVal($category['title'], 'title'); $name = Yii::$service->store->getStoreAttrVal($category['name'], 'name'); $this->breadcrumbs($name); $this->_title = $this->_title ? $this->_title : $name; Yii::$app->view->title = $this->_title; $this->_where = $this->initWhere(); return true; }
分类部分的初始化 对一些属性进行赋值。
initCategory
php
fecshop/yii2_fecshop
app/appfront/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Catalog/block/category/Index.php
BSD-3-Clause