Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.8k views
in Technique[技术] by (71.8m points)

php - Adding products from specific Ids in WooCommerce product custom tab

I am using the code generated based on Add a custom multi-select field to admin product options settings in Woocommerce

// Display a multiselect field in "Linked Products" section
add_action( 'woocommerce_product_options_related', 'display_handles_product_field' );
function display_handles_product_field() {
    global $product_object, $post;
    ?>
    <p class="form-field">
        <label for="handles_product"><?php _e( 'Handles Product', 'woocommerce' ); ?></label>
        <select class="wc-product-search" multiple="multiple" id="handles_product_ids" name="_handles_product_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
            <?php
                $product_ids = $product_object->get_meta( '_handles_product_ids' );

                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>
    </p>
    <?php
}

// Save the values to the product
add_action( 'woocommerce_admin_process_product_object', 'save_handles_product_field_value', 10, 1 );
function save_handles_product_field_value( $product ){
    $data = isset( $_POST['_handles_product_ids'] ) ? array_map( 'intval', (array) $_POST['_handles_product_ids'] ) : array();
    $product->update_meta_data( '_handles_product_ids', $data );
}

This code adds a custom search box and adds related products in the admin area when creating or editing a product.

I also created a new tab on the product page to show the added products.

/* New Handles Product Tab */
add_filter( 'woocommerce_product_tabs', 'new_handles_product_tab' );
function new_handles_product_tab( $tabs ) {
    
    /* Add new tab */
    $tabs['new_handles_product_tab'] = array(
        'title'     => __( 'Handles Product', 'woocommerce' ),
        'priority'  => 50,
        'callback'  => 'new_handles_product_tab_content'
    );

    return $tabs;

}

/* Display content in new tab */
function new_handles_product_tab_content() {
    
    // Content

}

Tell me how to add these products to the new tab correctly? And is my code correct?

I will be glad for your help!

question from:https://stackoverflow.com/questions/65645951/adding-products-from-specific-ids-in-woocommerce-product-custom-tab

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using [products] shortcode with ids argument, the function displaying the product handles in a custom product tab will be:

function new_handles_product_tab_content() {
    global $product;
    
    $product_ids = $product->get_meta( '_handles_product_ids' ); // Get handles
    
    if( ! empty($product_ids) )
        $product_ids = implode(',', $product_ids);
        
        echo do_shortcode( "[products ids='$product_ids']" ); // Using [products] shortcode for display.
}

Untested it should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...