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

          How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

          Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?