Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Magento';上设置配送地址和收取运费;s添加到购物车操作?_Php_Magento_Magento 1.5 - Fatal编程技术网

Php 如何在Magento';上设置配送地址和收取运费;s添加到购物车操作?

Php 如何在Magento';上设置配送地址和收取运费;s添加到购物车操作?,php,magento,magento-1.5,Php,Magento,Magento 1.5,我有一些产品只能销售到特定地区。因此,我添加了一个javascript弹出窗口,询问用户的发货邮政编码,然后点击一个web服务确认我可以向他们出售此商品 此外,在购物车页面上,我要求提供送货邮政编码,以便计算正确的运费 我想,如果我已经要求用户提供他们的邮政编码,我会自动将其设置为“添加到购物车”,并向他们提供适当的运费/方式 我假设我可以简单地扩展购物车控制器的addAction来设置zip并收集费率,但是zip似乎没有保存,我也无法收集费率 下面是我正在使用的函数: public funct

我有一些产品只能销售到特定地区。因此,我添加了一个javascript弹出窗口,询问用户的发货邮政编码,然后点击一个web服务确认我可以向他们出售此商品

此外,在购物车页面上,我要求提供送货邮政编码,以便计算正确的运费

我想,如果我已经要求用户提供他们的邮政编码,我会自动将其设置为“添加到购物车”,并向他们提供适当的运费/方式

我假设我可以简单地扩展购物车控制器的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();
    }
}
更奇怪的是,当我在同一个会话中多次运行该过程时,记录的内容会发生变化

以下是将同一产品添加到购物车3次的记录,始终使用邮政编码
10001

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
请注意,第二次添加中的
Set Zip
为空,但
当前Zip
不适用于第三次添加。第三次添加不会尝试收取运费,因为新的邮政编码与当前邮政编码相同


有人知道是什么阻止了数据的正确加载或保存吗?

解决方案是添加我在
CartController::indexAction
函数中找到的函数调用

$cart->init()将购物车的状态设置为签出\状态\开始并清除所有相关地址。因此,需要在获取地址对象之前调用它

例如:

if((isset($params['territory_value_submitted']) && !empty($params['territory_value_submitted']))) {
        $cart->init();

        $shipping_address = $cart->getQuote()->getShippingAddress();
        $billing_address = $cart->getQuote()->getBillingAddress();
现在,在购物车页面上调用
$cart->init()
不会清除对地址所做的更改

此外,如果不为配送地址设置CountryId,则无法选择正确的配送方式。因为我只在美国境内发货,所以我的所有发货规则都限制在美国,没有找到匹配的规则

这是通过改变

$shipping_address
    ->setCountryId('')

$shipping_address
    ->setCountryId('US')