Wie bekomme ich ein Versandangebot auf der Produktansichtsseite?
-
13-12-2019 - |
Frage
Ich möchte ein kleines Dienstprogramm wie anzeigen shipping calculator
auf der Produktansichtsseite.
wir wissen, dass natives Magento diesen Abschnitt auf der Warenkorbseite bereitstellt.
Wie kann ich es auf der Produktansichtsseite implementieren?
Ich habe ein kleines Formular mit Inhaltsfeldern wie Länder-Dropdown und Postleitzahl.
Wenn jemand auf klickt Calculate
dann sollte das Versandangebot mit Ajax angezeigt werden.
Ich bin hübsch hier geblieben.Hilf mir, bitte.
Ich habe geschaffen controller
& calculateAction()
Methode.Welchen Code kann ich da hineinschreiben calculateaction()
funktion ?
Lösung
Bitte fügen Sie den folgenden Code ein.phtml-Datei unter Ihrem Versandrechner-Formular für Ajax-Aufruf.
<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>
Jetzt müssen Sie eine Controller-Aktion erstellen, um die Ajax-Anforderung zu verarbeiten.Bitte beziehen Sie sich dazu auf das folgende Code-Snippet.
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);
}