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
});
請注意:代碼是未經測試這樣可能會出現一些錯別字;)