浏览文章
文章信息
后端 Magento2 产品排序以及条件搜索
17271
function getFilterProduct($page, $pageSize) { $params = $this->request->getBodyParams(); $order = isset($params['order']) ? $params['order'] : 'bestseller'; $sort = isset($params['sort']) ? $params['sort'] : 'desc'; $priceFrom = isset($params['price']['from']) ? $params['price']['from'] : null; $priceTo = isset($params['price']['to']) ? $params['price']['to'] : null; $attributeFilters = (isset($params['filters']) && is_array($params['filters'])) ? $params['filters'] : []; // 分类ID if (!isset($params['category_id'])) return json_encode(['code' => 'ERR', 'msg' => 'category_id必填', 'data' => false]); $category_id = $params['category_id']; $category = $this->category->load($category_id); $collection = $this->collection->addCategoryFilter($category); // 属性筛选 foreach ($attributeFilters as $attributeFilter) { if (is_array($attributeFilter)) { $collection->addAttributeToFilter($attributeFilter['name'], $attributeFilter['value']); } else { return json_encode(['code' => 'ERR', 'msg' => 'filter结构错误!', 'data' => $attributeFilter]); } } $collection->addAttributeToSelect('*'); $collection->addAttributeToFilter('is_add_purchase', 0); $collection->addAttributeToFilter('visibility', 4); // 搜索排序方式 switch ($order) { case 'new': if ($priceFrom && empty($priceTo)) { $collection->addPriceDataFieldFilter('%s >= %s', ['min_price', $priceFrom]); } if (empty($priceFrom) && $priceTo) { $collection->addPriceDataFieldFilter('%s <= %s', ['min_price', $priceTo]); } if ($priceFrom && $priceTo) { $collection->addPriceDataFieldFilter('%s >= %s', ['min_price', $priceFrom]) ->addPriceDataFieldFilter('%s <= %s', ['min_price', $priceTo]); } $collection->addMinimalPrice(); $collection->setOrder('created_at', $sort); break; case 'price': if ($priceFrom && empty($priceTo)) { $collection->addPriceDataFieldFilter('%s >= %s', ['min_price', $priceFrom]); } if (empty($priceFrom) && $priceTo) { $collection->addPriceDataFieldFilter('%s <= %s', ['min_price', $priceTo]); } if ($priceFrom && $priceTo) { $collection->addPriceDataFieldFilter('%s >= %s', ['min_price', $priceFrom]) ->addPriceDataFieldFilter('%s <= %s', ['min_price', $priceTo]); } $collection = $collection->addMinimalPrice(); $collection->addAttributeToSort('minimal_price ', $sort); break; case 'bestseller': if ($priceFrom && empty($priceTo)) { $collection->addPriceDataFieldFilter('%s >= %s', ['min_price', $priceFrom]); } if (empty($priceFrom) && $priceTo) { $collection->addPriceDataFieldFilter('%s <= %s', ['min_price', $priceTo]); } if ($priceFrom && $priceTo) { $collection->addPriceDataFieldFilter('%s >= %s', ['min_price', $priceFrom]) ->addPriceDataFieldFilter('%s <= %s', ['min_price', $priceTo]); } $collection->addMinimalPrice(); $collection->joinTable( array('best' => 'sales_bestsellers_aggregated_monthly'), 'product_id=entity_id', ['qty_ordered', 'product_id'], null, 'left')->setOrder('SUM(qty_ordered)', $sort); break; default: if ($priceFrom && empty($priceTo)) { $collection->addPriceDataFieldFilter('%s >= %s', ['min_price', $priceFrom]); } if (empty($priceFrom) && $priceTo) { $collection->addPriceDataFieldFilter('%s <= %s', ['min_price', $priceTo]); } if ($priceFrom && $priceTo) { $collection->addPriceDataFieldFilter('%s >= %s', ['min_price', $priceFrom]) ->addPriceDataFieldFilter('%s <= %s', ['min_price', $priceTo]); } $collection->addMinimalPrice(); $collection->joinTable( array('best' => 'sales_bestsellers_aggregated_monthly'), 'product_id=entity_id', ['qty_ordered', 'product_id'], null, 'left')->setOrder('SUM(qty_ordered)', $sort); } $collection->setCurPage($page)->setPageSize($pageSize); // p($collection->getData()); return json_encode($collection->getData());