How to change the weight before sending to fedex magento? - magento

In magento checkout page, after giving billing information and shipping information, i understand that these details are sending to fedex then the shipping rates are populating in chekout page, before sending theses details to fedex, i want to change the weight of the product, i Want to add additional weights for each products,
suppose user adding a product with weight of 2 pounds, i want to send
these weight to 2*5 = 10pounds, how can i do that in magento? please
help.

Not sure to understand what you want exactly but I'll give a try.
I think you can override Mage_Checkout_OnepageController::savePaymentAction method in your local pool and in your local method, dispatch your own new event 'save_payment_action_before' and pass your objects as parameters.
In your fedex module, create an observer, get your object and change the order weight before it's sent to fedex.
To create your custom event, check this post

Finally i find out that it is happening in the sales/quote/item.php file, there is a function called setProduct, here we need to add addititonal info while setting data.
public function setProduct($product)
{
$batchQty = Mage::getModel('catalog/product')->load($product->getId())->getBatchQty();
$roleId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$userrole = Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');
$userrole = strtolower($userrole);
if ($this->getQuote()) {
$product->setStoreId($this->getQuote()->getStoreId());
$product->setCustomerGroupId($this->getQuote()->getCustomerGroupId());
}
if($userrole=="retailer" && $batchQty>0 ){
$this->setData('product', $product)
->setProductId($product->getId())
->setProductType($product->getTypeId())
->setSku($this->getProduct()->getSku())
->setName($product->getName())
->setWeight($this->getProduct()->getWeight()*$batchQty)
->setTaxClassId($product->getTaxClassId())
->setBaseCost($product->getCost())
->setIsRecurring($product->getIsRecurring());
} else {
$this->setData('product', $product)
->setProductId($product->getId())
->setProductType($product->getTypeId())
->setSku($this->getProduct()->getSku())
->setName($product->getName())
->setWeight($this->getProduct()->getWeight())
->setTaxClassId($product->getTaxClassId())
->setBaseCost($product->getCost())
->setIsRecurring($product->getIsRecurring());
}
if ($product->getStockItem()) {
$this->setIsQtyDecimal($product->getStockItem()->getIsQtyDecimal());
}
Mage::dispatchEvent('sales_quote_item_set_product', array(
'product' => $product,
'quote_item' => $this
));
return $this;
}

Related

Magento billing address

I am using the one page checkout module from AheadWorks and I am trying to only retrieve billing addresses from logged in members.
Currently the dropdown menu for choosing their address to bill from shows all addresses created by a logged in member. Also shipping adresses.
I would like this to only show billingaddresses. Is there a way to do so?
I already took a look inside the block which should be used, but unfortunately, I don't understand.
public function getAddressesHtmlSelect($type)
{
if ($this->isCustomerLoggedIn()) {
$options = array();
foreach ($this->getCustomer()->getAddresses() as $address) {
$options[] = array(
'value' => $address->getId(),
'label' => $address->format('oneline')
);
}
$addressDetails = Mage::getSingleton('checkout/session')->getData('aw_onestepcheckout_form_values');
if (isset($addressDetails[$type.'_address_id'])) {
if (empty($addressDetails[$type.'_address_id'])) {
$addressId = 0;
} else {
$addressId = $addressDetails[$type.'_address_id'];
}
} else {
$addressId = $this->getQuote()->getBillingAddress()->getCustomerAddressId();
}
if (empty($addressId) && $addressId !== 0) {
$address = $this->getCustomer()->getPrimaryBillingAddress();
if ($address) {
$addressId = $address->getId();
}
}
$select = $this->getLayout()->createBlock('core/html_select')
->setName($type.'_address_id')
->setId($type.'-address-select')
->setClass('address-select')
->setValue($addressId)
->setOptions($options);
return $select->getHtml();
}
return '';
}
Simple answer is no, you can't do that. In principle magento has customer entity and customer address entity. Customer address can have type which is billing or shipping but this only applies for quote and order. But when the address is saved in customer addresses it has no indication which one it is so you can use it for both steps.
Long answer. To achieve what you need you would have to extend magento customer address entity and put this division inside the entity itself. This would however require to rewrite some portion of the code which is to broad to give exact answer here.
Alternatively you could try to determine if an address is billing or shipping by going through previous orders of the user and checking which type it was there.
I was looking for this as well, here is something that I found, try it out: http://www.magebuzz.com/blog/disable-shipping-address-in-magento-checkout/

How to remove shipping using observer for checkout in magento?

My Problem:
I want to use observer before checkout onepage page begins, in that, based on products in the cart, I want to disable shipping if it doesn't match certain conditions.
I am using [controller_action_predispatch_checkout_onepage_index] event observer, this basically call before checkout page starts loading...I am able to get all products and quote info but didn't found any method to disable shipping.
What I am looking for,
from the observer, is it possible to disable shipping by calling certain magento methods or any other solutions?
Overriding collectRates()
After getting few replies, I am trying to override collectRates() method using code given below
$method = Mage::getModel('shipping/rate_result_method');
$method->setCarrier('flatrate');
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod('flatrate');
$method->setMethodTitle($this->getConfigData('name'));
if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes()) {
$shippingPrice = '0.00';
}
$method->setPrice($shippingPrice);
$method->setCost($shippingPrice);
$result->append($method);
Although, I dont want to enable flatrate shipping method either. I just want to disable shipping, or optionally return with reply something like
Free shipping $0.00
User can select that to continue to next step. Please help me from here..
What should I use in $method->setCarrier('??????');
or what changes I do need to do in above code?
I think it might be better to either override or subclass the individual Carrier models.
All carriers implement a method "Mage_Shipping_Model_Carrier_Abstract::collectRates()" to return results.
It is possible to get information about the current quote to modify the returned rates/options from within this method.
That said, if there is a way to do it with an observer, it would probably be cleaner/easier.
Finally I have override shipping method, its a two step code but you can reduce it to one step if you wish. Here is my two step solution.
Before we start our shipping class needs to extend and implement
extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
Now, We create a protected method
protected function _createMethod($request, $method_code, $title, $price, $cost)
{
$method = Mage::getModel('shipping/rate_result_method');
$method->setCarrier('australiapost'); // in my case its australia post, it can be any other whatever you are using
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod($method_code);
$method->setMethodTitle($title);
$method->setPrice($this->getFinalPriceWithHandlingFee($price));
$method->setCost($cost);
return $method;
}
Now just use code below to create new method with free shipping and bypass existing shipping calculator, this code will go inside collectRates(Mage_Shipping_Model_Rate_Request $request) method
// PROCESS WILL RETURN FREE SHIPPING
if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes()) {
$shippingPrice = '0.00';
}
$shipping_method = 'Free Shipping';
$method = $this->_createMethod($request, $shipping_method, 'Shipping Disabled', '0.00', '0.00');
$result->append($method);
return $result;
By Doing this, you can get result like below in checkout and user can easily click continue to next step.
Shipping Disabled $0.00

Add checkbox to Magento checkout?

I've been searching, trying, and failing alot here. What I want is to add a checkbox, on the Shipping page of Onepage checkout, the standard magento checkout.
I want to create a checkbox so that customers can check it if they want their products to be placed on their address without a signature.
I've been messing around with some old "Accept Terms" checkboxes, but with no luck.
I'm hoping that somebody might have had to make the same kind of customization.
What you can do is save the preference in the checkout/session via an observer. First, add the checkbox to the shipping section and give it the property of name=shipping[no_signature]. Then, create a new module and hook into the event controller_action_postdispatch_checkout_onepage_saveShipping and then use this code:
public function controller_action_postdispatch_checkout_onepage_saveShipping($observer)
{
$params = (Mage::app()->getRequest()->getParams()) ? Mage::app()->getRequest()->getParams() : array();
if (isset($params['shipping']['no_signature']) && $params['shipping']['no_signature']) {
Mage::getSingleton('checkout/session')->setNoSignature(true);
} else {
Mage::getSingleton('checkout/session')->setNoSignature(false);
}
return $this;
}
Then, when the order is about to be placed, hook into the event sales_order_place_before you can add a comment to the order like this:
public function sales_order_place_before($observer)
{
$order = $observer->getOrder();
if (Mage::getSingleton('checkout/session')->getNoSignature()) {
$order->setCustomerNote('No signature required.');
} else {
$order->setCustomerNote(null);
}
return $this;
}
When you go to Sales > Orders, you should see a comment on the order regarding if the customer requires a signature or not. This is under the assumption that no other module or custom code is injecting anything into the customer_note field on the order object.

Magento - Indicate on credit memo whether items were returned to stock

I need to customize the Credit Memo page to store whether or not an item was returned to stock.
I've identified the Observer in:
app\code\core\Mage\CatalogInventory\Model\Observer.php
refundOrderInventory()
which gets triggered when the admin submits the credit memo with the 'Return to Stock' checkbox ticked. So I know I can add my own observer to write/save something.
But I cant figure out how to add an extra attribute to the Credit Memo product items.
Could anyone give me some pointers?
UPDATE:
I can also add the extra Returned to Stock table cell by editing:
app\design\adminhtml\default\default\template\sales\order\creditmemo\view\items.phtml
and
app\design\adminhtml\default\default\template\sales\order\creditmemo\view\items\renderer\default.phtml
To give me this:
I've hardcoded the "YES" value you see in there. I need to find some way of making this a writeable/readable credit-memo product attribute.
You will want to add the attribute and column to your creditmemo item entity, in an install script. Make sure to have your setup class to be Mage_Eav_Model_Entity_Setup as there is no addAttribute() function in Mage_Core_Model_Resource_Setup.
$installer->addAttribute('creditmemo_item', 'returned_to_stock', array('type' => 'int', 'grid' => true, 'source' => 'adminhtml/system_config_source_yesno'));
$installer->getConnection()->addColumn($installer->getTable('sales/creditmemo_item'), 'returned_to_stock', 'TINYINT(1) unsigned DEFAULT 0');
Then, in your observer (please don't modify the observer you listed there), set the value to be true, like so (I just copied the function you listed, and modified it slightly to demonstrate my point):
public function refundOrderInventory($observer)
{
$creditmemo = $observer->getEvent()->getCreditmemo();
$items = array();
foreach ($creditmemo->getAllItems() as $item) {
$return = false;
if ($item->hasBackToStock()) {
if ($item->getBackToStock() && $item->getQty()) {
$return = true;
}
} elseif (Mage::helper('cataloginventory')->isAutoReturnEnabled()) {
$return = true;
}
if ($return) {
$item->setReturnedToStock(1);
}
}
}

How to unobtrusively add new Validation methods to Magento checkout?

I want to prevent customers entering PO Boxes into the shipping address for selected Shipping Methods (UPS specifically in this case). I could override js/prototype/validation.js to insert a new validation pattern, but I don't want to fork such a key file.
Is there a mechanism to unobtrusively validate the customer's shipping address AFTER they select a shipping method via Javascript without overriding core files?
I see that Validation.add is used inside the validation.js, so it may be possible to add a new validation method outside of the core file?
The regex that I want to apply is:
\b([P|p](OST|ost)?\.?\s?[O|o|0](ffice|FFICE)?\.?\s)?([B|b][O|o|0][X|x])\s(\d+)
If the validation cannot be performed elegantly in the JS, I would be interested in an Observer on the controller_action_predispatch_onepage_saveShippingMethod that inspects the data and performs an Ajax redirect back to the shipping address form if necessary.
The library used is Really Easy Field Validation and that page does explain how to extend it. I guess you will need something like this:
Validation.add('address', 'Error message text', {
pattern : /\b([P|p](OST|ost)?\.?\s?[O|o|0](ffice|FFICE)?\.?\s)?([B|b][O|o|0][X|x])\s(\d+)/
});
As a brief look into it without debugging the checkout
# Unfortunately Magento 1.3.2.3 - Find real postcode from debugging checkout
public function saveShippingAction()
{
$this->_expireAjax();
if ($this->getRequest()->isPost()) {
$data = $this->getRequest()->getPost('shipping', array());
$customerAddressId = $this->getRequest()->getPost('shipping_address_id', false);
$result = $this->getOnepage()->saveShipping($data, $customerAddressId);
$preg_search = '\b([P|p](OST|ost)?\.?\s?[O|o|0](ffice|FFICE)?\.?\s)?([B|b][O|o|0][X|x])\s(\d+)';
$address = $this->getQuote()->getShippingAddress(); #find real postcode
if(preg_match($preg_search, $address['postcode']){
$result = array(
'error' => 1,
'message' => Mage::helper('checkout')->__('Invalid PO Box postcode');
);
}
else{
if (!isset($result['error'])) {
$result['goto_section'] = 'shipping_method';
$result['update_section'] = array(
'name' => 'shipping-method',
'html' => $this->_getShippingMethodsHtml()
);
}
}
$this->getResponse()->setBody(Zend_Json::encode($result));
}
}

Resources