How to add more custom field in Linked Product of Woocommerce

前端 未结 1 602
梦谈多话
梦谈多话 2020-12-16 07:49

Thanks to all developers at StackOverflow.

I want to add more fields in Linked Product Section of Woocommerce. The fields should be similar to Upsell/Crosssell.

1条回答
  •  误落风尘
    2020-12-16 07:51

    There are several things missing in your code above.

    1. The hook you have used on the first line, do not exist. The right hook is called woocommerce_product_options_related
    2. The code where you've designed your custom field, is not inside any function.
    3. You are creating a standard text-field. If you want a "Select Product"-dropdown, you should use another approach. This should be inside that function that you're using in the hook.
    4. You are missing a hook and a function the actually save the data from your custom field

    1. Finding the right hook/action

    To find the right hook to use, just search for "woocommerce_product_options_" inside the WoocCommerce plugin, and about 6 PHP-files should appear. One of those files is called "html-product-data-linked-products.php". This file contains all the existing options in that specific WooCommerce section. It also contains the hook used to display those options.

    Open the file and check it out. The hook is at the bottom of the page

    Full Path: /wp-content/plugins/woocommerce/includes/admin/metaboxes/views/


    2. Creating the dropdown

    To create a select-dropdown including a product search, you need a much different code than the one above.

    The spare some time, you can just copy-paste one of the existing options, in the file mentioned above, and then modify it, to your needs.

    All this should be placed inside a function called: woocom_linked_products_data_custom_field().

    2.1. Modify the ID/name

    The first things you need to modify in the code, is of course the unique ID/name of the field. This is placed in the label-tag (for) and the select-tag (id and name).

    In your example the ID/name should be upsizing_products and the label-text Upsizing Products:

    
    
            ID, '_upsizing_products_ids', true );
    
                foreach ( $product_ids as $product_id ) {
                    $product = wc_get_product( $product_id );
                    if ( is_object( $product ) ) {
                        echo '';
                    }
                }
            ?>
         
    

    To display the stored data use _upsizing_products_ids:

    echo get_post_meta( $post->ID, 'my-field-slug', true );
    

    Check out this guide Mastering WooCommerce Products Custom Fields for more information about Custom Fields for WooCommerce.

    0 讨论(0)
提交回复
热议问题