woocommerce: how to create a checkbox in the admin panel that will include a plugin

Анна Мороз :

I am creating a plugin that will change the look of the gallery in woocommerce. That is, instead of the gallery, an iframe will open. This works for all products.

add_filter( 'woocommerce_single_product_image_thumbnail_html', 'product_image');

function product_image(){
    $product = wc_get_product();
    $product_id = $product->get_id();
    $threedLink = 'http://sameurl/' .$product_id ;
    $content .= '<iframe src='.$threedLink.'  width="99%" height="300px"></iframe>';
    return $content;

}

But I need this to work not for all products, but for the chosen ones. That is, in the entire load of the product, you need to create a checkmark, where the administrator must agree to show the iframe. I created a tab in the product loading panel

function wk_custom_product_tab( $default_tabs ) {
    $default_tabs['custom_tab'] = array(
        'label'   =>  __( 'Custom Tab', 'domain' ),
        'target'  =>  'wk_custom_tab_data',
        'priority' => 60,
        'class'   => array()
    );
    return $default_tabs;
}

add_filter( 'woocommerce_product_data_tabs', 'wk_custom_product_tab', 10, 1 );

add_action( 'woocommerce_product_data_panels', 'wk_custom_tab_data' );

function wk_custom_tab_data() {
   echo '<div id="wk_custom_tab_data" class="panel woocommerce_options_panel">ddddd</div>';
}

how can I add a checkmark instead of ddddd and connect it with the plugin download?

7uc1f3r :
  • Add custom product tab
  • Add content to the product tab (checkbox)
  • Save the checkbox
  • Get value, 'yes' or 'no' and if yes, display iframe
/**
 * Add custom product tab.
 */
function wk_custom_product_tab( $default_tabs ) {
    $default_tabs['custom_tab'] = array(
        'label'   =>  __( 'Custom Tab', 'domain' ),
        'target'  =>  'wk_custom_tab_data',
        'priority' => 60,
        'class'   => array()
    );
    return $default_tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'wk_custom_product_tab', 10, 1 );

/**
 * Contents custom product tab.
 */
function wk_custom_tab_data() {

    global $post;

    // Note the 'id' attribute needs to match the 'target' parameter set above
    ?><div id='wk_custom_tab_data' class='panel woocommerce_options_panel'><?php

        ?><div class='options_group'><?php

            woocommerce_wp_checkbox( array(
                'id'        => '_allow_iframe',
                'label'     => __( 'Allow iFrame', 'woocommerce' ),
            ) );

        ?></div>

    </div><?php

}
add_filter( 'woocommerce_product_data_panels', 'wk_custom_tab_data' );

/**
 * Save the checkbox.
 */
function allow_iframe( $post_id ) {
    $allow_iframe = isset( $_POST['_allow_iframe'] ) ? 'yes' : 'no';

    // updates a post meta field based on the given post ID.
    update_post_meta( $post_id, '_allow_iframe', $allow_iframe );

}
add_action( 'woocommerce_process_product_meta', 'allow_iframe', 10, 1 );

/**
 * Product image
 */
function product_image( $html, $post_thumbnail_id ) {
    global $product;

    // Get product id
    $product_id = $product->get_id();

    // Get the custom field value
    $value = get_post_meta( $product_id, '_allow_iframe', true);

    // value = 'yes'
    if ( $value == 'yes' ) {
        $threedLink = 'http://sameurl/' .$product_id ;
        $html .= '<iframe src='.$threedLink.'  width="99%" height="300px"></iframe>';   
    }

    return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'product_image', 10, 2 );

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=21825&siteId=1