Skip to content
Advertisement

Woocommerce variable subscription custom field checkbox set default as unchecked

Below is my code in which I have set a custom field called mindesk_analytics_opt_out for my Variable Subscriptions and it’s working fine as per my expectations.

However, Whenever I create a new variation and open the panel, my default checkbox always set as checked. I cant understand where I have done wrong here in my code.

Can someone guide me please where I made a small mistake? Here is my working code.

<?php

// Showing fields for variable subscriptions 
add_action('woocommerce_product_after_variable_attributes', 'show_WC_Product_Variable_Subscription_Variation_Custom_Fields', 10, 3);

// Saving fields for variable subscriptions 
add_action('woocommerce_save_product_variation', 'save_WC_Product_Variable_Subscription_Variation_Custom_Fields', 10, 2);


function show_WC_Product_Variable_Subscription_Variation_Custom_Fields($loop, $variation_data, $variation) {

    $mindesk_analytics_opt_out = get_post_meta($variation->ID, 'mindesk_analytics_opt_out', true);
    echo '<strong>Mindesk e-Commerce</strong>';

    woocommerce_wp_checkbox(
        array(
            'id'            => "mindesk_analytics_opt_out{$loop}",
            'name'          => "mindesk_analytics_opt_out[{$loop}]",
            'wrapper_class' => 'show_if_simple',
            'label'         => __('&nbsp; Analytics', 'woocommerce'),
            'value'         => !empty($mindesk_analytics_opt_out) ? 'yes' : $mindesk_analytics_opt_out,
            'cbvalue'       => $mindesk_analytics_opt_out,

        )
    );
}
function save_WC_Product_Variable_Subscription_Variation_Custom_Fields($variation_id, $loop) {

    $mindesk_analytics_opt_out = isset($_POST['mindesk_analytics_opt_out'][$loop]) ? 'yes' : 'no';
    update_post_meta($variation_id, 'mindesk_analytics_opt_out', esc_attr($mindesk_analytics_opt_out));
}

Any advice or suggestion would be highly appreciated.

Advertisement

Answer

Updated: Instead use the following function replacement (checkbox will be unchecked by default)

// Add custom field checkbox for variable subscriptions 
add_action('woocommerce_product_after_variable_attributes', 'show_WC_Product_Variable_Subscription_Variation_Custom_Fields', 10, 3);
function show_WC_Product_Variable_Subscription_Variation_Custom_Fields($loop, $variation_data, $variation) {
    echo '<strong>' . __("Mindesk e-Commerce", 'woocommerce') . '</strong>';

    $value = get_post_meta($variation->ID, 'mindesk_analytics_opt_out', true);

    woocommerce_wp_checkbox(array(
        'id'            => "mindesk_analytics_opt_out{$loop}",
        'name'          => "mindesk_analytics_opt_out[{$loop}]",
        'wrapper_class' => 'show_if_simple',
        'label'         => __('&nbsp; Analytics', 'woocommerce'),
        'value'         => $value,
    ) );
}

Tested and works


This part doesn’t need changes:

// Saving fields for variable subscriptions 
add_action('woocommerce_save_product_variation', 'save_WC_Product_Variable_Subscription_Variation_Custom_Fields', 10, 2);
function save_WC_Product_Variable_Subscription_Variation_Custom_Fields($variation_id, $loop) 
{
    $value = isset($_POST['mindesk_analytics_opt_out'][$loop]) ? 'yes' : 'no';
    update_post_meta( $variation_id, 'mindesk_analytics_opt_out', esc_attr($value) );
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement