0

グリッドカテゴリビューの[カートに追加]ボタンの横に、製品の最小数量を含む[数量]ボックスが必要です。以下のコードを使用してみましたが、フィールドに常に「0」が表示されることを除いて機能します。

フィールドに「0」だけでなく、製品の最小数量が表示されるようにするにはどうすればよいですか。

これは私がlist.phtmlファイルを変更するために使用したものです:

                        <?php if(!$_product->isGrouped()): ?>

                        <label for="qty"><?php echo $this->__('Qty:') ?></label>                                 
                                <input name="qty" type="text" class="input-text qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />

                        <?php endif; ?>
4

1 に答える 1

2

関数 getProductDefaultQty はビュー ブロックでのみ使用でき、リストでは使用できません :(

クラス Mage_Catalog_Block_Product_List を顧客モジュールで書き直して、この関数をモジュールのクラスに含めることができます。

この回答のために、モジュールを Nat_Quantity と呼びます (必要に応じて変更できます)。

ステップ 1: moudle xml を作成する

/app/etc/modules/ の下にファイル Nat_Quantity.xml を作成します。次のようになります (codePool には大文字の P があることに注意してください)。

<?xml version="1.0"?>
<config>
    <modules>
        <Nat_Quantity>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Catalog />
            </depends>
        </Nat_Quantity>
    </modules>
</config>

ステップ 2: モジュールのフォルダー構造を作成する

/app/code/local/ の下に Nat フォルダーを作成し、その下に Quantity フォルダーを作成します。この Quantity フォルダの下に、次の 2 つのフォルダ etc と Block を作成します。(etc は小文字であることに注意してください)

ステップ 3: config.xml を作成する

/app/code/local/Nat/Quantity/etc の下に、次のような config.xml ファイルを作成します。

<?xml version="1.0"?>
<config>
    <modules>
        <Nat_Quantity>
            <version>1.0.0</version>
        </Nat_Quantity>
    </modules>
    <global>
        <blocks>
            <catalog>
                <rewrite>
                    <product_list>Nat_Quantity_Block_Product_List</product_list>
                </rewrite>
            </catalog>
        </blocks>
    </global>
</config>

ステップ 3: ブロックを作成する

/app/code/local/Nat/Quantity/Block/Product の下に、次のような List.php を作成します。

<?php
class Nat_Quantity_Block_Product_List extends Mage_Catalog_Block_Product_List {
    /**
     * Get default qty - either as preconfigured, or as 1.
     * Also restricts it by minimal qty.
     *
     * @param null|Mage_Catalog_Model_Product
     *
     * @return int|float
     */
    public function getProductDefaultQty($product)
    {
        $qty = $this->getMinimalQty($product);
        $config = $product->getPreconfiguredValues();
        $configQty = $config->getQty();
        if ($configQty > $qty) {
            $qty = $configQty;
        }

        return $qty;
    }
}

これにより、リスト テンプレートで $this->getProductDefaultQty($product) を呼び出すことができるようになります。関数に検証製品を渡す必要があります。または、製品 ID を渡して関数に製品をロードすることもできます。

$product = Mage::getModel('catalog/product')->load($productId);
于 2013-02-13T08:13:09.917 に答える