在 Woocommerce 中针对每个产品变体显示不同的产品描述

问题描述 投票:0回答:1

我要疯了。我想做的是为每个设计创建一个列表,并在他们想要打印的材料上有“变化”。

例如:设计是“蜘蛛侠”,在变化下他们可以选择是否打印:辛特拉板、A3海报、金属等。

我想根据他们选择的材料(sintra、a3、金属的不同规格)显示不同的描述,并使其出现在主要产品描述上,而不是作为变体下的简短描述。

如果您可以查看castify的网站,一旦您选择了案例类型,下面的描述也会发生变化。

这可以通过插件实现吗?我希望有人能帮助我!

我没有编码经验,但我尝试了很多插件,但没有一个起作用

wordpress woocommerce
1个回答
0
投票

在仪表板 > 外观 > 编辑主题文件 > Functions.php 中尝试此代码并粘贴以下代码片段。

// Add custom JavaScript to the product page
    function custom_variation_description_script() {
        // Check if we're on a single product page
        if (is_product()) {
            global $product;
            // Check if the product has variations
            if ($product->is_type('variable')) {
                ?>
                <script>
                    jQuery(function($){
                        // When a variation is changed
                        $('form.variations_form').on('change', '.variation-select', function(){
                            // Get the selected variation description
                            var variationDescription = $(this).find('option:selected').data('variation-description');
                            // Update the product description with the variation description
                            $('.woocommerce-product-details__short-description').html(variationDescription);
                        });
                    });
                </script>
                <?php
            }
        }
    }
    add_action('wp_footer', 'custom_variation_description_script');
    
    // Add custom variation description data to each product variation
    function custom_variation_description($variations) {
        // Loop through each variation
        foreach ($variations as $variation) {
            // Get the variation ID
            $variation_id = $variation['variation_id'];
            // Get the variation object
            $product_variation = new WC_Product_Variation($variation_id);
            // Get the custom description for this variation (replace 'variation_description' with your custom field name)
            $variation_description = get_post_meta($variation_id, 'variation_description', true);
            // Add the custom description to the variation data
            $variation['variation_description'] = $variation_description;
        }
        return $variations;
    }
    add_filter('woocommerce_available_variation', 'custom_variation_description');
© www.soinside.com 2019 - 2024. All rights reserved.