我有一些代码位于 Magento 旁边的文件夹中。我要过来Mage.php做点事情。我希望能够在我的代码中获取某种运输报价。我一直在网上浏览,我正在努力寻找任何合理的地方。
请有人告诉我实现这一目标的最有效方法?
\n\n我只有以下信息可用于获取费率:
\n\nProduct ID eg, 123\nQuantity eg, 1020\nCountry Code eg, GB\nZip code if needed eg, SY12 6AX\nRun Code Online (Sandbox Code Playgroud)\n\n我想得到以下信息:
\n\nRate eg, \xc2\xa32.50\nTitle eg, Royal Mail Special Delivery\nID eg, 6\nRun Code Online (Sandbox Code Playgroud)\n\n然后,我想用我的代码中的选项填充单选列表,以便可以选择它们。
\n\n非常感谢
\n小智 5
好的,我成功了。这是以编程方式从 magento 获取运输报价的最终代码。
\n\n该函数将返回特定产品、数量、国家/地区、邮政编码的所有可用运费。该代码不包括免费送货,这可以通过删除来撤消if($_rate->getPrice() > 0) { ...
<?php\nrequire_once("Mage.php");\numask(0);\nini_set(\'display_errors\',true); Mage::setIsDeveloperMode(true);\nMage::app();\n\n\nfunction getShippingEstimate($productId,$productQty,$countryId,$postcode ) {\n\n $quote = Mage::getModel(\'sales/quote\')->setStoreId(Mage::app()->getStore(\'default\')->getId());\n $_product = Mage::getModel(\'catalog/product\')->load($productId);\n\n $_product->getStockItem()->setUseConfigManageStock(false);\n $_product->getStockItem()->setManageStock(false);\n\n $quote->addProduct($_product, $productQty);\n $quote->getShippingAddress()->setCountryId($countryId)->setPostcode($postcode); \n $quote->getShippingAddress()->collectTotals();\n $quote->getShippingAddress()->setCollectShippingRates(true);\n $quote->getShippingAddress()->collectShippingRates();\n\n $_rates = $quote->getShippingAddress()->getShippingRatesCollection();\n\n $shippingRates = array();\n foreach ($_rates as $_rate):\n if($_rate->getPrice() > 0) {\n $shippingRates[] = array("Title" => $_rate->getMethodTitle(), "Price" => $_rate->getPrice());\n }\n endforeach;\n\n return $shippingRates;\n\n}\necho "<pre>";\n// product id, quantity, country, postcode\nprint_r(getShippingEstimate(1098,100,"GB","SY21 7NQ"));\necho "</pre>";\nRun Code Online (Sandbox Code Playgroud)\n\n可以将其放入下拉列表中,如下所示:
\n\n$results = getShippingEstimate(1098,100000,"GB","SY21 7NQ");\n$count = -1;\necho "<select>";\nforeach ($results as $result):\n$count++;\n?>\n<option value="<?=$count?>"><?=$result["Title"]." - \xc2\xa3".$result["Price"]?></option>\n<?php\nendforeach;\necho "</select>"\nRun Code Online (Sandbox Code Playgroud)\n