English 中文(简体)
根据WooCommerce产品变异的库存量显示不同的库存状况
原标题:Display different stock status based on stock quantity on WooCommerce product variations

我创建了一个旅游商店代理网站“WordPress with Elementor Free and Woocommerce ” 。 我的产品每种变异都有8个位子。

  • "Programmé" when stock quantity is up to 8,
  • "Initié" when stock quantity is 7 or 6,
  • "Confirmé" when stock quantity is 5 or 4,
  • "Dernières places" when stock quantity is 3, 2 or 1,
  • "Epuisé" when stock quantity is 0.

我试过以下代码:

<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <div class="product">
        <!-- PHP pour récupérer le stock du produit depuis WooCommerce -->
        <?php
        $product = wc_get_product( dates ); 
        $stock = $product->get_stock_quantity();
        ?>
        echo "En Stock: $stock";
        <div id="statut">Statut :</div>
    </div>

    <script>
        // Fonction pour mettre à jour le statut en fonction du stock récupéré
        function updateStatut() {
            // Récupérer le stock
            var stock = parseInt(document.getElementById("stock").textContent);

            // Mettre à jour le statut en fonction du stock
            var statutElement = document.getElementById("statut");
            if (stock >= 8) {
                statutElement.textContent = "Statut : Programmé";
            } else if (stock >= 6) {
                statutElement.textContent = "Statut : Initié";
            } else if (stock >= 4) {
                statutElement.textContent = "Statut : Confirmé";
            } else if (stock >= 1) {
                statutElement.textContent = "Statut : Dernières places";
            } else {
                statutElement.textContent = "Statut : Épuisé";
            }
        }

        // Appel initial de la fonction au chargement de la page
        updateStatut();
    </script>
</body>
</html>

问题似乎来自我无法捕捉到选中的产品变异 ID 。 我使用 ShopEngine, 所以有一个小菜单可以让我在我的产品页面上选择变异 。

问题回答

无需使用 JavaScript, 您可以使用 WooCommerce 专用过滤钩, 例如 :

add_filter( woocommerce_get_stock_html ,  filter_wc_get_stock_html , 10, 2 );
function filter_wc_get_stock_html($html, $product) {
    if ( ! $product->is_type( variation ) || ! $product->get_manage_stock() ) 
        return $html;

    $current_stock = $product->get_stock_quantity();
    
    if ($current_stock >= 8) {
        $status = __( Programmé ,  txtdomain );
    } 
    elseif ($current_stock >= 6) {
        $status = __( Initié ,  txtdomain );
    } 
    elseif ($current_stock >= 4) {
        $status = __( Confirmé ,  txtdomain );
    }
    elseif ($current_stock >= 1) {
        $status = __( Dernières places ,  txtdomain );
    } 
    else {
        $status = __( Épuisé ,  txtdomain );
    }
    return sprintf( <p class="stock">%s : %s</p> , __( Statut ,  txtdomain ), $status);
}

代码会在您的子主题( 或插件) 的.php 文件中输入函数。 测试和工作, 如果您没有更改与 WooCommerce 相关的模板 。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签