Php 获取变量产品属性的选定值

Php 获取变量产品属性的选定值,php,wordpress,woocommerce,attributes,product,Php,Wordpress,Woocommerce,Attributes,Product,我在我的woocommerce单一产品页面中添加了两个属性:金属和戒指尺寸。它们是变体,因此它们是“我的产品”页面上的下拉选择 我想能够点击我的按钮,我可以抓住所选的值,并把它放在我的URL参数 我解决了所有问题,直到抓取的值不正确的部分 到目前为止,我已经尝试过了,但它只是给了我一个可能值/选择的属性列表,而不是单一的选择值 $metal = $product->get_attribute( 'Metal' ); http://example.com/?metal=18K%20Whit

我在我的woocommerce单一产品页面中添加了两个属性:金属和戒指尺寸。它们是变体,因此它们是“我的产品”页面上的下拉选择

我想能够点击我的按钮,我可以抓住所选的值,并把它放在我的URL参数

我解决了所有问题,直到抓取的值不正确的部分

到目前为止,我已经尝试过了,但它只是给了我一个可能值/选择的属性列表,而不是单一的选择值

$metal = $product->get_attribute( 'Metal' );

http://example.com/?metal=18K%20White%20Gold%20|%2018K%20Yellow%20Gold%20|%20Rose%20Gold%20|%20Platinum
我只想要我在下拉列表中选择的内容,如下所示:

http://example.com/?metal=Platinum

下面是一个代码示例,您可能需要定制一点。该代码将:

显示用于测试目的的临时自定义按钮,自定义href值为:home url/?metal=…home url是您的网站url 从金属属性中获取所选值,并将其附加到按钮URL。 代码如下:

add_filter( 'woocommerce_single_variation', 'get_variation_selectected_value', 25 );
function get_variation_selectected_value() {
    global $product;

    // Display button (Just for testing purpose) 
    // You can remove or comment the line of code below …
    // But you need to set your url like: http://example.com/?metal=
    echo '<br /><a href="'.home_url("/?metal=").'" class="book-now button">'. __('Make an Appointment Now') .'</a>';

    // The script
    ?>
    <script>
    jQuery(document).ready(function($) {
        // Get the default value
        var selectedValue = $('select#metal option:checked').val(),
            hrefAttribute = $('a.book-now').attr("href");
            $('a.book-now').attr("href", hrefAttribute+selectedValue);

        // Display on browser console (for test)
        console.log($('a.book-now').attr("href"));

        // Get the live selected value
        $('select#metal').blur( function(){
            selectedValue = $('select#metal option:checked').val();
            hrefAttribute = $('a.book-now').attr("href");
            $('a.book-now').attr("href", hrefAttribute+selectedValue);

            // Display on browser console (for test)
            console.log($('a.book-now').attr("href"));
        });
    });
    </script>
    <?php
}
代码位于活动子主题或主题的function.php文件中,或者位于任何插件文件中


该代码已经用另一个属性进行了测试,可以正常工作。

使用以前的代码,我只想通过php来完成,因为这一切都在一起……我不知道该怎么做,因为我认为一旦点击“预约”按钮,下拉列表中选择的值将被保存或进入服务器。有没有办法回显下拉按钮?所以我可以在php中使用它?但我想这相当困难,因为选择需要改变形象和价格的反应…谢谢你,工程完美!如果我也在URL中添加“环大小”下拉列表,会是什么?我试着换成这个,但没用。是因为环大小键入了错误的空格吗?$'selectring\u Size'。模糊函数{selectedValue=$'selectring\u Size选项:选中'.val;hrefAttribute=$'a.book-now'.attrref;$'a.book-now'.attrref,hrefAttribute+'&Ring='+selectedValue;};我找到了,它的戒指大小。所以现在我试着自己做——因为我想要。这就是我所做的,但当我选择这两个选项时,它会给我一个奇怪的URL这是我尝试的->jQuerydocument.readyfunction${$'select.blur函数{selectedValue=$'SelectedMetal option:checked'.val;ringValue=$'selectring-size option:checked'.val;hrefAttribute=$'a.book-now'.attrref;$'a.book-now'.attrref,hrefAttribute+'&metal='+selectedValue+'&ringsize='+ringValue;};;实时链接忽略产品SKU和id->