Woocommerce - how to add custom fields to single product view? - woocommerce

my customer wants the shop "customers" (which are resellers in a b2b-shop) to be able to order every product for a variable number of stores for which he is responsible. If, for example, he has 3 stores, he wants
Store 1: 10 (qty)
Store 2: 10
Store 3: 5
and the regular WC amount field with +/- next to the big "add to cart" button should automatically update (to 25 in this case).
So I added some text fields to the single product view, below the thumbnails with this:
add_action( 'woocommerce_product_thumbnails', 'store_selector' );
function store_selector() {
$stores = get_stores();
echo '<div itemprop="store-selector" class="store-selector" style="font-size: 14px;">';
echo '<div class="product-border fusion-separator sep-"></div>';
echo '<input type='text' class='amount_per_store' name='amount_ps_".$store_id."' id='amount_ps_".$store_id."'>"'
echo '<br></div>';
}
So now I can loop through the fields, add them all up, and write the result to the big add-to-cart-field with simple jQuery. Works well.
But the Problem is: I don't seem able to access my custom fields later! All I can see is the "25", but not the order numbers for single stores. Of course not...WooCommerce does not really know my custom fields!
Maybe I should do something like this:
woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('amount_per_store'),
'label' => __('Store '.$store_id),
'placeholder' => __(''),
), $checkout->get_value( 'my_field_name' ));
I foud this here:
https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
...it's an example of how to add custom fields to other places.
I am a bit stuck at the moment because I don't know how to add custom fields for products the right way!
-A customer should be able to correct / edit his order and change the value of the custom fields when he goes back.
-Values should be carried through the ordering process, appear on the order confirmation page, in the order emails and also in the order details in the backend.
Any help is greatly acknowledged!

Related

Rank Math metabox preview for custom field

I've created a custom field in Rank Math...
add_action( 'rank_math/vars/register_extra_replacements', function(){
rank_math_register_var_replacement(
'op_shortcode',
[
'name' => esc_html__( 'OP ACF Field', 'rank-math' ),
'description' => esc_html__( 'Custom ACF field from ACF shortcodes.', 'rank-math' ),
'variable' => 'op_shortcode(field-name)',
'example' => esc_html__( 'Chrisad field value', 'rank-math' ),
],
'get_op_shortcode_callback'
);
});
function get_op_shortcode_callback( $shortcodename, $post_id ) {
global $post;
$post_id = "option"; // options page
$shortcodename = get_field( $shortcodename, $post_id, true );
return $shortcodename;
}
... it works in the frontend but I can't get it to be previewable in the backend metabox.
I wrote to Rank Math and the answer they gave first was, "There’s no additional code to be added to the filter since the custom variable is already working on the front end. For the preview to work, you need to ensure that the current content editor you are working on has WordPress editor support." (Which it does as far as I know because every other Rank Math variable is showing in the preview metabox).
Then they said, "When this happens it means that the custom variable you created contains code that cannot be rendered in the backend because the data is still not yet available." (I don't know what this means.)
And then finally, "The following article explains the default WordPress hooks firing sequence: http://rachievee.com/the-wordpress-hooks-firing-sequence/. Beyond that article, you will have to research further into how to get the variable to load. ACF support/forums might be able to help." (That's a 2015 article on hooks firing sequence which leads me to believe that there is a hook firing out of sequence but I can't figure it out).
I am just looking to see if anyone has any idea how to get the custom field I've registered to show up in the preview metabox in the backend (it is showing in the frontend).
Hope that makes sense.
Any help or insight is appreciated.

wordpress plugin GravityForms differenciate limit submissions for the same module in different pages

I'm trying to figure out a way to differenciate the limit submission for a "shared" form on different pages.
If I activate the built in function "limit submissions" in a form (which is shared on different pages) also the submissions limit is shared.
I try to explain better
I have 1 form, with let's say 50 limited submissions.
I use this form on page1, page2, page3
10 users go to page1, and submit the form, and nobody submitted anything on page2 or page3 yet.
The remaining allowed submissions in page2 and page3 would be 40.
I need to be able to limit submissions according to the specific pages where the form is used, so in this specific case I need to be able to have 40 submissions left on page1, but still 50 left on the other two pages.
Do you have any suggestion on how I can achieve this?
Thanks
You could try adding a hidden field to capture the embed page with the following merge tag in the default value: {embed_url}
Then add this to your functions.php
add_filter( 'gform_get_form_filter_100', 'limit_by_embed_page', 10, 2 );//replace 100 with your form id
function limit_by_embed_page( $form_string, $form ) {
$page=array("page1","page2","page3");//replace with name of pages where form is embedded
for($x=0;$x<count($page);$x++){
$search_criteria[$x] = array(
'status' => 'active',
'field_filters' => array(
'mode' => 'all',
array(
'key' => '2',
'operator' => 'contains',
'value' => $page[$x]
)
)
);
$entries[$x] = GFAPI::get_entries($form,$search_criteria[$x]);
$entries_count[$x] = count($entries[$x]);
if ( $entries_count[$x] > 50 && strpos($_SERVER['REQUEST_URI'], $page[$x]) !== false) {//change 50 to the # of the page limit
$form_string = '<p>Sorry, the submission limit for this has been reached.</p>';
}
}
return $form_string;
}
I'm not aware of a simple way to accomplish this with code but I do have a plugin that handles submission limits per page.
https://gravitywiz.com/documentation/gravity-forms-limit-submissions/#embed-url
Embed URL
Limit by the URL from which the entry was submitted. This is very useful when using the same form on multiple pages and wanting to apply the Feed Limit per page rather than per form.

How add field to the email report?

How add field to the email report??
http://www.evernote.com/l/Ac2uV87BrP9G9L7eKZplR1x3LrsGs-Yfsm8/
and
how add field to the order page
http://www.evernote.com/l/Ac1m-Ah7v71P9Zv3dmbIPXlHdxnrr9T7WkA/
You can add a field in email report using the following code
function wdm_custom_order_item_details($total_rows, $object){
$total_rows['custom Message']= array(
'label' => 'My Custom Message',
'value' => 'Custom information i want to see in order emails',
);
return $total_rows;
}
add_filter('woocommerce_get_order_item_totals','wdm_custom_order_item_details',10,2);
here the output will be
and to add field to the order page use this code
function wdm_admin_order_panel_after_shipping_address($order){
//use $order to get the details you want related to that order and display it here.
echo "<div><h4>My Custom Information</h4><p>Custom information i want to see in order details</p></div>";
}
//to add text after Shipping address of an order in admin panel
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'wdm_admin_order_panel_after_shipping_address' ,10,1);
here the output will be
Tested. Let me know if it worked for you.

Treat the node nids as a field (for display only in a content type) in drupal 7

I need to use the nid of a node as field in a content type: i need to choose where to print it (put it before some fields but after others) and format it as i wish. the only thing i could think about is create a "fake" custom field with no widget to insert it buth with a theme formatter to display it but it seems to me that this is a little to complicated. How should i do it?
If I understand correctly, you just want to expose data to the node view. Could it be as easy as using hook_node_view() from a module?
With that, you can set a 'fake' field to be sent out to the content array of the node, which you can access in the node template.
From drupal.org:
<?php
function hook_node_view($node, $view_mode, $langcode) {
$node->content['my_additional_field'] = array(
'#markup' => $additional_field,
'#weight' => 10,
'#theme' => 'mymodule_my_additional_field',
);
}
?>

Using HTML drop down boxes to sort through custom fields in WordPress

I have a website (located here) and I would like to do something to improve the way people search and view different reviews.
I have thought about introducing a way to search through posts based on rating. I currently have one category which has custom field with the field title "Rating". Each value in the custom field is an integer from 1 to 5.
Here's the test category (http://enoda.co.uk/category/ipad-2/ipad-reviews/ipad-utilities/)
I was wanting to know how I would go about introducing a drop down menu somewhere above the posts in the category with star ratings from 1 up to 5. Further, I would like it to take users to reviews with that rating.
Any help would be great,
Best regards,
Jack
Jack,
This is a somewhat complex issue as there are a number of areas within your WP installation that you need address to solve your problem. I'll point you in the right direction, but I don't anticipate that this will be the full answer for you.
First of all, the dropdown. Since you know that you want nothing more that a dropdown that displays choice of rating, you can easily hard code this. Since it is in a custom field, controlled by you, that only you/your colleagues set, you will know what is in that field and can feel safe hard coding this. Alternatively, I would have set up rating as a custom taxonomy with the terms '1 Star', '2 Stars', etc. and then you could build your drop down dynamically from this taxonomy. But, since it's in a custom field, I would just use something like this:
<form action="rating-search.php" method="GET">
<select name="rating-search" id="rating-search">
<option value="1">1 Star</option>
<option value="2">2 Stars</option>
<option value="3">3 Stars</option>
<option value="4">4 Stars</option>
<option value="5">5 Stars</option>
</select>
<input id="rating-search-submit" name="rating-search-submit" value="Search" />
</form>
Now, I'll assume that you can handle the submission of this data. On the page that receives this data (you can accomplish this with a template page), you will need to modify the wp_query to search only for posts that have a meta value of rating = {input value}. You will need code similar to:
$query = new WP_Query();
$query->query(
array(
'meta_query' => array(
array(
'key' => 'rating', // Insert the name of the meta key here
'value' => $_POST['rating-search'], // This is the rating you are matching
'compare' => '=',
'type' => 'NUMERIC'
)
)
)
);
if($query->have_posts())
{
while($query->have_posts())
{
$query->the_post();
global $post;
//Do normal loop business here
}
}
That query will help you search for posts that have the custom field set to the desired value. Note that the meta_query parameter only works for WP 3.1 and higher. It's a super powerful feature and worth updating to 3.1 alone. More about what you can do with the WP_Query class can be found here: http://codex.wordpress.org/Function_Reference/WP_Query.
I know you can't just copy and paste this code to get what you want, but I hope this gives you some ideas of what you can do to get closer to your desired result. Good luck!

Resources