make a woocommerce select product setting in plugin

So far, following code makes a select product setting in the tab added by code

public function __construct(){
add_action(‘woocommerce_product_data_tabs’, array($this, ‘add_manage_upsell_tab’), 20);

// add your data to existing data panel or custom tab
add_action(‘woocommerce_product_data_panels’, array($this, ‘cpm_manage_upsell_item’));
}
public function add_manage_upsell_tab( $product_data_tab){
$product_data_tab[”] = array(
‘label’ => __( ‘Manage Upsells’, ‘codepixelzmedia’ ),
‘target’ => ‘manage_upsell_items’,
‘class’ => array( ),
‘priority’ => 45
);
return $product_data_tab;
}
// Adding markup to existing or cutom data panel in product page
public function cpm_manage_upsell_item(){
// this section will display html markup so
?>
<div id=”manage_upsell_items” class=”panel woocommerce_options_panel”>
<p>
Second option to customize upsells based on conditions
</p>
<p class=”form-field”>
<label for=”upsell_products”><?php _e( ‘Products to Upsell’, ‘codepixelzmedia’ ); ?></label>
<select class=”wc-product-search” multiple=”multiple” style=”width: 50%;” id=”upsell_products” name=”upsell_products[]” data-placeholder=”<?php esc_attr_e( ‘Select Upsell product&hellip;’, ‘codepixelzmedia’ ); ?>” data-action=”woocommerce_json_search_products_and_variations” data-exclude=”<?php echo intval( $post->ID ); ?>”>
<?php
$product_ids = get_post_meta( $post->ID, ‘_upsell_products_ids’, true );

foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo ‘<option value=”‘ . esc_attr( $product_id ) . ‘”‘ . selected( true, true, false ) . ‘>’ . wp_kses_post( $product->get_formatted_name() ) . ‘</option>’;
}
}
?>
</select> <?php echo wc_help_tip( __( ‘Select Products Here.’, ‘codepixelzmedia’ ) ); ?>
</p>
</div>
}

This works perfectly but when I’m trying to move the same code to another plugin like:

public function __construct(){

// Action to add sub-level menu to woocommerce menu
add_action( ‘admin_menu’, array( $this, ‘cpm_upsell_menu’ ) );

// Register settings
add_action( ‘admin_init’, array( $this, ‘cpm_upsell_register_settings’ ) );

}

public function cpm_upsell_menu(){

add_submenu_page(
‘woocommerce’,
‘Up-Sell Settings’,
‘Manage Upsells’,
‘manage_options’,
‘cpm_upsell’,
array( $this, ‘cpm_display_settings_page’ )
);

}

// Display settings page for the plugin
public function cpm_display_settings_page(){

if( ! current_user_can( ‘manage_options’ ) ){ exit; }

?>

<div class=”wrap”>

<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>

<form action=”options.php” method=”post”>

<?php
// Output security fields
settings_fields( ‘cpm_upsell_options’ );

// Output settings sections
do_settings_sections( ‘cpm_upsell’ );

// Submit button
submit_button( “Save Settings” );
?>

</form>

</div>

<?php
}

public function cpm_upsell_callback_field_select( $args ){
global $products;

?>
<div id=”cpm_upsell_products” class=”panel woocommerce_options_panel”>

<p class=”form-field”>
<label for=”upsell_products”><?php _e( ‘Products to Upsell’, ‘codepixelzmedia’ ); ?></label>
<select class=”wc-product-search” multiple=”multiple” style=”width: 50%;” id=”upsell_products” name=”upsell_products[]” data-placeholder=”<?php esc_attr_e( ‘Select Upsell product&hellip;’, ‘codepixelzmedia’ ); ?>” data-action=”woocommerce_json_search_products_and_variations” data-exclude=”<?php echo intval( $post->ID ); ?>”>
<?php
$product_ids = get_post_meta( $post->ID, ‘_upsell_products_ids’, true );

foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo ‘<option value=”‘ . esc_attr( $product_id ) . ‘”‘ . selected( true, true, false ) . ‘>’ . wp_kses_post( $product->get_formatted_name() ) . ‘</option>’;
}
}
?>
</select> <?php echo wc_help_tip( __( ‘Select Products Here.’, ‘codepixelzmedia’ ) ); ?>
</p>
<?php
</div>
}

This above code doesn’t work as it was supposed to.

It’s very long question but I tried not to include full code ( class declaration, instantiate class ).
My question is can we make a choose products setting ( just like woocommerce product select setting ) in the later plugin

Comment if there is any clarification you need.

Any help would be appreciated.

Read more here:: make a woocommerce select product setting in plugin

Leave a Reply

Your email address will not be published. Required fields are marked *