製品の表示ページで出荷見積もりを取得する方法
-
13-12-2019 - |
質問
shipping calculator
のような小さなユーティリティを製品ビューページに表示したい。
ネイティブマゼントはカートページでこのセクションを提供しています。
製品ビューページにどのように実装できるか?
国のドロップダウンと郵便番号のようなコンテンツフィールドが小さいフォームを持っています。
誰かがCalculate
をクリックすると、ajaxを使って配送料が表示されるべきです。
私はここにきれいに立ち往生しています。私を助けてください。
controller
&calculateAction()
メソッドを作成しました。どのコードがそのcalculateaction()
関数に書き込むことができるか?
解決
Ajax呼び出し用の送料計算フォームの下に、.phtmlファイルにコードを追加してください。
<script type="text/javascript">
//<![CDATA[
var coShippingEstimateForm = new VarienForm('your_form_id',true);
//]]>
function getEstimateShipping(){
if (coShippingEstimateForm.validator.validate()) {
new Ajax.Updater(
{ success:'result_container_id' }, "<?php echo $this->getUrl('your_route_name/your_controller/calculate') ?>", {
method:'post',
asynchronous:true,
evalScripts:false,
onSuccess:function(transport) {
var shiphtml = transport.responseText;
if(shiphtml != "" && shiphtml != null){
$('result_container_id').insert(shiphtml).show();
}else{
alert("No shipping method available");
}
$('submit').disabled = false;
},
onLoading:function(request, json){
$('submit').disabled = true;
},
parameters:jQuery('form').serialize(true)
}
);
}
}
</script>
.
Ajax要求を処理するためのコントローラアクションを作成する必要があります。以下のコードスニペットを参照してください。
public function calculateAction()
{
$country = (string) $this->getRequest()->getParam('country_id');
$postcode = (string) $this->getRequest()->getParam('estimate_postcode');
$qty = intval($this->getRequest()->getParam('qty'));
if($qty == 0 || $qty == null){
$qty = 1;
}
$currentProductId = $this->getRequest()->getPost('currunt_product');
$quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('default')->getId());
$_product = Mage::getModel('catalog/product')->load($currentProductId);
$params = $this->getRequest()->getParams();
$reqOb = new Varien_Object($params);
$_product->getStockItem()->setUseConfigManageStock(false);
$_product->getStockItem()->setManageStock(false);
$quote->addProduct($_product, $reqOb);
$quote->getShippingAddress()->setCountryId($country)->setPostcode($postcode);
$quote->getShippingAddress()->collectTotals();
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();
$groups = $quote->getShippingAddress()->getGroupedAllShippingRates();
$shippingRates = array();
$shippingHtml = "";
$shippingBlock = new Mage_Checkout_Block_Cart_Shipping();
foreach($groups as $code=>$_rates){
$shippingHtml .= "<dt>" . $shippingBlock->getCarrierName($code) . "</dt><dd><ul>";
foreach ($_rates as $_rate) {
//if($_rate->getPrice() > 0) {
$shippingHtml .= "<li><label>";
$shippingHtml .= $_rate->getMethodTitle();
$shippingHtml .= " - ";
$shippingHtml .= Mage::helper('core')->currency($_rate->getPrice(), true, false);
$shippingHtml .= "</label></li>";
}
$shippingHtml .= "</ul></dd>";
}
$this->getResponse()->setBody($shippingHtml);
}
. 所属していません magento.stackexchange