0

如何在 magento 的结帐页面上自定义免费的 shpping 文本。

我想在结帐页面添加一些文本来代替运费。

默认情况下,运费为:-“免费 0.00 美元”,我想将其更改为“免费 6.95 美元 - 6.95 美元 = 0.00 美元”。

问候, Ravijot Kaur

4

1 回答 1

1

Two solutions come to mind:

1) You can add an attribute to all the products and hard code how much you expect a shipping of each product to cost and then just sum up those numbers. In this case you would simply find all the templates in which you would like to change the text of free shipping, get all items that are currently in cart (see this answer on how you can get quote items), from them you can get product data with getProduct(), load your attribute, sum them up, format the price to your store's locale and print it out.

For example: one such location is app/design/frontend/base/default/template/checkout/onepage/shipping_method/available.phtml you would have to put an if statement there so that you would make the change only to free shipping. There you can see a call to $_rate->getCode(). If the output of that function matches to freeshipping_freeshipping then you have to attach your text to html.

2) A second and a bit more complex solution is that you select another shipping method and get the amount from it (e.g. you use ups shipping method to see how much it would cost if free shipping would not be used). For this you would have to create a helper class and implement a function that converts shipping adderess (class Mage_Sales_Model_Quote_Address) to a request (class Mage_Shipping_Model_Rate_Request)

public function prepareRequest( Mage_Sales_Model_Quote_Address $address )
{
  $request = Mage::getModel( 'shipping/rate_request' )
      ->setAllItems( $address->getAllItems() )
      ->setDestCountryId( $address->getCountryId() )
      ->setDestRegionId( $address->getRegionId() )
      ->setDestPostcode( $address->getPostcode() )
      ->setPackageValue( $address->getBaseSubtotal() )
      ->setPackageValueWithDiscount( $address->getBaseSubtotalWithDiscount() )
      ->setStoreId( Mage::app()->getStore()->getId() )
      ->setWebsiteId( Mage::app()->getStore()->getWebsiteId() )
      ->setBaseCurrency( Mage::app()->getStore()->getBaseCurrency() )
      ->setPackageCurrency( Mage::app()->getStore()->getCurrentCurrency() )
      ->setPackageQty( $address->getItemQty() )
      ->setPackageWeight( $address->getWeight() );

  return $request;
}

You can get address from qoute:

Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress();

With that request you would call collectRates function on a carrier of your choice (e.g. Mage_Usa_Model_Shipping_Carrier_Ups::collectRates). You can see an example of call to all the carriers in Mage_Shipping_Model_Shipping::collectRates and the function that gets called from ther Mage_Shipping_Model_Shipping::collectCarrierRates which contains $carrier->collectRates($request); - function call that you will have to make with your request.

collectRates function will return a result class (Mage_Shipping_Model_Tracking_Result) (use var_dump( $response->getData() ); or Mage::log( $response->getData() ); to see the structure) and from that class you will get the price that the carrier would charge for the delivery of products to shipping address.

With that number you then do the same as if you would use solution 1) (see For example part at the bottom of first solution):

  1. format price to your store's locale
  2. find all phtml files where you want to make the change
  3. check if shipping method is freeshipping_freeshipping
  4. if it is free shipping then add your text

EDIT:

Alternative to adding your text to each template file would be to use a rewrite on Mage_Shipping_Model_Carrier_Freeshipping class and add your text to $method->setMethodTitle($this->getConfigData('name')); setter.

EDIT 2:

Or better yet write your own shipping method based on free shipping method, this article and the solution above. That way you won't have to use a rewrite or change template files.

Edit 3:

I forgot to mention that in the above prepareRequest function you also have to set package_qty and package_weight (have added them now). This two values are calculated and added to address in Mage_Sales_Model_Quote_Address_Total_Shipping::collect.

于 2013-04-12T15:14:13.943 回答