WooCommerce 将变体详细信息(如价格、尺寸、重量等)存储在 .variations_form 元素中名为 data-product_variations 的数据属性中。当用户选择特定变体时,此数据用于显示价格和其他详细信息。您可以使用过滤器 woocommerce_available_variation 在此数据属性中添加元值,例如:
add_filter('woocommerce_available_variation', 'vna_add_variation_meta');
function vna_add_variation_meta($dataattr, $product, $variation) {
$dataattr['pre_order'] = get_post_meta($variation->get_id(), 'pre_order', true);
return $dataattr;
}
您可以在class-wc-product-variable.php 文件中找到有关woocommerce_available_variation 的更多详细信息。
一旦您在 DOM 元素中获得元值,您可以使用以下事件来了解用户何时选择了一个变体。
$( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {
// Fired when the user selects all the required dropdowns / attributes
// and a final variation is selected / shown
// Here you will need to get the value of 'pre_order' from the variation object
// something like: variation.pre_order
// and take appropriate action
} );
请注意:代码未经测试,因此可能存在一些拼写错误;)