Пример #1
0
 /**
  * Convert quote address model to order
  *
  * @param   Mage_Sales_Model_Quote $quote
  * @return  Mage_Sales_Model_Order
  */
 public function addressToOrder(Mage_Sales_Model_Quote_Address $address, $order = null)
 {
     if (!$order instanceof Mage_Sales_Model_Order) {
         $order = $this->toOrder($address->getQuote());
     }
     $order->setWeight($address->getWeight())->setShippingMethod($address->getShippingMethod())->setShippingDescription($address->getShippingDescription())->setShippingRate($address->getShippingRate())->setSubtotal($address->getSubtotal())->setTaxAmount($address->getTaxAmount())->setDiscountAmount($address->getDiscountAmount())->setShippingAmount($address->getShippingAmount())->setGiftcertAmount($address->getGiftcertAmount())->setCustbalanceAmount($address->getCustbalanceAmount())->setGrandTotal($address->getGrandTotal())->setBaseSubtotal($address->getBaseSubtotal())->setBaseTaxAmount($address->getBaseTaxAmount())->setBaseDiscountAmount($address->getBaseDiscountAmount())->setBaseShippingAmount($address->getBaseShippingAmount())->setBaseGiftcertAmount($address->getBaseGiftcertAmount())->setBaseCustbalanceAmount($address->getBaseCustbalanceAmount())->setBaseGrandTotal($address->getBaseGrandTotal());
     Mage::dispatchEvent('sales_convert_quote_address_to_order', array('address' => $address, 'order' => $order));
     return $order;
 }
Пример #2
0
 public function collect(Mage_Sales_Model_Quote_Address $address)
 {
     $oldWeight = $address->getWeight();
     $address->setWeight(0);
     $address->setShippingAmount(0);
     $method = $address->getShippingMethod();
     $freeAddress = $address->getFreeShipping();
     $address->setFreeMethodWeight(0);
     foreach ($address->getAllItems() as $item) {
         $item->calcRowWeight();
         $address->setWeight($address->getWeight() + $item->getRowWeight());
         if ($freeAddress || $item->getFreeShipping() === true) {
             $item->setRowWeight(0);
         } elseif (is_numeric($item->getFreeShipping())) {
             $origQty = $item->getQty();
             if ($origQty > $item->getFreeShipping()) {
                 $item->setQty($origQty - $item->getFreeShipping());
                 $item->calcRowWeight();
                 $item->setQty($origQty);
             } else {
                 $item->setRowWeight(0);
             }
         }
         $address->setFreeMethodWeight($address->getFreeMethodWeight() + $item->getRowWeight());
     }
     $address->collectShippingRates();
     $address->setShippingAmount(0);
     $address->setBaseShippingAmount(0);
     $method = $address->getShippingMethod();
     if ($method) {
         foreach ($address->getAllShippingRates() as $rate) {
             if ($rate->getCode() == $method) {
                 $amountPrice = $address->getQuote()->getStore()->convertPrice($rate->getPrice(), false);
                 $address->setShippingAmount($amountPrice);
                 $address->setBaseShippingAmount($rate->getPrice());
                 $address->setShippingDescription($rate->getCarrierTitle() . ' - ' . $rate->getMethodDescription());
                 break;
             }
         }
     }
     $address->setGrandTotal($address->getGrandTotal() + $address->getShippingAmount());
     $address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getBaseShippingAmount());
     return $this;
 }
Пример #3
0
 /**
  * Collect totals information about shipping
  *
  * @param   Mage_Sales_Model_Quote_Address $address
  * @return  Mage_Sales_Model_Quote_Address_Total_Shipping
  */
 public function collect(Mage_Sales_Model_Quote_Address $address)
 {
     $_code = 'customshippingrate';
     $method = $address->getShippingMethod();
     if ($method == $_code) {
         $amountPrice = $address->getQuote()->getStore()->convertPrice($address->getBaseShippingAmount(), false);
         if (Mage::helper('customshippingrate')->isMage13()) {
             $address->setShippingAmount($amountPrice);
             $address->setBaseShippingAmount($address->getBaseShippingAmount());
             $address->setGrandTotal($address->getGrandTotal() + $address->getShippingAmount());
             $address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getBaseShippingAmount());
         } else {
             $this->_setAddress($address);
             $this->_setAmount($amountPrice);
             $this->_setBaseAmount($address->getBaseShippingAmount());
         }
         return $this;
     } else {
         return parent::collect($address);
     }
 }
Пример #4
0
 /**
  * Attempt to collect address shipping rates and return them for further usage in instant update API
  * Returns empty array if it was impossible to obtain any shipping rate
  * If there are shipping rates obtained, the method must return one of them as default.
  *
  * @param Mage_Sales_Model_Quote_Address $address
  * @param bool $mayReturnEmpty
  * @return array|false
  */
 protected function _prepareShippingOptions(Mage_Sales_Model_Quote_Address $address, $mayReturnEmpty = false, $calculateTax = false)
 {
     $options = array();
     $i = 0;
     $iMin = false;
     $min = false;
     $userSelectedOption = null;
     foreach ($address->getGroupedAllShippingRates() as $group) {
         foreach ($group as $rate) {
             $amount = (double) $rate->getPrice();
             if ($rate->getErrorMessage()) {
                 continue;
             }
             $isDefault = $address->getShippingMethod() === $rate->getCode();
             $amountExclTax = Mage::helper('tax')->getShippingPrice($amount, false, $address);
             $amountInclTax = Mage::helper('tax')->getShippingPrice($amount, true, $address);
             $options[$i] = new Varien_Object(array('is_default' => $isDefault, 'name' => trim("{$rate->getCarrierTitle()} - {$rate->getMethodTitle()}", ' -'), 'code' => $rate->getCode(), 'amount' => $amountExclTax));
             if ($calculateTax) {
                 $options[$i]->setTaxAmount($amountInclTax - $amountExclTax + $address->getTaxAmount() - $address->getShippingTaxAmount());
             }
             if ($isDefault) {
                 $userSelectedOption = $options[$i];
             }
             if (false === $min || $amountInclTax < $min) {
                 $min = $amountInclTax;
                 $iMin = $i;
             }
             $i++;
         }
     }
     if ($mayReturnEmpty && is_null($userSelectedOption)) {
         $options[] = new Varien_Object(array('is_default' => true, 'name' => Mage::helper('paypal')->__('N/A'), 'code' => 'no_rate', 'amount' => 0.0));
         if ($calculateTax) {
             $options[$i]->setTaxAmount($address->getTaxAmount());
         }
     } elseif (is_null($userSelectedOption) && isset($options[$iMin])) {
         $options[$iMin]->setIsDefault(true);
     }
     // Magento will transfer only first 10 cheapest shipping options if there are more than 10 available.
     if (count($options) > 10) {
         usort($options, array(get_class($this), 'cmpShippingOptions'));
         array_splice($options, 10);
         // User selected option will be always included in options list
         if (!is_null($userSelectedOption) && !in_array($userSelectedOption, $options)) {
             $options[9] = $userSelectedOption;
         }
     }
     return $options;
 }
Пример #5
0
 /**
  * Attempt to collect address shipping rates and return them for further usage in instant update API
  * Returns empty array if it was impossible to obtain any shipping rate
  * If there are shipping rates obtained, the method must return one of them as default.
  *
  * @param Mage_Sales_Model_Quote_Address $address
  * @param bool $mayReturnEmpty
  * @return array|false
  */
 protected function _prepareShippingOptions(Mage_Sales_Model_Quote_Address $address, $mayReturnEmpty = false)
 {
     $options = array();
     $i = 0;
     $iMin = false;
     $min = false;
     $iDefault = false;
     foreach ($address->getGroupedAllShippingRates() as $group) {
         foreach ($group as $rate) {
             $amount = (double) $rate->getPrice();
             if (!$rate->getMethodTitle() || 0.0 == $amount) {
                 continue;
             }
             $isDefault = $address->getShippingMethod() === $rate->getCode();
             if ($isDefault) {
                 $iDefault = $i;
             }
             $options[$i] = new Varien_Object(array('is_default' => $isDefault, 'name' => "{$rate->getCarrierTitle()} - {$rate->getMethodTitle()}", 'code' => $rate->getCode(), 'amount' => $amount));
             if (false === $min || $amount < $min) {
                 $min = $amount;
                 $iMin = $i;
             }
             $i++;
         }
     }
     if ($mayReturnEmpty) {
         $options[] = new Varien_Object(array('is_default' => false === $iDefault ? true : false, 'name' => 'N/A', 'code' => 'no_rate', 'amount' => 0.0));
     } elseif (false === $iDefault && isset($options[$iMin])) {
         $options[$iMin]->setIsDefault(true);
     }
     return $options;
 }
 public static function canApply(Mage_Sales_Model_Quote_Address $address)
 {
     $helper = Mage::helper('dccharge');
     //		$deliveryType = Mage::getStoreConfig('dc_charge_extension/dccharge/delivery-type');
     $countryFrom = Mage::getStoreConfig('shipping/origin/country_id') ? Mage::getStoreConfig('shipping/origin/country_id') : Mage::getStoreConfig('general/country/default');
     if ($countryFrom && $address->getCountryId() != $countryFrom && $helper->canUseForCountry($address->getCountryId()) && $address->getShippingMethod() && $helper->canUseForShippingMethod($address->getShippingMethod())) {
         return true;
     }
     return false;
 }
 public function collect(Mage_Sales_Model_Quote_Address $address)
 {
     $oldWeight = $address->getWeight();
     $address->setWeight(0);
     $address->setShippingAmount(0);
     $address->setBaseShippingAmount(0);
     $address->setFreeMethodWeight(0);
     $items = $address->getAllItems();
     if (!count($items)) {
         return $this;
     }
     $method = $address->getShippingMethod();
     $freeAddress = $address->getFreeShipping();
     $addressWeight = $address->getWeight();
     $freeMethodWeight = $address->getFreeMethodWeight();
     $addressQty = 0;
     foreach ($items as $item) {
         /**
          * Skip if this item is virtual
          */
         if ($item->getProduct()->isVirtual()) {
             continue;
         }
         /**
          * Children weight we calculate for parent
          */
         if ($item->getParentItem()) {
             continue;
         }
         if ($item->getHasChildren() && $item->isShipSeparately()) {
             foreach ($item->getChildren() as $child) {
                 if ($child->getProduct()->isVirtual()) {
                     continue;
                 }
                 $addressQty += $item->getQty() * $child->getQty();
                 if (!$item->getProduct()->getWeightType()) {
                     $itemWeight = $child->getWeight();
                     $itemQty = $item->getQty() * $child->getQty();
                     $rowWeight = $itemWeight * $itemQty;
                     $addressWeight += $rowWeight;
                     if ($freeAddress || $child->getFreeShipping() === true) {
                         $rowWeight = 0;
                     } elseif (is_numeric($child->getFreeShipping())) {
                         $freeQty = $child->getFreeShipping();
                         if ($itemQty > $freeQty) {
                             $rowWeight = $itemWeight * ($itemQty - $freeQty);
                         } else {
                             $rowWeight = 0;
                         }
                     }
                     $freeMethodWeight += $rowWeight;
                     $item->setRowWeight($rowWeight);
                 }
             }
             if ($item->getProduct()->getWeightType()) {
                 $itemWeight = $item->getWeight();
                 $rowWeight = $itemWeight * $item->getQty();
                 $addressWeight += $rowWeight;
                 if ($freeAddress || $item->getFreeShipping() === true) {
                     $rowWeight = 0;
                 } elseif (is_numeric($item->getFreeShipping())) {
                     $freeQty = $item->getFreeShipping();
                     if ($item->getQty() > $freeQty) {
                         $rowWeight = $itemWeight * ($item->getQty() - $freeQty);
                     } else {
                         $rowWeight = 0;
                     }
                 }
                 $freeMethodWeight += $rowWeight;
                 $item->setRowWeight($rowWeight);
             }
         } else {
             if (!$item->getProduct()->isVirtual()) {
                 $addressQty += $item->getQty();
             }
             $itemWeight = $item->getWeight();
             $rowWeight = $itemWeight * $item->getQty();
             $addressWeight += $rowWeight;
             if ($freeAddress || $item->getFreeShipping() === true) {
                 $rowWeight = 0;
             } elseif (is_numeric($item->getFreeShipping())) {
                 $freeQty = $item->getFreeShipping();
                 if ($item->getQty() > $freeQty) {
                     $rowWeight = $itemWeight * ($item->getQty() - $freeQty);
                 } else {
                     $rowWeight = 0;
                 }
             }
             $freeMethodWeight += $rowWeight;
             $item->setRowWeight($rowWeight);
         }
     }
     if (isset($addressQty)) {
         $address->setItemQty($addressQty);
     }
     $address->setWeight($addressWeight);
     $address->setFreeMethodWeight($freeMethodWeight);
     $address->collectShippingRates();
     $address->setShippingAmount(0);
     $address->setBaseShippingAmount(0);
     $method = $address->getShippingMethod();
     if ($method) {
         foreach ($address->getAllShippingRates() as $rate) {
             if ($rate->getCode() == $method) {
                 $amountPrice = $address->getQuote()->getStore()->convertPrice($rate->getPrice(), false);
                 $address->setShippingAmount($amountPrice);
                 $address->setBaseShippingAmount($rate->getPrice());
                 $address->setShippingDescription($rate->getCarrierTitle() . ' - ' . $rate->getMethodTitle());
                 break;
             }
         }
     }
     $address->setGrandTotal($address->getGrandTotal() + $address->getShippingAmount());
     $address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getBaseShippingAmount());
     return $this;
 }