WooCommerce在.variations_form
元素中存储名为data-product_variations
的数据属性中的变体详细信息(如价格,尺寸,重量等)。此数据用于显示用户选择特定变体时的价格和其他详细信息。
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
更多细节:您可以使用过滤器woocommerce_available_variation
像这个数据属性添加您的meta值。
一旦获得了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
});
请注意:代码是未经测试这样可能会出现一些错别字;)