我知道我們可以很容易limit the max quantity of a given product a customer can purchase per order,但是可能的(本地或甚至有一個插件)限制每個客戶給定產品的最大數量?Magento:限制產品最大數量每個客戶(不是每個訂單)
我不想use a coupon也不modify the code:它需要是一個銷售價格藉助本地或擴展功能。
Magento的1.5.1
我知道我們可以很容易limit the max quantity of a given product a customer can purchase per order,但是可能的(本地或甚至有一個插件)限制每個客戶給定產品的最大數量?Magento:限制產品最大數量每個客戶(不是每個訂單)
我不想use a coupon也不modify the code:它需要是一個銷售價格藉助本地或擴展功能。
Magento的1.5.1
這是不可能的人,但你可以將執行這些限制的模塊。
您需要創建一個資源模型,該模型將檢索未取消訂單且未退回具有特定產品ID的產品的訂單。其實它只是一個簡單的選擇銷售/訂單和銷售/ order_item表。資源模型的方法可能如下所示:
public function getPurchasedProductQty(array $productIds, $customerId)
{
$select = $this->_getReadAdapter()->select();
$select
->from(array('order_item' => $this->getTable('sales/order_item')),
array(
'qty' => new Zend_Db_Expr('order_item.ordered_qty - order_item.canceled_qty - order_item.refunded_qty'),
'product_id'))
// Joining order to retrieve info about item and filter out canceled or refunded orders
->join(array('order' => $this->getTable('sales/order')),
'order.entity_id = order_item.order_id',
array())
// Limit it to the current customer
->where('order.customer_id = ?', $customerId)
// Filter out refunded and canceled orders
->where('order.state NOT IN(?)', array(
Mage_Sales_Model_Order::STATE_CLOSED,
Mage_Sales_Model_Order::STATE_CANCELED
))
// Add Product Id Condition
->where('order_item.product_id IN(?)', $productIds);
return $this->_getReadAdapter()->fetchCol($select);
}
然後,當你觀察sales_quote_item_collection_products_after_load
事件中,你只可以將您的定製邏輯與檢查上會在車中使用的產品的限制,並刪除那些從加載的集合。你應該自己實現這個邏輯。
假設您嘗試限制產品數量,當前登錄的註冊客戶可以將其添加到購物車中。
(這是一對一的關係,但可以很容易地修改,以適應許多不同的產品和每個客戶數量)
創建一個自定義模塊,將在客戶實體添加一個字段,因此管理員可以設置相應的每個客戶的數量。
字段名稱:[模塊名] _product_id(見Adding attributes to customer entity)
字段名稱:[模塊名] _max_cart_qty(見Adding attributes to customer entity)
在(複製文件到您的本地模板文件夾),並更新數量輸入領域。
/app/design/frontend/base/default/template/catalog/product/view/addtocart.phtml /app/design/frontend/base/default/template/checkout/cart/item/default。PHTML
變化
<input type="text" class="input-text qty" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" />
到(添加驗證類,以確保數量小於或等於)
$addValidationClass = '';
if(Customer is login && ModuleName_product_id == $_product->getId() && [ModuleName]_max_cart_qty > 0){
$addValidationClass = ' validate-digits-range-1-' . [ModuleName]_max_cart_qty
}
<input type="text" class="input-text qty<?php echo $addValidationClass; ?>" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" />
如果你想要做服務器端驗證,然後創建一個觀察者爲了加入購物車活動,比較上述邏輯到目前已添加到購物車
下面的擴展將有助於實現這一點
https://www.magentocommerce.com/magento-connect/maximum-order-quantity.html
編輯 在此擴展給
常常店主需要在購物車頁面,限制使用定製信息訂單的產品數量。這在使用默認管理設置時不可行。但是,通過使用此擴展名,您可以通過自定義錯誤消息來設置產品數量的限制。如果最大數量限制超過限制,那麼錯誤消息將顯示在購物車頁面上。
您可以通過自定義錯誤消息設置每個產品的最大數量。您可以使用後端選項全局啓用/禁用它們。
儘管此鏈接可能回答此問題,但最好在此處包含答案的重要部分並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/10875269) –
我添加了一些描述 –
親愛的@srinath,那個評論是由SO在評論時添加的。 –
您能否告訴我們如何從'sales_quote_item_collection_products_after_load'事件的觀察者處獲取產品的Ids信息。 – Sourabh