cheerychops
Forum Replies Created
-
Forum: Developing with WordPress
In reply to: “isset” not working in from submission to WP metaboxHi thanks for your input here are the results, no success though
Try 1. caused “Fatal error: Uncaught Error: Undefined constant ““wizpart”” in C:\xampp\htdocs\wiz_invent\wp-content\themes\storefront-child\functions.php”
if ( (! isset($_POST[“wizpart”][“sku”])) || (! isset($_POST[“wizpart”][“quant”]))) { print '<p class= "error">Fields not completed!</p>'; $okay = FALSE; }
Try 2. This produces no code error but does not prevent the saving of empty text fields as presumably $okay = FALSE
if ( (! isset($_POST['wizpart']['sku'])) || (! isset($_POST['wizpart']['quant']))) { print '<p class= "error">Fields not completed!</p>'; $okay = FALSE; }
Try 3. No errors but does not save data. And commenting the ‘if ($okay)’ results in empty string added.
if ( (! isset($_POST[0]['wizpart']['sku'])) || (! isset($_POST[0]['wizpart']['quant']))) { print '<p class= "error">Fields not completed!</p>'; $okay = FALSE; }
Try 4. No code errors still saving empty form text fields.
foreach( $_POST[$wizpart] as $entry){ // print out each part if (! isset($entry) ) { $okay = FALSE; return; } }
Try 6. Using ‘if ( empty($entry) )’ in above also fails to prevent saving of empty text fields.
The output from a successful save is;
array(2) { [0]=> array(2) { [“sku”]=> string(0) “” [“quant”]=> string(0) “” } [1]=> array(2) { [“sku”]=> string(3) “PA1” [“quant”]=> string(1) “6” } }
Product comprises units of
Product comprises 6 units of PA1Entire code
add_action( 'add_meta_boxes', 'wizpart_meta_box_init' ); // meta box functions for adding the meta box and saving the data function wizpart_meta_box_init() { // create our custom meta box add_meta_box( 'wizpart_meta', // Unique ID 'Parts Information', // Title 'wizpart_meta_box', // Callback function 'product', // post type 'normal', // Context 'high' // Priority ); } function wizpart_meta_box( $post ) { // Leading HTML // retrieve the custom meta box values $wizpart_meta_full = get_post_meta( $post->ID, '_wizpart_data'); echo var_dump($wizpart_meta_full).'<br />'; if (! empty($wizpart_meta_full)) { foreach( $wizpart_meta_full as $entry){ // print out each part print 'Product comprises '.$entry['quant'].' units of '.$entry['sku'].' <br />'; } } //nonce for security wp_nonce_field( 'meta_box_save', 'wizpart_plugin' ); // Form HTML echo '<div class = wizpart>'; echo '<table>'; echo '<tr>'; echo '<td> Enter correct SKU of part: </td>'; echo '<td> <input type="text" name="wizpart[sku]" value = "" size="5" > </td>'; echo '</tr>'; echo '<tr>'; echo '<td> Enter quantity of parts required as integer: </td>'; echo '<td> <input type="text" name="wizpart[quant]" value = "" size="5" > </td>'; echo '</tr>'; echo '</table>'; echo '</div>'; } // hook to save meta box data when the post is saved add_action( 'save_post', 'wizpart_save_meta_box' ); function wizpart_save_meta_box( $post_id ) { // Flag variable $okay = TRUE; // catch incomplete form /*if ( (! isset($_POST[0]['wizpart']['sku'])) || (! isset($_POST[0]['wizpart']['quant']))) { print '<p class= "error">Fields not completed!</p>'; $okay = FALSE; }*/ foreach( $_POST[$wizpart] as $entry){ // print out each part if (! isset($entry) ) { $okay = FALSE; return; } } // if auto saving skip saving our meta box data if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; //check nonce for security wp_verify_nonce( 'meta_box_save', 'wizpart_plugin' ); // store data in an array // Get the post ID from the from SKU $wizpart_data = $_POST['wizpart']; // use array map function to sanitize options $wizpart_data = array_map( 'sanitize_text_field', $wizpart_data) ; // save the meta box data as post meta using the post ID as a unique prefix // using add_post_meta to increment the array with values if ($okay) { add_post_meta( $post_id, '_wizpart_data', $wizpart_data ); print '<p class= "error">Success!</p>'; } else { print '<p class= "error">Encountered error</p>'; } }
Forum: Developing with WordPress
In reply to: “isset” not working in from submission to WP metaboxI’m afraid not, the code works to enter values when I comment out the ‘if’ statement
if ( (! isset($_POST["wizpart[sku]"])) || (! isset($_POST["wizpart[quant]"]))) { print '<p class= "error">Fields not completed!</p>'; $okay = FALSE; }
to give the following output;
array(1) { [0]=> array(2) { [“sku”]=> string(3) “PA1” [“quant”]=> string(1) “5” } }
Product comprises 5 units of PA1But with that code commented out using the post’s UPDATE button it will keep adding Null data
array(3) { [0]=> array(2) { [“sku”]=> string(3) “PA1” [“quant”]=> string(1) “5” } [1]=> array(2) { [“sku”]=> string(0) “” [“quant”]=> string(0) “” } [2]=> array(2) { [“sku”]=> string(0) “” [“quant”]=> string(0) “” } }
Product comprises 5 units of PA1
Product comprises units of
Product comprises units ofThe bottom ‘if’ appears to work as intended
if ($okay) { add_post_meta( $post_id, '_wizpart_data', $wizpart_data ); print '<p class= "error">Success!</p>'; } else { print '<p class= "error">Encountered error</p>'; }
Still nothing is printed though.
- This reply was modified 10 months ago by cheerychops. Reason: minor edit
Forum: Developing with WordPress
In reply to: Custom meta box saving post issuesThanks for your continuing patience!
This is the current code. It displays the first value using the ‘reset()’ function (bold) other wise it displays ‘NA’. Is this the best way to access the ‘nth’ entry in an associative 2D array?
Also I used the ‘add_post_meta’ (bold) function as the update overwrites the data – is this correct.
<?php // Meta Box Inventory Project /* Plugin Name: WIZ Custom Meta Box Plugin Plugin URI: Description: Version: 1.0 Author: David Author URI: License: */ add_action( 'add_meta_boxes', 'wizpart_meta_box_init' ); // meta box functions for adding the meta box and saving the data function wizpart_meta_box_init() { // create our custom meta box add_meta_box( 'wizpart_meta', 'Parts Information', 'wizpart_meta_box', 'product', 'normal', 'high' ); } function wizpart_meta_box( $post ) { // retrieve the custom meta box values $wizpart_meta = reset(get_post_meta( $post->ID, '_wizpart_data')); // debug echo var_dump($wizpart_meta); $wizpart_sku = (! empty( $wizpart_meta['sku'])) ? $wizpart_meta['sku'] : 'NA'; $wizpart_quant = (! empty( $wizpart_meta['quant'])) ? $wizpart_meta['quant'] : 'NA'; //nonce for security wp_nonce_field( 'meta_box_save', 'wizpart_plugin' ); // display stored values echo '<table>'; echo '<tr>'; echo '<td> SKU: </td>'; echo '<td> <input type="text" name="wizpart[sku]" value = "'.esc_attr( $wizpart_sku ).'" size="5" > </td>'; echo '</tr>'; echo '<tr>'; echo '<td> Quant: </td>'; echo '<td> <input type="text" name="wizpart[quant]" value = "'.esc_attr( $wizpart_quant ).'" size="5" > </td>'; echo '</tr>'; echo '</table>'; } // hook to save our meta box data when the post is saved add_action( 'save_post', 'wizpart_save_meta_box' ); function wizpart_save_meta_box( $post_id ) { // process form data if $_POST is set // if auto saving skip saving our meta box data if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; //check nonce for security wp_verify_nonce( 'meta_box_save', 'wizpart_plugin' ); // store data in an array $wizpart_data = $_POST['wizpart']; // use array map function to sanitize options $wizpart_data = array_map( 'sanitize_text_field', $wizpart_data) ; // save the meta box data as post meta using the post ID as a unique prefix // using add_post_meta to increment the array with values add_post_meta( $post_id, '_wizpart_data', $wizpart_data ); } ?>
- This reply was modified 10 months, 1 week ago by cheerychops. Reason: minor edit
- This reply was modified 10 months, 1 week ago by cheerychops. Reason: minor edit
Forum: Developing with WordPress
In reply to: Custom meta box saving post issuesThank you!
Saving into the array is working fine. I am not picking up the meta data as I probably have not understood how to extract from this array – which I need to do to display the previous entered meta data and to delete it if necessary.
array(3) { [0]=> array(2) { [“sku”]=> string(3) “GA1” [“quant”]=> string(1) “6” } [1]=> array(2) { [“sku”]=> string(3) “GA1” [“quant”]=> string(1) “6” } [2]=> array(2) { [“sku”]=> string(3) “GA1” [“quant”]=> string(1) “6” } }
I have also made a local installation for testing. I use Notepad++ which has an extension for the XDEBUG so I will look into that.
Forum: Plugins
In reply to: [WooCommerce] Linked ProductsThank you, that sounds like a great idea. I had not seen that option so I will explore, thanks!
have a good day (from a rainy UK)
Forum: Plugins
In reply to: [WooCommerce] Linked ProductsGood morning
Thank you for replying.
In more detail
- Yes, the products can be purchased separately.
- These ‘component’ products can be purchased individually in the same way as any simple product can be purchased.
The scenario is this. A model locomotive kit is composed of simple products, for example wheels, chimney, cab etc. It is quite possible that a locomotive kit could also be a variable product containing different formations of exterior features (e.g. chimney styles) suitable for different periods during its operational period.
However, if one component is not in stock the kit (or variant thereof) is not available.
For a locomotive kit, the inventory is = 1 if only if all components are available. The kit is made up ‘just in time’ for delivery. What we want to avoid is that a locomotive kit is ordered only to find that, for example, the wheel set required is ‘out of stock’.
It is quite possible that a chimney is ordered separately for a customer’s project. (Point 2 above)
What I am looking for is a (hopefully) free stock/inventory plug-in which allow ‘dependent’ components to be added to another product, and if a necessary kit component is missing then to automatically register the kit (or variant) as out of stock.
Additional functionality might be to indicate the missing component and raise an alert for the shop staff.
I hope this provides the detail you asked for.
Kind regards