CartInfoController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fectfurnilife\app\appfront\modules\Checkout\controllers;
  10. use fecshop\app\appfront\modules\AppfrontController;
  11. use Yii;
  12. /**
  13. * @author Terry Zhao <2358269014@qq.com>
  14. * @since 1.0
  15. */
  16. class CartInfoController extends AppfrontController
  17. {
  18. public function actionIndex()
  19. {
  20. $arr = [];
  21. $arr['product_total'] = 0;
  22. $cartInfos = Yii::$service->cart->quoteItem->getCartProductInfo();
  23. if (is_array($cartInfos) && !empty($cartInfos)) {
  24. $currency_info = Yii::$service->page->currency->getCurrencyInfo();
  25. $arr['symbol'] = $currency_info['symbol'];
  26. $product_total = isset($cartInfos['product_total']) ? $cartInfos['product_total'] : 0;
  27. $arr['product_total'] = Yii::$service->helper->format->number_format($product_total);
  28. $products = $cartInfos['products'];
  29. if (is_array($products) && !empty($products)) {
  30. foreach ($products as $product) {
  31. $product_image = isset($product['product_image']['main']['image']) ? $product['product_image']['main']['image'] : '';
  32. $name = $product['name'];
  33. $product_price = $product['product_price'];
  34. $qty = $product['qty'];
  35. $arr['products'][] = [
  36. 'product_image' => Yii::$service->product->image->getResize($product_image, [100,100], false) ,
  37. 'name' => $name,
  38. 'product_price' => Yii::$service->helper->format->number_format($product_price),
  39. 'qty' => $qty,
  40. 'product_url' => Yii::$service->url->getUrl($product['product_url']),
  41. 'custom_option_info' => $this->getProductOptions($product),
  42. ];
  43. }
  44. }
  45. }
  46. echo json_encode($arr); exit;
  47. }
  48. /**
  49. * 将产品页面选择的颜色尺码等显示出来,包括custom option 和spu options部分的数据.
  50. */
  51. public function getProductOptions($product_one)
  52. {
  53. $custom_option_info_arr = [];
  54. $custom_option = isset($product_one['custom_option']) ? $product_one['custom_option'] : '';
  55. $custom_option_sku = $product_one['custom_option_sku'];
  56. if (isset($custom_option[$custom_option_sku]) && !empty($custom_option[$custom_option_sku])) {
  57. $custom_option_info = $custom_option[$custom_option_sku];
  58. foreach ($custom_option_info as $attr=>$val) {
  59. if (!in_array($attr, ['qty', 'sku', 'price', 'image'])) {
  60. $attr = str_replace('_', ' ', $attr);
  61. $attr = ucfirst($attr);
  62. $custom_option_info_arr[$attr] = $val;
  63. }
  64. }
  65. }
  66. $spu_options = $product_one['spu_options'];
  67. if (is_array($spu_options) && !empty($spu_options)) {
  68. foreach ($spu_options as $label => $val) {
  69. $custom_option_info_arr[$label] = $val;
  70. }
  71. }
  72. $str = '';
  73. if (is_array($custom_option_info_arr) && !empty($custom_option_info_arr)) {
  74. foreach ($custom_option_info_arr as $label => $val) {
  75. $str .= '<p>'.Yii::$service->page->translate->__(ucwords($label)).':'.Yii::$service->page->translate->__($val).'</p>';
  76. }
  77. }
  78. return $str;
  79. }
  80. }