我在 woocommerce 购物车和结帐中保存自定义复选框值时遇到问题。
我在结帐运输表中添加了自定义复选框,如果选择了某些运输方式,该复选框会添加自定义袋子。如果复选框被选中并且我选择了其他运输方式,然后使用复选框返回到某个,则在购物车摘要的 ajax 重新加载时未选中复选框。
我试图用这样的东西在 cookie 中保存复选框值
var checkbox = $('#your-form :checkbox:first'),
checkboxCookieName = 'checkbox-state';
checkbox.prop('checked', +$.cookie(checkboxCookieName));
checkbox.click(function() {
$.cookie(checkboxCookieName, +this.checked);
});
但它没有用。
复选框代码:
function my_custom_checkout_field() {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ($chosen_shipping == 'nova_poshta_shipping_method') {
echo '<div id="my_custom_checkout_field">';
woocommerce_form_field( 'my_field_name', array(
'type' => 'checkbox',
'checked' => 'checked',
'class' => array('input-checkbox'),
'label' => __('<span class="paperbagspan">Добавить фирменный пакет <img class="paperbag" src="https://paradisefruit.com.ua/wp-content/uploads/2019/06/paper-bag.png" style="width:30px"><img class="paperbagpink" style="display:none; width:30px" src="https://paradisefruit.com.ua/wp-content/uploads/2019/06/paper-bag-pink.png" ></span>'),
), WC()->checkout->get_value( 'my_field_name' ) );
echo '</div>';
}
}
add_action( 'woocommerce_after_shipping_rate', 'checkout_shipping_additional_field', 20, 2 );
function checkout_shipping_additional_field( $method, $index )
{
if( $method->get_id() == 'nova_poshta_shipping_method' ){
return my_custom_checkout_field();
}
}
// Save the custom checkout field in the order meta, when checkbox has been checked
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 10, 1 );
function custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['my_field_name'] ) )
update_post_meta( $order_id, 'my_field_name', sanitize_text_field($_POST['my_field_name'] ));
}
我做错了什么,或者是否有一些 wordpress 功能可以保存自定义字段值?
慕容森