magento - 在 getProductCollection 中添加自定义 sql,或插入地理位置公式

标签 magento geolocation

在以下文章的帮助下,我尝试按地理位置数据对 magento 中的产品列表进行排序:https://developers.google.com/maps/articles/phpsqlsearch_v3?hl=hu-HU .

我在我的模块中覆盖了 Mage_Catalog_Block_Product_List 以便我可以访问这行代码:

$this->_productCollection = $layer->getProductCollection();

拥有 _productCollection 对象后,我想将公式添加为“虚拟”属性“距离”,以便可以使用它对查询进行排序。这是我的想法:
$this->_productCollection = $layer->getProductCollection()
->addAttributeToSelect('distance','I insert my custom formula here')
->addAttributeToSort('distance','DESC');

我知道 API 不是用来处理这种语法的,但我认为它可以帮助人们理解这个想法。

这是我要添加的公式:

SELECT ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance



知道怎么做吗?

最佳答案

如果 latlng是您产品的属性,您可以使用 addExpressionAttributeToSelect():

$this->_productCollection
->addExpressionAttributeToSelect('distance', '(select ... {{lat}} ... {{lng}} ... )', array('lat'=>'lat', 'lng'=>'lng'));

在自定义选择查询中,latlng需要用双括号括起来(如图所示)。确保您的选择语句在括号内。

关于magento - 在 getProductCollection 中添加自定义 sql,或插入地理位置公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13883014/

相关文章:

javascript - 我试图在输入字段中显示地理位置 javascript 函数的结果

Magento 全页缓存与 Varnish

Magento,填充管理编辑表单上的复选框字段

rest - 如何在 Magento 2 的 API 中按类别过滤?

magento - 使用 group() 会破坏 magento 中的 getSelectCountSql

magento - 如何使用模块在magento admin中创建子菜单

java - Solr 地理空间搜索返回错误的距离

geolocation - 按邮政编码进行地理位置定位

google-maps - 谷歌地图 API 最大坐标长度?

javascript - 在授予的地理定位权限上附加事件监听器