【问题标题】:Magento Changing Default Payment MethodMagento 更改默认付款方式
【发布时间】:2016-05-12 00:25:09
【问题描述】:

我有一个函数可以在 Magento 中对我们的数据库运行原始 SQL 查询。该函数所做的是将客户的默认信用卡更改为传递给该函数的值。我的问题是如何使用 Magento 模型重写函数。当前函数可以工作,但我们宁愿它不直接与 SQL 接口。

函数如下:

public function setDefaultPayment($value)
{
    $customerId = $this->_getSession()->getCustomer()->getId();
    $write = Mage::getSingleton('core/resource')->getConnection('core_write');

    $read = $write->query("SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code='customer'");
    $row = $read->fetch();
    $entity_type_id = $row['entity_type_id'];

    $read = $write->query("SELECT attribute_id FROM eav_attribute WHERE attribute_code='default_payment' AND entity_type_id = $entity_type_id");
    $row = $read->fetch();
    $attribute_id = $row['attribute_id'];

    $read = $write->query("SELECT * FROM customer_entity_int WHERE entity_type_id='$entity_type_id' AND attribute_id='$attribute_id' AND entity_id='$customerId'");
    if ($row = $read->fetch()) {
        $write->update(
            'customer_entity_int',
            array('value' => $value),
            "entity_type_id='$entity_type_id' AND attribute_id='$attribute_id' AND entity_id='$customerId'"
        );
    } else {
        $write->insert(
            'customer_entity_int',
            array(
                'entity_type_id' => $entity_type_id,
                'attribute_id' => $attribute_id,
                'entity_id' => $customerId,
                'value' => $value
            )
        );
    }
}

【问题讨论】:

  • 您的自定义类扩展了什么原生 Magento 类?
  • 属性“default_payment”从何而来?一个扩展?你是怎么创造的? - 通常信用卡数据不会存储在 Magento 中,因为 99% 运行 Magento 的商家不符合 PCI DSS (en.wikipedia.org/wiki/…)
  • @codedge,我没有创建这个。我最近在一家使用完全非标准 Magento 实践的公司接受了一份工作,甚至更换了 Magento 本身的部分核心。换句话说,我们被锁定到特定版本的 magento (1.9)。我们正在修改网站并使其成为标准。

标签: magento magento-1.9


【解决方案1】:

如果我没看错你的代码,你想用给定的值更新客户属性default_payment

为此,您需要:

  • 按 id 加载客户
  • 为客户属性default_payment设置新值
  • 拯救客户
public function setDefaultPayment($value)
{
    $customerId = $this->_getSession()->getCustomer()->getId();
    $write = Mage::getSingleton('core/resource')->getConnection('core_write');

    $customer = Mage::getModel('customer/customer')->load($customerId);
    $oldValue = $customer->getDefaultPayment(); // optional, just for checking
    $customer->setDefaultPayment($value);
    $customer->save();

}

【讨论】:

  • 这正是我想要的。这让我对如何修复我们在 SQL 中拥有的其他一些函数有了一些了解。谢谢!
猜你喜欢
  • 2012-03-01
  • 2016-01-05
  • 2016-11-05
  • 2015-10-10
  • 2015-04-03
  • 1970-01-01
  • 2021-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多