[英]How to query magento database tables to show categories and products?
I have a PHP website and now the I want to add a shopping cart to my existing php website. 我有一个PHP网站,现在我想向我现有的php网站添加购物车。 So I am using magento for this.
所以我正在使用magento。
In the directory of my website I have created a new directory with the name of "shop" and put magento in this directory. 在我的网站目录中,我创建了一个名为“shop”的新目录,并将magento放在此目录中。
My website is working fine. 我的网站工作正常。 But on website's home page I want to show categories and products from the magento site so it is not possible to use the magento built-in functions for this as my website's home page is out the magento site.
但是在网站的主页上我想显示来自magento网站的类别和产品,因此不可能使用magento内置功能,因为我的网站的主页不在magento网站上。
So I think I should query the magento database tables for this but the magento database tables are so complex so any body who can give me some idea about this problem? 因此,我认为我应该为此查询magento数据库表,但是magento数据库表是如此复杂,以至于有谁能给我一些有关此问题的想法?
Is there any other way to solve my problem and if the only the query is the solution then how will I query the magento database tables to show categories and products. 还有其他方法可以解决我的问题,如果唯一的查询是解决方案,那么我将如何查询magento数据库表以显示类别和产品。
Thanks in advance. 提前致谢。
I would try something like this: 我会尝试这样的事情:
require 'shop/app/Mage.php';
/* Run magento app */
Mage::app();
function get_categories(){
$category = Mage::getModel(’catalog/category’);
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
$arr = array();
if ($ids){
foreach ($ids as $id){
$cat = Mage::getModel(’catalog/category’);
$cat->load($id);
array_push($arr, $cat);
}
}
return $arr;
}
$categories = get_categories();
foreach($categories as $cat) {
echo "<br />Category: " . $cat->getName();
$collection = $cat->getProductCollection();
foreach ($collection as $item) {
$product = Mage::getModel("catalog/product")->load($item->getId());
echo "<br />Product: " . $product->getName();
}
}
You need not to write complex queries to show some categories in homepage. 您无需编写复杂的查询即可在首页中显示某些类别。 But instead you can add the below in your homepage (Menu > CMS > Pages > Homepage)
但您可以在主页中添加以下内容(菜单> CMS>页面>主页)
{{block type="catalog/product_list" category_id="3" template="catalog/product/list.phtml"}}
where category_id is the id of the category to be shown. 其中category_id是要显示的类别的ID。 See screenshot to get cat id .
查看截图以获取cat id 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.