1

我有兴趣修改此对象中的一些受保护值。更具体地说,我想price根据method可能的情况来修改。这个对象是对 UPS 的 XML 运费请求的响应。我遇到的问题是不同方法返回的费率不是我需要的。我不提供具体尺寸(对于任意请求无法提供确切数字),因此 UPS Ground 以外的任何方法都会产生不合适的费率。请注意,一种解决方案是首先在 XML 请求中为其提供一些估计尺寸(我正在以这种方式进行测试),但我也想知道如何使用该对象。

我试图简单地访问这个对象中的一些值,但它似乎受到保护,它们不会打印到浏览器?我试过了$object->_rates[0]$object->{_rates[0]}但他们不打印任何东西。有人可以指出我修改price此对象中的值的正确方向吗?

$object = 
Mage_Shipping_Model_Rate_Result Object
(
    [_rates:protected] => Array
    (
            [0] => Mage_Shipping_Model_Rate_Result_Method Object
            (
                [_data:protected] => Array
                (
                    [carrier] => ups
                    [carrier_title] => UPS
                    [method] => 03
                    [method_title] => UPS Ground
                    [cost] => 8.9
                    [price] => 8.9
                )
            [_hasDataChanges:protected] => 1
            [_origData:protected] => 
            [_idFieldName:protected] => 
            [_isDeleted:protected] => 
            [_oldFieldsMap:protected] => Array
            (
            )
            [_syncFieldsMap:protected] => Array
            (
            )
        )
....
)
4

1 回答 1

3

通常受保护的属性受到保护是有原因的。有一种方法getAllRates()可以让你编辑你想要的。

这意味着你可能会得到这样的东西:

foreach($object->getAllRates() as $rate) {
    $rate->setPrice($rate->getPrice() * 123);
}

Magento 文档中记录了更改价格的方法。

但是,通常可以使用Reflection更改属性/方法的可见性。

例如你可以使用这个:

$object = new Mage_Shipping_Model_Rate_Result();
$rp = new ReflectionProperty($object, '_rates');
$rp->setAccessability(true);

但是,这通常不是您想要的方式!这不是OOP方式。

于 2012-07-11T22:10:49.220 回答