在做了大量的研究之后,我用下面的代码得到了我的结果。
为了获得最终结果,我必须安装WC Fields Factory插件,并使用控制台日志或变量def传递动态值
在下使用此代码。js文件,并确保它在按钮点击时触发。
$w.event.subscribe("item.book.click", function(item) {
$(\'[name="room_type"]\')[0].value = item[3].Name;
$(\'[name="date"]\')[0].value = item[4].date;
$(\'[name="nights"]\')[0].value = item[4].period;
$(\'[name="adults"]\')[0].value = item[4].adults;
$(\'[name="children"]\')[0].value = item[4].children;
$(\'[name="infants"]\')[0].value = item[4].infants;
$(\'[name="cost"]\')[0].value = item[3].Availability.Cost;
$(\'[name="add-to-cart"]\')[0].click(); // Clicked on add to cart button
});
功能。动态获取自定义成本值的php代码。
/* Price Update Based On Plugin WC Field */
function calculate_cart_total( $cart_object ) {
$additionalPrice = 0;
foreach ( $cart_object as $key => $valueArray ) {
if(is_array($valueArray)) {
foreach ( $valueArray as $k => $value ) {
if($value[\'wccpf_cost\']) {
$additionalPrice = $value[\'wccpf_cost\'][\'user_val\'];
}
}
}
}
foreach ( WC()->cart->get_cart() as $key => $value ) {
if( method_exists( $value[\'data\'], "set_price" ) ) {
$value[\'data\']->set_price( $additionalPrice );
} else {
$value[\'data\']->price = ($additionalPrice );
}
}
}
add_action( \'woocommerce_before_calculate_totals\', \'calculate_cart_total\', 99 );
/* before addto cart, clear cart values */
add_filter( \'woocommerce_add_to_cart_validation\', \'woo_custom_add_to_cart_before\' );
function woo_custom_add_to_cart_before( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return true;
}
在WC Fields Factory下,我创建了room\\u类型、日期、夜晚、成人、儿童、婴儿、成本字段,onclick值也会相应地动态提取。