私は特定の地域にのみ販売できる商品があります。そこで私は、ユーザーに配送郵便番号を尋ねるjavascriptポップアップを追加しました。そして、私はこのアイテムをそれらに販売できることを確認するためにWebサービスを打ちました。Magentoのカートに追加アクションで配送先住所を設定し、配送料金を収集するにはどうすればよいですか?
また、カートのページでは、適切な送料を計算できるように、配送郵便番号をお尋ねします。
私はすでに自分のジップをユーザーに要求していたら、自動的にカートに追加し、適切な送料率/方法で提示すると思った。
カートコントローラーのaddActionを拡張してzipを設定してレートを収集するだけで、zipが保存されていないように見えたり、レートを収集することはできません。ここで
は、私が使用している機能です。
public function addAction()
{
$cart = $this->_getCart();
$params = $this->getRequest()->getParams();
try {
if (isset($params['qty'])) {
$filter = new Zend_Filter_LocalizedToNormalized(
array('locale' => Mage::app()->getLocale()->getLocaleCode())
);
$params['qty'] = $filter->filter($params['qty']);
}
$product = $this->_initProduct();
$related = $this->getRequest()->getParam('related_product');
/**
* Check product availability
*/
if (!$product) {
$this->_goBack();
return;
}
$cart->addProduct($product, $params);
if (!empty($related)) {
$cart->addProductsByIds(explode(',', $related));
}
if((isset($params['territory_value_submitted']) && !empty($params['territory_value_submitted']))) {
$shipping_address = $cart->getQuote()->getShippingAddress();
$billing_address = $cart->getQuote()->getBillingAddress();
Mage::log('Current Zip: '.$shipping_address->getPostcode());
Mage::log('New Zip: '.$params['territory_value_submitted']);
if($shipping_address->getPostcode() != $params['territory_value_submitted']) {
$region = '';
// use web service to lookup coresponding state for this zip code
$state_lookup_response = file_get_contents(WEB_SERVICE_URL.'?zip='.$params['territory_value_submitted'].'&field=state');
if($state_lookup_response != '') {
$region = $state_lookup_response;
}
$shipping_address
->setCountryId('')
->setCity('')
->setPostcode($params['territory_value_submitted'])
->setRegion($region)
->setRegionId('')
->setCollectShippingRates(true)
->save();
Mage::log('Set Zip: '.$shipping_address->getPostalcode());
$current_bill_zip = $billing_address->getPostcode();
if(empty($current_bill_zip)) {
$billing_address
->setCountryId('')
->setCity('')
->setPostcode($params['territory_value_submitted'])
->setRegion($region)
->setRegionId('')
->save();
}
$ship_method = $shipping_address->getShippingMethod();
if(empty($ship_method)) {
$shipping_address->collectShippingRates();
$rates = $shipping_address->getAllShippingRates();
Mage::log('Available Rate Count: '.count($rates));
//if a free shipping method exists, choose that as the default
if($this->_getQuote()->getShippingAddress()->getShippingRateByCode('productmatrix_Free_Shipping') !== false) {
$this->_getQuote()->getShippingAddress()->setShippingMethod('productmatrix_Free_Shipping');
}
//if a standard shipping method exists, choose that as the default
elseif($this->_getQuote()->getShippingAddress()->getShippingRateByCode('productmatrix_Standard_Delivery') !== false) {
$this->_getQuote()->getShippingAddress()->setShippingMethod('productmatrix_Standard_Delivery');
}
$shipping_address->save();
}
$cart->getQuote()->save();
}
}
$cart->save();
$this->_getSession()->setCartWasUpdated(true);
/**
* @todo remove wishlist observer processAddToCart
*/
Mage::dispatchEvent('checkout_cart_add_product_complete',
array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
);
if (!$this->_getSession()->getNoCartRedirect(true)) {
if (!$cart->getQuote()->getHasError()){
$message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->htmlEscape($product->getName()));
$this->_getSession()->addSuccess($message);
}
$this->_goBack();
}
} catch (Mage_Core_Exception $e) {
if ($this->_getSession()->getUseNotice(true)) {
$this->_getSession()->addNotice($e->getMessage());
} else {
$messages = array_unique(explode("\n", $e->getMessage()));
foreach ($messages as $message) {
$this->_getSession()->addError($message);
}
}
$url = $this->_getSession()->getRedirectUrl(true);
if ($url) {
$this->getResponse()->setRedirect($url);
} else {
$this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
}
} catch (Exception $e) {
$this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));
Mage::logException($e);
$this->_goBack();
}
}
何でもより多くの奇妙なようだが、私は同じセッションでプロセスを複数回実行するとどのような記録されますが変更されるということです。
ここでは常に10001
の郵便番号を使用して、カートに3回、同じ製品を追加することから、ログです:第2回追加でSet Zip
は空ですが、Current Zip
ではないこと
2012-04-04T19:58:20+00:00 DEBUG (7): Current Zip:
2012-04-04T19:58:20+00:00 DEBUG (7): New Zip: 10001
2012-04-04T19:58:20+00:00 DEBUG (7): Set Zip:
2012-04-04T19:58:20+00:00 DEBUG (7): Available Rate Count: 0
2012-04-04T19:58:36+00:00 DEBUG (7): Current Zip:
2012-04-04T19:58:36+00:00 DEBUG (7): New Zip: 10001
2012-04-04T19:58:36+00:00 DEBUG (7): Set Zip:
2012-04-04T19:58:36+00:00 DEBUG (7): Available Rate Count: 0
2012-04-04T19:59:00+00:00 DEBUG (7): Current Zip: 10001
2012-04-04T19:59:00+00:00 DEBUG (7): New Zip: 10001
お知らせが3番目の追加のために。そして、新しいzipが現在のzipと同じであるため、3番目のaddは送料を収集しようとしません。
データが正しく読み込まれたり保存されないことを誰もが知っていますか?