首页 > 解决方案 > WooCommerce 产品变体下拉列表的自定义 HTML

问题描述

我想获取单个产品页面变体下拉列表的自定义 HTML 。

我在这里得到了一些线索。

另一个参考链接

它的功能在这里 →

woocommerce/includes/wc-template-functions.php

我希望最终的 HTML 看起来像这样 →

<select class="customdropdown" name="customdropdown" id="customdropdown">
    <option value="license_one" data-1="500">License One</option>
    <option value="license_two" data-2="700">License Two</option>
    <option value="license_three" data-3="1400">License Three</option>
</select>

正确的流程是什么? 我们必须编辑一些模板,或者我们必须使用 WooCommerce 中的一些钩子进行更改?

如果有一些钩子。有人可以指导我正确的钩子吗?

标签: phpwordpresswoocommercehookproduct

解决方案


add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'wt_dropdown_choice' );

/**
 * Change the custom dropdown  "Choose an option" text on the front end
 */
function wt_dropdown_choice( $args ){
        if( is_product() ) {
                $args['show_option_none'] = "Add your custom text in here"; // Change your text here
                $args['class'] = 'customdropdown';
        }  
        return $args;    
}

尝试使用此代码将类添加到选择框 - 您无法更改属性的 id 和其他唯一名称,它将禁用添加到购物车功能以正常工作


推荐阅读