異なる在庫番号の異なるテキストを表示するために、「在庫あり」テキストを変更しました。したがって、3より上の在庫番号の場合、それはsaisである: " 在庫"(cssで緑色)、在庫番号3またはそれ以下では "2だけ残っている"(cssで赤色)と表示されます。アマゾンのように。WooCommerceで製品バリエーションの在庫数量が表示されない
あなたがここに見ることができるので、これは簡単な製品と完璧に動作します:https://edelmix.de/superdry-urban-blau-limette-quarzuhr-syg164un/
をしかし、変数の製品に私のコードは、所望の結果を持っていません。もちろん、バリエーションの適切な在庫量で上記のテキストと同じテキストを表示します。
私が得るのは、すべてのバリエーションの合計数を含むテキストです。私はすべてのバリエーションについてバックエンドの製品レベルで製品数量を管理し、そのバリエーションに対して異なる在庫量を持っていました。今のような可変積上
結果はここで見ることができます: https://stage.edelmix.de/buddha-to-buddha-blue-lace-agate-ring-603ba/
示す量は、(あなたがドロップダウンからバリエーションを選択した後)5です。すべてのバリエーションの総量はすべて一緒になります。異なるリングサイズの在庫数は1/3/1です(私のテストでは、価格の横にある在庫変数が表示されています)、これはそれぞれのバリエーションが選択されたときに表示したいものですシンプルな製品と同じようにカスタムテキスト)。
マイコードは、これまでのところです:
add_filter('woocommerce_get_availability', 'custom_get_availability', 1, 2);
function custom_get_availability($availability, $_product) {
global $woocommerce, $product;
if ($_product->is_type('variable')) {
$available_variations = $product->get_available_variations();
foreach ($available_variations as $variation)
{
$variation_id = $variation['variation_id'];
$variation_obj = new WC_Product_Variation($variation_id);
$stock = $variation_obj->get_stock_quantity();
echo $stock; // <-- for testing purpose to see if I do get the individual variation-quantities = works!!
}
} else {
$stock = $product->get_stock_quantity();
}
// change text "In Stock' to 'Auf Lager' when quantity more than 3
if ($_product->is_in_stock() && $stock > 3) $availability['availability'] = $stock . ' ' .__('auf Lager', 'woocommerce');
// change text to n Left, where n is the quantity
if ($_product->is_in_stock() && ($stock <= 3)) $availability['availability'] = '<p class="stock ed_low_stock ' . esc_attr($availability['class']) . '">' . __('Nur noch ' . $stock . ' auf Lager!', 'woocommerce') . '</p>';
// change text "Out of Stock' to 'SOLD OUT'
if (!$_product->is_in_stock()) $availability['availability'] = __('zzt. nicht verfügbar!', 'woocommerce');
// change text "In Stock' to 'Special Order' for products with unmanaged stock
if (!$_product->managing_stock() && $_product->is_in_stock()) $availability['availability'] = __('Auf Lager', 'woocommerce');
return $availability;
}
はなぜこの仕事をしますか?
私は最後の時間にすべてを試しましたが、なぜこれがうまくいかないのかわかりません。
ご迷惑をおかけして申し訳ありません。
作業まあ、それは、部分的に正しいですが、私の質問に答えていません。 – Visualpro
申し訳ありません...あなたのコードをチェックしてテストしました。私は何が間違っていたかを見つけました... 下記の答えを見てください。 – LoicTheAztec