2016-05-12 10 views
2

Magentoのデータベースに生のSQLクエリを実行する機能があります。この関数は、顧客のデフォルトクレジットカードを関数に渡される値に変更します。私の質問は、どのように私はMagentoモデルを利用して機能を書き換えるだろうかです。現在の関数は機能しますが、SQLとの直接的なインタフェースを持たないようにしたいと考えています。私はあなたが正しいコーディング読めば、あなたが与えられた値を持つ顧客属性default_paymentを更新したいMagentoデフォルトの支払方法を変更

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 
      ) 
     ); 
    } 
} 
+0

何ネイティブMagentoのための新しい値を設定し、ID

  • によって顧客:あなたがする必要があることについては

    クラスはあなたのカスタムクラスですか? –

  • +0

    属性 'default_payment'はどこから来たのですか?拡張?それはどうやって作りましたか? - Magentoを運営する商人の99%がPCI DSSに準拠していない(https://en.wikipedia.org/wiki/Payment_Card_Industry_Data_Security_Standard) – codedge

    +0

    @codedgeであるため、通常、クレジットカードのデータはMagentoに保存されません。私は最近Magento自体の中核部分を置き換える点まで完全に非標準的なMagentoのプラクティスを使用していた会社で仕事を受け入れました。つまり、特定バージョンのマゼンタ(1.9)にロックされています。私たちは現場を見直し、やや標準的にしています。 – djames

    答えて

    1

    :ここ

    機能です。

    • ロード顧客属性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(); 
    
    } 
    
    +0

    これはまさに私が欲しかったことです。これは私が持っている他のいくつかの関数をどのように修正するかについてのいくつかの洞察をSQLにも与えてくれます。ありがとう! – djames

    関連する問題