magento1 得到产品的相关信息

366 查看

一、产品信息
1、先判定是否为产品页面    
   if (Mage::registry(‘product’)){
    
   }
2、获取产品的Id,根据Id得到产品对象
   通过sku获取产品对象:
   $_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
   $product_id = Mage::registry(‘current_product’)->getId();
   $_product = Mage::getModel(‘catalog/product’)->load($product_id);
3、产品的信息
  产品库存 $qtyStock = Mage::getModel('cataloginventory/stock_item')->getConllection()
    ->addfiledtofilter('product_id',$_product->getId());
    
  echo $_product->getShortDescription(); //product’s short description
  echo $_product->getDescription(); // product’s long description
  echo $_product->getName(); //product name
  echo $_product->getPrice(); //product’s regular Price
  echo $_product->getSpecialPrice(); //product’s special Price
  echo $_product->getProductUrl(); //product url
  echo $_product->getImageUrl(); //product’s image url
  echo $_product->getSmallImageUrl(); //product’s small image url
  echo $_product->getThumbnailUrl(); //product’s thumbnail image url
4、将产品加入购物车:
$this->getAddToCartUrl($_product) ;
注:如果产品购买时需要选择options下拉框时,该url将进入产品详情页,否则直接就如购物车。
5、在前台显示产品的自定义属性:
$_product->getResource()->getAttribute('code')->getFrontend()->getValue($_product)
code为属性创建时的code

二、用户订单
1、全部订单
 $_customer= Mage::getModel('customer/customer');
 $_customer->loadByEmail('用户登录邮箱');
  
 // get the customers last order
 $orders = Mage::getResourceModel('sales/order_collection')
    ->addFieldToSelect('*')
    ->addFieldToFilter('customer_id',$_customer->getId());
    ->addAttributeToSort('created_at','DESC');
  
  foreach($ordersas $order)
  { 
     echo$order->getId()."";
  }
2、最后一个订单
$_customer = Mage::getModel('customer/customer');
$_customer->loadByEmail('用户登录邮箱');
$orders = Mage::getResourceModel('sales/order_collection')
    ->addFieldToSelect('*')
    ->addFieldToFilter('customer_id', $_customer->getId())
    ->addAttributeToSort('created_at', 'DESC')
    ->setPageSize(1);
 
echo $orders->getFirstItem()->getId();