magento 修改paypal传递产品name参数

来源:互联网 发布:世界图书出版社 知乎 编辑:程序博客网 时间:2024/04/30 15:16

    magento 整合了paypal方便了很多人,但是对一些paypal的用户有特殊要求的用户就会觉得修改起来非常麻烦,有朋友问我,如何修改magento传递到paypal 的产品的名称为自定义想要的参数呢?

    通过追查源码,发现了app\code\core\Mage\Paypal\Model下的cart.php有这么一个方法

    /**     * Add a line item     *     * @param string $name     * @param numeric $qty     * @param float $amount     * @param string $identifier     * @return Varien_Object     */    public function addItem($name, $qty, $amount, $identifier = null)    {        $this->_shouldRender = true;        $item = new Varien_Object(array(            'name'   => $name,            'qty'    => $qty,            'amount' => (float)$amount,        ));        if ($identifier) {            $item->setData('id', $identifier);        }        $this->_items[] = $item;        return $item;    }

通过注释我们也能很快的理解 他接受传递过来的 name qty amont等参数。这个就和cart传递的参数很像了,往后去看看 他在什么时候去使用这个方法

    /**     * Add a usual line item with amount and qty     *     * @param Varien_Object $salesItem     * @return Varien_Object     */    protected function _addRegularItem(Varien_Object $salesItem)    {        if ($this->_salesEntity instanceof Mage_Sales_Model_Order) {            $qty = (int) $salesItem->getQtyOrdered();            $amount = (float) $salesItem->getBasePrice();            // TODO: nominal item for order        } else {            $qty = (int) $salesItem->getTotalQty();            $amount = $salesItem->isNominal() ? 0 : (float) $salesItem->getBaseCalculationPrice();        }        // workaround in case if item subtotal precision is not compatible with PayPal (.2)        $subAggregatedLabel = '';        if ($amount - round($amount, 2)) {            $amount = $amount * $qty;            $subAggregatedLabel = ' x' . $qty;            $qty = 1;        }        // aggregate item price if item qty * price does not match row total        if (($amount * $qty) != $salesItem->getBaseRowTotal()) {            $amount = (float) $salesItem->getBaseRowTotal();            $subAggregatedLabel = ' x' . $qty;            $qty = 1;        }        
return $this->addItem($salesItem->getName() . $subAggregatedLabel, $qty, $amount, $salesItem->getSku());

}

在这儿,我们能看到magento对购物车中产品信息进行处理,最后通过addItem()这个方法去添加到paypal的参数列表中去

所以,如果我们想要修改magento想paypal提交的产品名称,只需要修改 addItem()这个方法传递的参数

例如,我修改传递过去的产品名称为产品的ID,只需要在return的时候,修改name为id

return $this->addItem($salesItem->getId() . $subAggregatedLabel, $qty, $amount, $salesItem->getSku());
同理,修改为sku

return $this->addItem($salesItem->getSku() . $subAggregatedLabel, $qty, $amount, $salesItem->getSku());
magento面向对象的编程方式,这儿只要$salesItem里面有的属性,都可以通过魔术方法get来获取当做传递到paypal的参数。

稍微复杂点的,可以自己去按照自己的需求来写业务逻辑。


原创粉丝点击