首页>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当订单全额退款完成后需要生成自定义邮件