magento 1.x 方法总结

来源:互联网 发布:2016软件人均产值 编辑:程序博客网 时间:2024/06/05 04:28

1 collection: 使用count 会使整个纪录load。 使用count后 ,collection 在使用 limit 会失效。获取 collection total 可以使用 方法 getSize()(该方法会使用另一个sql语句,会忽略 limit 的设置)

例:

Mage::getModel('catalog/product')->getCollection ->getSize()

collection 分页方法:

getCollection()->->setCurPage($page)->setPageSize($limit)

2 购物车商品列表问题
有关collectTotals() 方法问题
在调用collectTotals 的时候 ,程序是通过$this->getCheckoutSession()->getQuote() 获取有关购物车的商品,如果此时将购物车中的商品在后台禁用,而前台用户没有退出的情况,购物车列表会无法加载(报出: 嵌套的方法数量已经达到上限)。前台用户退出后,才可以正常访问购物车列表。

建议: 上线的商品,不要禁用,正确的方式是让商品售罄,由用户主动删除。或者将商品设置为过期商品

3 配置magneto 移动端和pc端的不同样式包
在system->general->design->package->增加匹配
匹配表达式:
phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone

value:包名

4 通过 configurable product 获取simple product

$configurableProduct = Mage::getModel('catalog/product')->load(1); $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$configurableProduct);   foreach($childProducts as $child) {    echo $child->getId();}

如果仅获取 simple product ids

$childProductsId = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($configproductid);

5 magento 各版本下载地址

https://www.magentocommerce.com/download?_ga=1.17642041.1692527827.1477279402

6 获取购物车 详细报错信息的方法

foreach ($quote->getAllItems() as $quoteItem)    {        if ($errorItems = $quoteItem->getErrorInfos())        {            foreach ($errorItems as $errorItem)            {                if ($errorItem['code'] ==             Mage_CatalogInventory_Helper_Data::ERROR_QTY)                {                    $quote->addErrorInfo(                        'error',                        'cataloginventory',                        Mage_CatalogInventory_Helper_Data::ERROR_QTY,                        Mage::helper('cataloginventory')->__('Not all products are available in the requested quantity')                    );                    return;                }            }        }    }

7 订单save 操作,对sales_flat_order_address 自定义字段的操作

   <checkout_submit_all_after>              <observers>                  <auto_set_custom_field>                      <class>module/observer</class>                      <method>method</method>                  </auto_set_custom_field>              </observers>   </checkout_submit_all_after>   public function method($observe){       // 获取保存后的order 信息       $order=$observe->getEvent()->getOrder();       ...    }

8 获取configurable product 商品信息形式如下:

"product_options":    [        {            "product_id": "2154",            "attributes":            [                {                    "attribute_id": "92",                    "attribute_code": "color",                    "attribute_label": "Color",                    "option_id": "178",                    "option_label": "奶油色"                },                {                    "attribute_id": "202",                    "attribute_code": "size",                    "attribute_label": "size",                    "option_id": "132",                    "option_label": "One size"                }            ],            "price": 1280,            "qty": 11111        }    ]

代码如下:

  function getProductOptions($currentProduct){        $productOptionData = array();        foreach ($this->getAllowProducts($currentProduct) as $product) {            $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);            $productOption = array(                "product_id" => (int)$product->getId(),                "attributes" => array(),                "price" => $currentProduct->getFinalPrice(),                "qty" => (int)$stock->getQty()            );            foreach ($this->getAllowAttributes($currentProduct) as $attribute) {                $productAttribute = $attribute->getProductAttribute();                $productAttributeId = $productAttribute->getId();                $attributeValue = $product->getData($productAttribute->getAttributeCode());                $optionPriceData = $attribute->getPrices()[array_search($attributeValue, array_column($attribute->getPrices(), 'value_index'))];                $productOption['attributes'][] = array(                    "attribute_id" => $productAttributeId,                    "attribute_code" => $productAttribute->getAttributeCode(),                    "attribute_label" => $productAttribute->getStoreLabel(),                    "option_id" => $attributeValue,                    "option_label" => $optionPriceData['label']                );                if ($optionPriceData['is_percent']) {                    $productOption['price'] += ($optionPriceData['pricing_value'] * $productOption['price'] / 100);                }                else {                    $productOption['price'] += $optionPriceData['pricing_value'];                }            }            $productOptionData[] = $productOption;        }        return $productOptionData;    }     function getAllowProducts($product)    {        $products = array();        $skipSaleableCheck = Mage::helper('catalog/product')->getSkipSaleableCheck();        if ($typeInstance = $product->getTypeInstance(true)) {            if ($typeInstance instanceof Mage_Catalog_Model_Product_Type_Configurable) {                $allProducts = $product->getTypeInstance(true)->getUsedProducts(null, $product);                foreach ($allProducts as $p) {                    if ($p->isSaleable() || $skipSaleableCheck) {                        $products[] = $p;                    }                }            }        }        return $products;    }     function getAllowAttributes($product)    {        if ($typeInstance = $product->getTypeInstance(true)) {            if ($typeInstance instanceof Mage_Catalog_Model_Product_Type_Configurable) {                return $typeInstance->getConfigurableAttributes($product);            }        }        return array();    }
0 0
原创粉丝点击