Удалить вариант доставки, если условие выполнено
Я хотел бы удалить стандартный вариант доставки productmatrix_Standard
от способов доставки, если определенное условие выполнено. Я считаю, что мне нужно переопределить следующее:
/**
* One page checkout status
*
* @category Mage
* @category Mage
* @package Mage_Checkout
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Checkout_Block_Onepage_Shipping_Method_Available extends Mage_Checkout_Block_Onepage_Abstract
{
protected $_rates;
protected $_address;
public function getShippingRates()
{
if (empty($this->_rates)) {
$this->getAddress()->collectShippingRates()->save();
$groups = $this->getAddress()->getGroupedAllShippingRates();
/*
if (!empty($groups)) {
$ratesFilter = new Varien_Filter_Object_Grid();
$ratesFilter->addFilter(Mage::app()->getStore()->getPriceFilter(), 'price');
foreach ($groups as $code => $groupItems) {
$groups[$code] = $ratesFilter->filter($groupItems);
}
}
*/
return $this->_rates = $groups;
}
return $this->_rates;
}
}
Как я могу удалить существующие методы доставки из этой коллекции или очистить ее и пересобрать вручную, пропустив productmatrix_Standard
вариант?
2 ответа
Решение
Я закончил с
<?php
public function getShippingRates() {
if (empty($this->_rates)) {
$this->getAddress()->collectShippingRates()->save();
$groups = $this->getAddress()->getGroupedAllShippingRates();
if ($this->someCondition()) {
$badMethods = array('productmatrix_Standard');
// Loop through groups (productmatrix)
// pass by reference for unset later
foreach ($groups as $code => &$groupItems) {
// Lopp through methods (standards, 2day)
foreach($groupItems as $key => $item) {
// Check for bad shipping methods
if (in_array($item->getCode(), $badMethods)) {
// Remove the method from the shipping groups
unset($groupItems[$key]);
}
}
}
}
$this->_rates = $groups;
}
return $this->_rates;
}
Вы должны вместо этого переопределитьfunction collectRates()
"в модели доставки.
например внутри /app/code/core/Mage/Shipping/Model/Carrier/Flatrate.php
или же /app/code/Mage/Usa/Model/Shipping/Carrier/Ups.php
Все детали об адресе и корзине доступны на момент collectRates()
выполняется на каждом носителе. Это делает его идеальным для фильтрации конкретных ставок или изменения их names/price.