从Magento的订单中获取产品

时间:2013-08-22 08:38:47

标签: php magento

在我的magento项目中,在我的帐户>下我的订单(登录客户),我能够查看订单详细信息以及我订购的产品。现在,对于每个订购的产品,我想检索一个特定的属性,但是根据我的理解,sales/order/items/renderer/default.phtml开头的代码片段$_item = $this->getItem();是订单本身,所以如果我使用$_item->getId()之类的东西,我得到订单ID而不是产品。

我尝试过研究并最终得到了这段代码:

$orders = Mage::getModel('sales/order')->load($_item->getId());
foreach($orders as $order):
    $is = $order->getAllItems();
    foreach($is as $i):
        echo $i->getProductId();
    endforeach;
endforeach;

希望我可以使用产品ID来获取所述产品的其他属性,但是我在这段代码中收到错误而无法确定错误是什么。我也试过这样的事情:

 $_productCollection = Mage::getResourceModel('reports/product_collection')
                        ->addAttributeToSelect('*')
                        ->addAttributeToFilter('name', $name);

                    foreach($_productCollection as $_product):
                        $_temp = $_product->getResource()->getAttribute('name_en')->getFrontend()->getValue($_product);
                    endforeach;

但是当我尝试检查产品系列中的项目数时,我一直变为0。如何在此页面中检索产品的自定义属性?

3 个答案:

答案 0 :(得分:24)

这应该更好:

   $orderIncrementId = '100000010';
   $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
   $items = $order->getAllVisibleItems();
   foreach($items as $i):
      echo $i->getProductId();
   endforeach;

请点击此处了解详情:http://www.magentocommerce.com/boards/viewthread/18629/

答案 1 :(得分:18)

虽然Electric Jesus给出的答案可行,但它包含了在循环中加载产品的潜在性能问题。

正确的实施方法是首先获取已订购产品的ID,然后立即加载所有产品。假设您已经加载了订单:

$orderedItems = $order->getAllVisibleItems();
$orderedProductIds = [];

foreach ($orderedItems as $item) {
    $orderedProductIds[] = $item->getData('product_id');
}

$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection->addAttributeToSelect('*');
$productCollection->addIdFilter($orderedProductIds);

答案 2 :(得分:1)

public class MainActivity extends AppCompatActivity implements View.OnClickListener,Runnable {
Button b1, b2, b3, b4;
Random r = new Random();
int random_selection;
Thread t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1 = (Button) findViewById(R.id.button);
    b2 = (Button) findViewById(R.id.button2);
    b3 = (Button) findViewById(R.id.button3);
    b4 = (Button) findViewById(R.id.button4);
    b1.setBackgroundColor(Color.BLUE);
    b2.setBackgroundColor(Color.BLUE);
    b3.setBackgroundColor(Color.BLUE);
    b4.setBackgroundColor(Color.BLUE);
    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
    b3.setOnClickListener(this);
    b4.setOnClickListener(this);
    delay(7000);

    main();


}

private void main() {
    // Thread t1 = new Thread(new MainActivity());
    // this wi run() function
    t1 = new Thread(new MainActivity());
    System.out.println("inside main");
    t1.start();
    System.out.println("after t1.start()");

}

@Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.button:

            break;
        case R.id.button2:

            break;
        case R.id.button3:

            break;
        case R.id.button4:

            break;


        default:
            android.widget.Toast.makeText(getBaseContext(), "Wrong Selection",
                    android.widget.Toast.LENGTH_SHORT).show();
            break;
    }

}


public int randomgen() {

    random_selection = r.nextInt(4) + 1;
    return random_selection;
}

public void delay(int t) {
    try {
        Thread.sleep(t);
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
}


//@Override•
public void run() {
    try {
        System.out.println("inside run");
        int x, count = 0;
        for (; ; ) {

            count = count + 1;
            x = randomgen();
            //     android.widget.Toast.makeText(getBaseContext(), x,android.widget.Toast.LENGTH_SHORT).show();
            if (x == 1) {
              b1.setBackgroundColor(Color.BLACK);
                delay(5000);
              b1.setBackgroundResource(android.R.drawable.btn_default);
                System.out.println(this.b1);
            } else if (x == 2) {
              b2.setBackgroundColor(Color.BLACK);
                delay(5000);
              b2.setBackgroundResource(android.R.drawable.btn_default);
                System.out.println(this.b2);
            } else if (x == 3) {
              b3.setBackgroundColor(Color.BLACK);
                delay(5000);
              b3.setBackgroundResource(android.R.drawable.btn_default);
                System.out.println(this.b3);
            } else if (x == 4) {
              b4.setBackgroundColor(Color.BLACK);
                delay(5000);
              b4.setBackgroundResource(android.R.drawable.btn_default);
                System.out.println(this.b4);

            }

            if (count == 4) {
                break;
            }
        }

    } catch (Exception e) {
        System.out.println("inside catch");
        e.printStackTrace();
    }
}

我有同样的问题,这是我的解决方案