【问题标题】:Magento: Limit Product Max Quantity Per Customer (NOT per Order)Magento:限制每个客户的产品最大数量(不是每个订单)
【发布时间】:2012-12-08 13:45:34
【问题描述】:

我知道我们可以轻松limit the max quantity of a given product a customer can purchase per order,但是是否可以(本机或什至使用插件)限制每个客户的给定产品的最大数量 ??

我不想use a coupon 也不想modify the code:它需要是在原生或扩展功能的帮助下的销售价格

Magento 1.5.1

【问题讨论】:

    标签: magento e-commerce magento-1.5


    【解决方案1】:

    不可能是原生的,但您可以制作一个执行此类限制的模块。

    1. 您需要创建一个资源模型,该模型将检索具有特定产品 ID 的产品的未取消且未退款的订单。实际上,它只是对 sales/order 和 sales/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);
      }
      
    2. 然后,当您观察到 sales_quote_item_collection_products_after_load 事件时,您只需放置自定义逻辑,检查将在购物车中使用的产品的限制,然后从加载的集合中删除这些限制。这个逻辑你应该自己实现。

    【讨论】:

    • 你能告诉我如何从sales_quote_item_collection_products_after_load事件的观察者那里获取产品的ID。
    【解决方案2】:

    假设您正在尝试限制当前登录的注册客户可以添加到购物车的产品数量。

    (这是一对一的关系,但可以轻松修改以适应许多不同的产品和每个客户的数量)

    创建一个自定义模块,该模块将在客户实体中添加一个字段,因此管理员可以为每个客户设置适当的数量。

    字段名称:[ModuleName]_product_id(参见Adding attributes to customer entity

    字段名称:[ModuleName]_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') ?>" />
    

    to(添加验证类以确保数量小于或等于)

    $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') ?>" />
    

    如果您想进行服务器端验证,则为添加到购物车事件创建一个观察者,将上述逻辑与当前添加到购物车的商品进行比较

    【讨论】:

      【解决方案3】:

      下面的扩展将有助于实现这一目标

      https://www.magentocommerce.com/magento-connect/maximum-order-quantity.html

      编辑 在这个扩展中给予

      店主经常需要在购物车页面使用自定义消息来限制订购产品的数量。这在使用默认管理员设置时是不可能的。但是,通过使用此扩展程序,您可以使用自定义错误消息设置产品数量限制。如果最大数量限制超过限制,则会在购物车页面显示错误消息。

      您可以使用自定义错误消息设置每个产品的最大数量。您可以使用后端选项全局启用/禁用它们。

      【讨论】:

      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
      • 我添加了一些描述
      • 亲爱的@srinath,SO 在审核时添加了该评论。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 2023-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多