在 WooCommerce 可变产品单页中获取选定的产品变体 ID

Get the selected product variation ID in WooCommerce variable products single pages

对于我的 Woocommerce 可变产品,我在单个产品页面上实现了一个自定义尺寸选择器,并将此尺寸放在 table:

这些变体只是:30B 30C 30D 而不是拆分为:30B C D

我想弄清楚的是:如何获取每个产品变体的运费 class?

我在购物车页面上有类似的东西,但我不知道变体 ID 是什么或如何从单个产品页面获取它:

$product_id = ( 0 != $cart_item['variation_id'] ) ? $cart_item['variation_id'] : $cart_item['product_id'];
// need the variation id for the above to be able to do the rest:

$product_shipping_classes = get_the_terms( $product_id, 'product_shipping_class' );
$product_shipping_class_name = ( $product_shipping_classes && ! is_wp_error( $product_shipping_classes ) ) ? current( $product_shipping_classes )->name : '';

一旦有了变体 ID,我就知道该怎么做了。我只是不知道如何得到它才能完成剩下的工作。我唯一需要为此获取的是产品 ID 和变体的 slug(此页面上还有一个颜色属性)。

但是我已经为产品提供了一个默认变体以供使用(因此设置了隐藏 variation_id)。

也许需要先获取它以了解所选颜色的 ID,然后再获取其他颜色的变体 ID?

在可变产品的 WooCommerce 产品单页中,您无法在 PHP 中获取选定的产品变体,因为它是实时事件(客户端,而不是服务器端)。

方法是使用JavaScript/jQuery获取。由于您没有为这个单一产品页面提供任何关于您的可变产品的相关代码,因此我无法为您提供完全符合您需求的真实工作代码。

在可变产品的单个产品页面上获取所选变体 ID (在此示例中,我在添加到购物车按钮下方动态显示所选变体 ID):

add_action( 'woocommerce_single_product_summary', 'single_product_variable_customization', 3, 0 );
function single_product_variable_customization() {
    global $product;

    // Only for variable products on single product pages
    if ( $product->is_type('variable') && is_product() ) {

    ##  ==>  ==>  Here goes your php code (if needed)  ==>  ==>  ##

    // Passing a variable to javascript
    $string1 = "The selected Variation ID is: ";
    $string2 = "No Variation ID is selected ";
    ?>
    <script>
    jQuery(document).ready(function($) {

        // Initializing
        var myText1 = '<?php echo $string1; ?>', myText2 = '<?php echo $string2; ?>';

        // Get and display the default variation ID after add-to-cart button (initialization)
        if( '' != $('input.variation_id').val() )
            $('div.woocommerce-variation-add-to-cart').after('<div class="selected-variation-id">'+myText1+$('input.variation_id').val()+'</div>');
        else
            $('div.woocommerce-variation-add-to-cart').after('<div class="selected-variation-id">'+myText2+'</div>'); // No ID selected

        console.log($('input.variation_id').val()); // Output in the browser console


        // Get and display the chosen variation ID after add-to-cart button
        $('select').blur( function(){
            if( '' != $('input.variation_id').val() )
                $('div.selected-variation-id').html(myText1+$('input.variation_id').val());
            else
                $('div.selected-variation-id').html(myText2); // No ID selected

            console.log($('input.variation_id').val()); // Output in the browser console
        });
    });
    </script>
    <?php
    }
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

此代码已在 WooCommerce 3+ 下测试并有效。

So on this base you can make your own function to match your requirements (as you don't give anymore necessary details or code).

一些相关答案示例用法(与 jQuery 和 Woocommerce 单品页面):

  • Replace Variable Product Price by the chosen variation price in WooCommerce 3