2016-07-21 6 views
0

ステータスを監視し、値をデータベースに書き込むモジュールを持っています。 製品がシンプルであれば動作します。しかし、設定可能な場合は、子ではなくパターンプロダクトの値が書き込まれます。子製品からデータを取得

if (($status !== $previousStatus) && ($status == Mage_Sales_Model_Order::STATE_PROCESSING)) { 
    $order = $observer->getEvent()->getOrder(); 
    $items = $order->getAllVisibleItems(); 
    mysql_connect('localhost', 'user', 'pass'); 
    foreach($items as $item){ 
    $cd = $item->getProduct()->getData('cd'); 
    mysql_query("INSERT INTO `bd`.`table` (`id`, `cd`) VALUES (NULL, '$cd')"); 
    } 
    return true; 
} 

構成可能な製品であるかどうかを識別し、その子製品からデータを取得する方法を教えてください。あなたのSQL接続を無視

答えて

1
  1. Magentoのは、すでに初期化接続を持っている、あなたが直接、別のものをスピンアップする必要はありません。
  2. 接続するデータベースを直接指定する必要はありません。
  3. 構成可能な製品にはシンプルな製品を含めることができます。これにアクセスするには、childItemを取得する必要があります。
  4. このようなことのための好ましいアプローチは、注文アイテムに対して商品データを保存することです。これにより、注文アイテムが時間通りに商品データから切り離されるため、配置の。 、ちょうどFYIしかしgetAllVisibleItems返す構成可能https://magento.stackexchange.com/questions/62111/what-is-the-effect-of-deleting-a-product-with-associated-orders/62142#62142
  5. を、あなたはすべてのコレクションで構成可能とそれらの最も単純なを見たい場合は、forループ内代わり

    if (($status !== $previousStatus) && ($status == Mage_Sales_Model_Order::STATE_PROCESSING)) { 
        /** @var Mage_Sales_Model_Order $order */ 
        $order = $observer->getData('order'); 
        $items = $order->getAllVisibleItems(); 
    
        /** @var Mage_Sales_Model_Order_Item $item */ 
        foreach($items as $item){ 
         $cd = $item->getProduct()->getData('cd'); 
    
         //If configurable get the first child item and the cd value from it 
         if ($item->getProductType() === Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) { 
          $childrenItems = $item->getChildrenItems(); 
          $childItem = reset($childrenItems); 
          if ($childItem instanceof Mage_Sales_Model_Order_Item) { 
           $cd = $childItem->getProduct()->getData('cd'); 
          } 
         } 
    
         /** @var Mage_Core_Model_Resource $resource */ 
         $resource = Mage::getSingleton('core/resource'); 
         $writeConnection = $resource->getConnection('core_write'); 
    
         $query = "INSERT INTO `table` (`cd`) VALUES ('{$cd}')"; 
         $writeConnection->query($query); 
        } 
        return true; 
    } 
    
+0

完全に作業しました:)ステータスがPENDINGの場合、$ cdの値は取られません。それを修正するには? – user4817258

+0

上部の初期条件は、異なる注文状態を除外します。そこで条件を変更してみてください。 '&&(in_array($ status、array(Mage_Sales_Model_Order :: STATE_PROCESSING、Mage_Sales_Model_Order :: STATE_PENDING_PAYMENT))' –

0

- あなたはそれを修正する必要があります - ここを見て:http://fishpig.co.uk/magento/tutorials/direct-sql-queries/

を設定可能な製品の選択された子製品を取得するには、これを行います。

foreach($items as $item){  

    // Establish product type and if configurable, load the chosen product 
    if ($item->getProductType() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) { 
     $simple = reset($item->getChildren()); 
     $_product = $simple->getProduct(); 
    } else { 
     $_product = $item->getProduct(); 
    } 

    $cd = $_product->getData('cd'); 
    mysql_query("INSERT INTO `bd`.`table` (`id`, `cd`) VALUES (NULL, '$cd')"); 

} 
+0

ロードgetAllItemsというメソッドを使用することができます:ここに続きを読みますパフォーマンスには非常に悪い。必要なデータは 'item-> getProduct()'によって読み込まれ、遅延ロードされ、オブジェクトに対してキャッシュされます。 'Mage_Sales_Model_Order_Item :: getProduct'を参照してください –

+0

これは通常悪い考えであることに同意します。これは、必要なデータが完全な負荷を必要とする$ item-> getProduct()オブジェクト内にないところで使用したスニペットです。これに注目すると、これは注文で呼び出されているので、そこに何百ものアイテムがある可能性はかなりスリムです。 – PixieMedia

+0

それにもかかわらず改訂されました:) – PixieMedia

関連する問題