最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WooCommerce add custom product_type_option

programmeradmin0浏览0评论

I added custom product_type_option like this

by filter product_type_option hook like this

function rpf_add_wild_card_product_type_option($type_options) {
    $checked = 'no';
    $type_options['wild_card'] = array(
        'id'            => '_wild_card',
        'wrapper_class' => 'show_if_simple',
        'label'         => __('Wild Card', 'woocommerce'),
        'description'   => __('Please check if this product is wild card', 'woocommerce'),
        'default'           => $checked,
    );
    return $type_options;
}
add_filter('product_type_option', 'rpf_add_wild_card_product_type_option', 100, 1);

But, WooCommerce never save Wild Card Field!

I want WooCommerce treat my wild card product type option like Virtual,Downloadable Field.

What should I do in this case?

I added custom product_type_option like this

by filter product_type_option hook like this

function rpf_add_wild_card_product_type_option($type_options) {
    $checked = 'no';
    $type_options['wild_card'] = array(
        'id'            => '_wild_card',
        'wrapper_class' => 'show_if_simple',
        'label'         => __('Wild Card', 'woocommerce'),
        'description'   => __('Please check if this product is wild card', 'woocommerce'),
        'default'           => $checked,
    );
    return $type_options;
}
add_filter('product_type_option', 'rpf_add_wild_card_product_type_option', 100, 1);

But, WooCommerce never save Wild Card Field!

I want WooCommerce treat my wild card product type option like Virtual,Downloadable Field.

What should I do in this case?

Share Improve this question edited Feb 11, 2020 at 18:44 Kolawole Emmanuel Izzy 2031 silver badge8 bronze badges asked Jan 27, 2018 at 6:23 Byeongin YoonByeongin Yoon 1661 silver badge10 bronze badges 1
  • please edit your question to put the code directly instead of a picture. – mmm Commented Jan 28, 2018 at 11:11
Add a comment  | 

1 Answer 1

Reset to default 5

Your code only creates the checkbox, you need also to save the value like that

add_filter("product_type_options", function ($product_type_options) {

    $product_type_options["wildcard"] = [
        "id"            => "_wildcard",
        "wrapper_class" => "show_if_simple",
        "label"         => "Wildcard",
        "description"   => "Description",
        "default"       => "no",
    ];

    return $product_type_options;

});


add_action("save_post_product", function ($post_ID, $product, $update) {

    update_post_meta(
          $product->ID
        , "_wildcard"
        , isset($_POST["_wildcard"]) ? "yes" : "no"
    );

}, 10, 3);
发布评论

评论列表(0)

  1. 暂无评论