You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

62 lines
1.7 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2020/3/18
  6. * Time: 5:26 PM
  7. */
  8. namespace addons\unishop\extend;
  9. /**
  10. * 商品相关逻辑
  11. * Class Product
  12. * @package addons\unishop\extend
  13. */
  14. class Product
  15. {
  16. /**
  17. * 获取商品的基础信息
  18. * @param array $product 商品信息数组
  19. * @param string $spec 规格值,用,号隔开
  20. * @param string $key 要获取的字段
  21. * @return array
  22. */
  23. public function getBaseData(array $product, string $spec = '', string $key = '')
  24. {
  25. if (!$product) {
  26. return [];
  27. }
  28. $data = [];
  29. if ($spec && $product['use_spec'] == \addons\unishop\model\Product::SPEC_ON && !empty($product['specTableList'])) {
  30. $specValueArr = json_decode($product['specTableList'], true);
  31. foreach ($specValueArr as $k => $specItem) {
  32. if (implode(',', $specItem['value']) == $spec) {
  33. if ($key) {
  34. $data = $specItem[$key];
  35. } else {
  36. $data = $specItem;
  37. $data['key'] = $k;
  38. }
  39. }
  40. }
  41. }
  42. if (empty($data)) {
  43. if ($key) {
  44. $data = $product[$key];
  45. } else {
  46. $data['market_price'] = $product['market_price'];
  47. $data['sales_price'] = $product['sales_price'];
  48. $data['stock'] = $product['stock'];
  49. $data['sales'] = $product['sales'];
  50. $data['image'] = $product['image'];
  51. }
  52. }
  53. if (is_array($data)){
  54. $data['image'] = $data['image'] ? $data['image'] : $product['image'];
  55. }
  56. return $data;
  57. }
  58. }