首頁>Magento>source

我正在尝試以程式設計方式建立引用地址,一切正常並且订單已建立,但這是在 2 workarounds之後   我不知道為什麼我需要它们,我会列出我所有的观察結果,希望有人可以指出我做錯了什麼。

我最大的問题是計算運费和運费地址的建立方式,所以我们說建立報價單的基本方法如下(不介意變數)

// Initializing the Quote Model, all good here
$quote = Mage::getModel('sales/quote')
    ->setStoreId($store_id)
    ->setWebsiteId($website_id)
    ->assignCustomer($customer);
// The shipping address, all good here
$shipping_address = array(
    'prefix' => '',
    'firstname' => $customer->getFirstname(),
    'middlename' => '',
    'lastname' => $customer->getLastname(),
    'suffix' => '',
    'company' => '',
    'street' => array(
        '0' => $d['street'],
        '1' => ''
    ),
    'city' => $d['city'],
    'country_id' => $d['country_id'],
    'region' => $d['region'],
    'postcode' => '0000',
    'telephone' => $customer->getPhone(),
    'fax' => '',
    'vat_id' => '',
    'save_in_address_book' => 1
);
// Adding a product, all good here
$quote->addProduct($product_model, new Varien_Object(
    array(  'qty' => 1,
            'options' => $options
        )
));
// Setting the shipping/billing address
$quote->getShippingAddress()->addData($shipping_address);
$quote->getBillingAddress()->addData($shipping_address);
// Setting the shipping method, all good here as well
$methods = Mage::getSingleton('shipping/config')->getActiveCarriers();
foreach($methods as $_code => $_method){
    // Get the first allowed method of this shipping carrier
    $m = key($_method->getAllowedMethods());
    $shipping_method = $_code . '_' . $m;
}
$shippingAddress->setShippingMethod($shipping_method);
// Collecting the shipping rates, i have no clue what im doing wrong here
$shippingAddress->setWeight(90); // I will have to set his manually, not sure why
$shippingAddress->setFreeMethodWeight(90); // I will have to set this manually, not sure why
$shippingAddress->setShippingMethod($shipping_method);
$shippingAddress->setCollectShippingRates(true);
$shippingAddress->collectShippingRates();
$quote->collectTotals();

現在我想要檢索運费,所以我只是做了一个 $quote->getShippingAddress()->getShippingAmount()

問题是我必须按照以下方式設置運輸重量,如上面的代碼塊所示:

$shippingAddress->setWeight(90);
$shippingAddress->setFreeMethodWeight(90);

這是因為某種原因,在

app/code/core/Mage/Sales/Model/Quote/Address.php:913

$request->setPackageWeight($item ? $item->getRowWeight() : $this->getWeight());

getWeight   函式返迴0,如果我没有設置它是明確的,這是有道理的,因為在我的代碼中,我没有找到它的計算或修改,当我添加产品来获取權重. So this is my first issue, why im setting the weight explicitly, why i need to do so.

second issue   是 $shippingAddress->setFreeMethodWeight(90);   我必须這樣做,因為在

app/code/Mage/Shipping/Model/Carrier/Tablerate.php:138

// Package weight and qty free shipping
$oldWeight = $request->getPackageWeight();
$oldQty = $request->getPackageQty();
$request->setPackageWeight($request->getFreeMethodWeight());
$request->setPackageQty($oldQty - $freeQty);
$result = $this->_getModel('shipping/rate_result');
$rate = $this->getRate($request);
$request->setPackageWeight($oldWeight);
$request->setPackageQty($oldQty);

getRate   將包重量設置為 getFreeMethodWeight的返迴值後計算函式 ,如果我没有按上述方式設置,則為null.我有 FreeShipping   運輸方法 disabledno Cart Price rules ,所以我不知道為什麼上面這些行是用這種方式寫的。

這就是它. Hopefull我能够解釋一切。

最新回復
  • 2019-12-5
    1 #

    首先,你在哪裏宣告變數$ shippingAddress?

    由於你必须手動輸入重量,你可以查看 $product->getWeight();   给你90?

    如果没有,可能在当前商店的管理員中没有正確定義權重。

    否則,您是否可以尝試將商品添加到送货地址中,如下所示:

    $quote->getShippingAddress()->addData($shipping_address);
    $quote->getShippingAddress()->addItem($item);
    

  • magento2:在magento 2中使用此代碼$ resultPage-> setActiveMenu()有什麼用?
  • php:magento 1x当订單全額退款完成後需要生成自定義郵件