magento 1.x 方法总结

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();
    }




if($_product->getTypeId() == "configurable"):
    $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
    $simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
    foreach($simple_collection as $simple_product){
        echo Mage::helper('core')->currency($simple_product->getPrice());
    }
endif;

9 controller ajax输出 block

  $this->getLayout()->createBlock(blockname)->setDataName(value)->setIsAjax(true)->toHtml();
        $this->getResponse()->setBody(json_encode(array(data)));
        $this->setFlag('', self::FLAG_NO_POST_DISPATCH, true);

10 phtml 调用带有标签的内容

    <?php echo $this->helper('cms')->getBlockTemplateProcessor()->filter($_product->getDescription());?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值