Show a custom product field if you click a checkbox in WooCommerceWooCommerce Custom Field in CheckoutWoocommerce override Product Price with custom value on cart and checkoutAdding Custom Meta Data from Products to Orders items in WoocommerceBest practice for custom product fields in WooCommerce?Display a custom field under short description in Woocommerce single product pagesShow multiple product custom fields values in cart on WoocommerceGet product custom field values as variables in WooCommerceAdd custom fields to custom product calculated price in WoocommerceDisplay custom backorder text from a custom field in Woocommerce cart itemsCustom field in Linked Products woocommerce

A verb to describe specific positioning of three layers

What are the basics of commands in Minecraft Java Edition?

Was Apollo 13 radio blackout on reentry longer than expected?

Strategy to pay off revolving debt while building reserve savings fund?

Which GPUs to get for Mathematical Optimization (if any...)?

Use chown -R while excluding one or two files

Why does "git status" show I'm on the master branch and "git branch" does not in a newly created repository?

What is the word for "event executor"?

Should I be able to keep my company purchased standing desk when I leave my job?

Can you perfectly wrap a cube with this blocky shape?

How to find location on Cambridge-Mildenhall railway that still has tracks/rails?

Can a Resident Assistant be told to ignore a lawful order?'

Vienna To Graz By Rail

Movie where a man was put into a computer before death, wife doesn't trust him anymore

My credit card has no magnetic stripe. Is this a problem in the USA?

Why is Katakana not pronounced Katagana?

Cauchy reals and Dedekind reals satisfy "the same mathematical theorems"

Wordplay subtraction paradox

What "fuel more powerful than anything the West (had) in stock" put Laika in orbit aboard Sputnik 2?

Is it okay for a chapter's POV to shift as it progresses?

At which point can a system be compromised when downloading archived data from an untrusted source?

Is it legal for a supermarket to refuse to sell an adult beer if an adult with them doesn’t have their ID?

Increasing muscle power without increasing volume

Is passive Investigation essentially truesight against illusions?



Show a custom product field if you click a checkbox in WooCommerce


WooCommerce Custom Field in CheckoutWoocommerce override Product Price with custom value on cart and checkoutAdding Custom Meta Data from Products to Orders items in WoocommerceBest practice for custom product fields in WooCommerce?Display a custom field under short description in Woocommerce single product pagesShow multiple product custom fields values in cart on WoocommerceGet product custom field values as variables in WooCommerceAdd custom fields to custom product calculated price in WoocommerceDisplay custom backorder text from a custom field in Woocommerce cart itemsCustom field in Linked Products woocommerce






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have a code that displays the checkbox on the product edit page.



Here is my code:



// Display Roast Level Custom Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_roast_general_fields' );

function woo_roast_general_fields()
// Create a custom checkbox field

// Checkbox
woocommerce_wp_checkbox(
array(
'id' => '_roast',
'label' => __('Roast Level', 'woocommerce' ),
'description' => __( 'Enable roast level!', 'woocommerce' )
)
);



// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_roast_general_fields_save' );

/** Hook callback function to save custom fields information */
function woo_roast_general_fields_save( $post_id )

// Save Checkbox
$checkbox = isset( $_POST['_roast'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_roast', $checkbox );



When you click on this checkbox, on the page of a single product, the selection field should be displayed. After selecting the options, these data should be displayed in the shopping cart, on the checkout page, on the Thank You page, email and when editing the order.



Here is my second code:



// The product custom field before add-to-cart button 
add_action( 'woocommerce_before_add_to_cart_button', 'woo_roast_custom_field' );

function woo_roast_custom_field()
global $product;

echo '<div>';

woocommerce_form_field( 'roast_custom_field', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Roast Level'),
'required' => false,
'options' => array(
'' => 'Please select',
'Blue' => 'Blue',
'Rare' => 'Rare',
'Medium Rare' => 'Medium Rare',
'Medium' => 'Medium',
'Medium Well' => 'Medium Well',
'Well Done' => 'Well Done'
)), '' );

echo '</div>';


// Save the new unique value in the array of values (as product meta data)
add_action( 'woocommerce_add_to_cart', 'roast_custom_field_add_to_cart', 20, 6 );

function roast_custom_field_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data )

if( isset($_POST['roast_custom_field']) )
// Get the array of existing values
$roast_values = (array) get_post_meta( $product_id, '_roast_custom_field_values', true );
// append the new value to the array of values
$roast_values[] = sanitize_text_field( $_POST['roast_custom_field'] );
// Save the appended array
update_post_meta( $product_id, '_roast_custom_field_values', $roast_values );





But unfortunately, I do not know how to make a condition so that when you click on the checkbox, a selection box will appear on the page of a single product.



It should be noted that if the checkbox is not active, then the selection field should not be displayed on the page of a single product.



Also, I'm not quite sure about the correctness of the code when saving data.



I ask for your help!










share|improve this question






























    1















    I have a code that displays the checkbox on the product edit page.



    Here is my code:



    // Display Roast Level Custom Fields
    add_action( 'woocommerce_product_options_general_product_data', 'woo_roast_general_fields' );

    function woo_roast_general_fields()
    // Create a custom checkbox field

    // Checkbox
    woocommerce_wp_checkbox(
    array(
    'id' => '_roast',
    'label' => __('Roast Level', 'woocommerce' ),
    'description' => __( 'Enable roast level!', 'woocommerce' )
    )
    );



    // Save Fields
    add_action( 'woocommerce_process_product_meta', 'woo_roast_general_fields_save' );

    /** Hook callback function to save custom fields information */
    function woo_roast_general_fields_save( $post_id )

    // Save Checkbox
    $checkbox = isset( $_POST['_roast'] ) ? 'yes' : 'no';
    update_post_meta( $post_id, '_roast', $checkbox );



    When you click on this checkbox, on the page of a single product, the selection field should be displayed. After selecting the options, these data should be displayed in the shopping cart, on the checkout page, on the Thank You page, email and when editing the order.



    Here is my second code:



    // The product custom field before add-to-cart button 
    add_action( 'woocommerce_before_add_to_cart_button', 'woo_roast_custom_field' );

    function woo_roast_custom_field()
    global $product;

    echo '<div>';

    woocommerce_form_field( 'roast_custom_field', array(
    'type' => 'select',
    'class' => array('my-field-class form-row-wide'),
    'label' => __('Roast Level'),
    'required' => false,
    'options' => array(
    '' => 'Please select',
    'Blue' => 'Blue',
    'Rare' => 'Rare',
    'Medium Rare' => 'Medium Rare',
    'Medium' => 'Medium',
    'Medium Well' => 'Medium Well',
    'Well Done' => 'Well Done'
    )), '' );

    echo '</div>';


    // Save the new unique value in the array of values (as product meta data)
    add_action( 'woocommerce_add_to_cart', 'roast_custom_field_add_to_cart', 20, 6 );

    function roast_custom_field_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data )

    if( isset($_POST['roast_custom_field']) )
    // Get the array of existing values
    $roast_values = (array) get_post_meta( $product_id, '_roast_custom_field_values', true );
    // append the new value to the array of values
    $roast_values[] = sanitize_text_field( $_POST['roast_custom_field'] );
    // Save the appended array
    update_post_meta( $product_id, '_roast_custom_field_values', $roast_values );





    But unfortunately, I do not know how to make a condition so that when you click on the checkbox, a selection box will appear on the page of a single product.



    It should be noted that if the checkbox is not active, then the selection field should not be displayed on the page of a single product.



    Also, I'm not quite sure about the correctness of the code when saving data.



    I ask for your help!










    share|improve this question


























      1












      1








      1








      I have a code that displays the checkbox on the product edit page.



      Here is my code:



      // Display Roast Level Custom Fields
      add_action( 'woocommerce_product_options_general_product_data', 'woo_roast_general_fields' );

      function woo_roast_general_fields()
      // Create a custom checkbox field

      // Checkbox
      woocommerce_wp_checkbox(
      array(
      'id' => '_roast',
      'label' => __('Roast Level', 'woocommerce' ),
      'description' => __( 'Enable roast level!', 'woocommerce' )
      )
      );



      // Save Fields
      add_action( 'woocommerce_process_product_meta', 'woo_roast_general_fields_save' );

      /** Hook callback function to save custom fields information */
      function woo_roast_general_fields_save( $post_id )

      // Save Checkbox
      $checkbox = isset( $_POST['_roast'] ) ? 'yes' : 'no';
      update_post_meta( $post_id, '_roast', $checkbox );



      When you click on this checkbox, on the page of a single product, the selection field should be displayed. After selecting the options, these data should be displayed in the shopping cart, on the checkout page, on the Thank You page, email and when editing the order.



      Here is my second code:



      // The product custom field before add-to-cart button 
      add_action( 'woocommerce_before_add_to_cart_button', 'woo_roast_custom_field' );

      function woo_roast_custom_field()
      global $product;

      echo '<div>';

      woocommerce_form_field( 'roast_custom_field', array(
      'type' => 'select',
      'class' => array('my-field-class form-row-wide'),
      'label' => __('Roast Level'),
      'required' => false,
      'options' => array(
      '' => 'Please select',
      'Blue' => 'Blue',
      'Rare' => 'Rare',
      'Medium Rare' => 'Medium Rare',
      'Medium' => 'Medium',
      'Medium Well' => 'Medium Well',
      'Well Done' => 'Well Done'
      )), '' );

      echo '</div>';


      // Save the new unique value in the array of values (as product meta data)
      add_action( 'woocommerce_add_to_cart', 'roast_custom_field_add_to_cart', 20, 6 );

      function roast_custom_field_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data )

      if( isset($_POST['roast_custom_field']) )
      // Get the array of existing values
      $roast_values = (array) get_post_meta( $product_id, '_roast_custom_field_values', true );
      // append the new value to the array of values
      $roast_values[] = sanitize_text_field( $_POST['roast_custom_field'] );
      // Save the appended array
      update_post_meta( $product_id, '_roast_custom_field_values', $roast_values );





      But unfortunately, I do not know how to make a condition so that when you click on the checkbox, a selection box will appear on the page of a single product.



      It should be noted that if the checkbox is not active, then the selection field should not be displayed on the page of a single product.



      Also, I'm not quite sure about the correctness of the code when saving data.



      I ask for your help!










      share|improve this question
















      I have a code that displays the checkbox on the product edit page.



      Here is my code:



      // Display Roast Level Custom Fields
      add_action( 'woocommerce_product_options_general_product_data', 'woo_roast_general_fields' );

      function woo_roast_general_fields()
      // Create a custom checkbox field

      // Checkbox
      woocommerce_wp_checkbox(
      array(
      'id' => '_roast',
      'label' => __('Roast Level', 'woocommerce' ),
      'description' => __( 'Enable roast level!', 'woocommerce' )
      )
      );



      // Save Fields
      add_action( 'woocommerce_process_product_meta', 'woo_roast_general_fields_save' );

      /** Hook callback function to save custom fields information */
      function woo_roast_general_fields_save( $post_id )

      // Save Checkbox
      $checkbox = isset( $_POST['_roast'] ) ? 'yes' : 'no';
      update_post_meta( $post_id, '_roast', $checkbox );



      When you click on this checkbox, on the page of a single product, the selection field should be displayed. After selecting the options, these data should be displayed in the shopping cart, on the checkout page, on the Thank You page, email and when editing the order.



      Here is my second code:



      // The product custom field before add-to-cart button 
      add_action( 'woocommerce_before_add_to_cart_button', 'woo_roast_custom_field' );

      function woo_roast_custom_field()
      global $product;

      echo '<div>';

      woocommerce_form_field( 'roast_custom_field', array(
      'type' => 'select',
      'class' => array('my-field-class form-row-wide'),
      'label' => __('Roast Level'),
      'required' => false,
      'options' => array(
      '' => 'Please select',
      'Blue' => 'Blue',
      'Rare' => 'Rare',
      'Medium Rare' => 'Medium Rare',
      'Medium' => 'Medium',
      'Medium Well' => 'Medium Well',
      'Well Done' => 'Well Done'
      )), '' );

      echo '</div>';


      // Save the new unique value in the array of values (as product meta data)
      add_action( 'woocommerce_add_to_cart', 'roast_custom_field_add_to_cart', 20, 6 );

      function roast_custom_field_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data )

      if( isset($_POST['roast_custom_field']) )
      // Get the array of existing values
      $roast_values = (array) get_post_meta( $product_id, '_roast_custom_field_values', true );
      // append the new value to the array of values
      $roast_values[] = sanitize_text_field( $_POST['roast_custom_field'] );
      // Save the appended array
      update_post_meta( $product_id, '_roast_custom_field_values', $roast_values );





      But unfortunately, I do not know how to make a condition so that when you click on the checkbox, a selection box will appear on the page of a single product.



      It should be noted that if the checkbox is not active, then the selection field should not be displayed on the page of a single product.



      Also, I'm not quite sure about the correctness of the code when saving data.



      I ask for your help!







      wordpress woocommerce custom-fields






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 26 at 10:03







      Dmitry

















      asked Mar 26 at 9:12









      DmitryDmitry

      4510 bronze badges




      4510 bronze badges






















          0






          active

          oldest

          votes










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55353382%2fshow-a-custom-product-field-if-you-click-a-checkbox-in-woocommerce%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes




          Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







          Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55353382%2fshow-a-custom-product-field-if-you-click-a-checkbox-in-woocommerce%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현