开发者

Magento Check the ID of the client if he already bought the product

开发者 https://www.devze.com 2023-04-07 18:44 出处:网络
I have a product that can only be purchased once only for each customer .. If I put in the admin, only one item in the shopping cart, Elee can buy one at a time, and so buy more than one time.

I have a product that can only be purchased once only for each customer .. If I put in the admin, only one item in the shopping cart, Elee can buy one at a time, and so buy more than one time. How do I check if the "ID" of the customer has already bought, so if they bought the message that he has already purchased this product.? I think 开发者_如何学运维you have to do the buy button


Best thing you could do is write your own observer that is called before/after the add_to_cart event. (Read more about that here)

Inside that observer file it's best that you get all the previous orders of that particular customer:

$orderCollection = Mage::getModel('sales/order')->getCollection();
$customer_orders = $orderCollection->getSelect()->where('e.customer_id =CUSTOMER_ID_GOES_HERE'); 

Foreach order of this customer you iterate over all the orderded items, and if one of them matches the product:

$order = Mage::getModel('sales/order')->load($order_id);
$items = $order->getAllItems();
foreach ($items as $itemId => $item)
{
    if($item->getProductId() == ordered_product_id_goes_here){
       //Show output message here that customer can only buy this once
    }
    break;
}

Good luck ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号