I added some custom fields to the each product in Woocommerce, and I would like the data from the custom fields to show on the following receipt page (Order Details) after checkout is completed.
Just can't get it, not strong in php, why can't get anything. I've tried to use var_dump(get_post_custom($order->id)); i don't have my custom field in result.
Can somebody light me up?
Here is my code:
add_action( 'woocommerce_order_details_after_order_table', 'code_activation', 10, 1);
function code_activation($order){
echo '<p><strong>'.__('Activation code').':</strong> ' . get_post_meta( $order->id, 'activation_code', true ). '</p>';
}
You have added custom fields to the product. So custom fields will be associated with your product-id .
But you are trying to retrieve the custom fields using $order->id which is wrong. following code should help to retrieve the product id from order. And using the product id you can retrieve your custom field.
$orderItems = $order->get_items();
foreach($orderItems as $orderItem)
{
$product_id = $orderItem['variation_id'] ? $orderItem['variation_id'] : $orderItem['product_id'] );
}
Related
I am using ACF to add fields to my vendors' dashboard profile pages. I currently have a test ACF field loading the field from only the WP Admin profile page on all the vendors' product listing page using this simple hook in my child theme's functions.php:
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() { ?>
<?php if(get_field('founded_on')) { ?>
<?php the_field('founded_on'); ?>
<?php }
}
Perhaps I'm pulling the wrong hook, but I can't seem to find the right hook in the Product Vendor frontend page.
I need help customizing it so that when you are on a vendor's product page, it pulls the ACF fields from that particular vendor's profile. Currently, it partially works; however it only pulls the WP main admin's data for all the different vendors'.
I know there is a way to pull the vendor's ID for each particular vendor's page and have it load their data for their page, but my php knowledge is very limited. I usually just hunt for existing code and tweak it. Unfortunately I haven't found any solutions that have worked, and this is the closest I've come to getting custom fields to work on a vendor's page.
Or if anyone can point me to a better solution to allow me to create customer fields for a vendor to fill out that will be loaded on their front end page, that would be great. I've tried Nicola Mustone's solution ( here ), which would have been perfect, except I couldn't get it to load the new custom fields on the vendor's store profile form page, nor have it load the fields into that vendor's storefront page. Based on comments, it only shows up for the site's Admin and only they can edit it. There's no visible way to have it load on the storefront, which defeats the purpose.
I imagine that the providers are users with a certain level within the WordPress system?
Considering that this is your case, the ACF fields need some additional parameters to become visible:
$post_id = false; // current post
$post_id = 1; // post ID = 1
$post_id = "user_2"; // user ID = 2
$post_id = "category_3"; // category term ID = 3
$post_id = "event_4"; // event (custom taxonomy) term ID = 4
$post_id = "option"; // options page
$post_id = "options"; // same as above
$value1 = the_field( 'my_field', $post_id );
$value2 = get_field( 'my_field', $post_id );
take some examples found in the ACF documentation, but in your particular case you have to pass the user's ID
the_field('founded_on', 'user_' . $user->ID );
echo get_field('founded_on', 'user_' . $user->ID );
function documentation the_field()
You need to pull the user ID of the user and then use the format user_{$user->ID} for the post ID as the second parameter of the ACF field.
If I understand your question, then this should work.
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() {
$user = wp_get_current_user();
if ( get_field( 'founded_on', 'user_' . $user->ID ) ) {
the_field( 'founded_on', 'user_' . $user->ID );
}
}
i added a custom field to a variable product and displayed it on the front single product page with the help of an answer here
https://stackoverflow.com/a/55774613/14382875
by default the custom field is displayed on the front page above the add to cart button. but i want to move the custom field from there to right above the product meta data or the sku field in variable product. i was able to do this in simple product via hooks but the variable product code doesn't seem to be using any hook or i am missing something.
the code i used to display the variable product on front is this
function vp_variation_display_commodity_code( $data, $product, $variation ) {
if( $value = $variation->get_meta( 'jk_mpn' ) ) {
$data['price_html'] .= '<p class="vp-ccode"><strong>' . __("Manifacturer Part #", "woocommerce") .
': </strong>'.esc_html( $value ).'</small></p>';
}
return $data;
}```
I have a Custom Post type with two custom fields - Description and Attachment (to upload file/PDF).
When I complete this Custom Post I want the link to go directly to the attachment rather than the post page. I am using CPT UI and Custom Fields plugins to manage all this.
Does anyone know how I can create a custom post that will go directly to an attachment rather than the post page? I want to be able to display the title of each post on a page and have the title go to the attachment within the post.
I hope this makes sense and any help greatly appreciated!
This example assumes that you are using ACF to create the fields. And the field with the file gives its ID when requested (when creating a field in ACF there is an option to give ID or link)
add_filter( 'post_type_link', 'custom_post_permalink1', 10, 4 );
function custom_post_permalink1( $permalink, $post, $leavename, $sample ) {
// Change here to your post type name
if ( $post->post_type == 'your_post_type' ) {
$post_current_id = $post->ID;
// Change here 'file' to your custom field slug
if(get_post_meta($post_current_id, "file", true) ):
$PDF_ID = get_post_meta($post_current_id, "file", true);
$PDF_URL = wp_get_attachment_url( $PDF_ID );
$permalink = $PDF_URL;
endif;
}
return $permalink;
}
There is a way to get all the checked checkboxes of a custom field in Magento/Wordpress(with Fishpig extension):
$post->getCustomField($customfield)
I'm trying to filter the posts by selected checkboxes, and I'm considering to compare the filters to the posts via for loop, but is there a more efficient way of filtering posts?
You can create a custom collection of posts that are filtered by a custom field:
$posts = Mage::getResourceModel('wordpress/post_collection')
->addIsViewableFilter()
->addMetaFieldToFilter('custom_field_name', 'custom field value')
->load();
This will return all published posts that have the value 'custom field value' for the custom field called 'custom_field_name'.
Once you have a post model, the correct way to retrieve a custom field value is with the following:
$customFieldValue = $posts->getMetaValue('custom_field_name');
$posts = Mage::getResourceModel('wordpress/post_collection')
->addIsViewableFilter()
->load();
foreach($posts as $post) {
echo $post->getId() . '<br/>';
echo $post->getPostTitle() . '<br/>';
echo $post->getMetaValue('your_custom_field_name') . '<br/><br/>';
}
This code gets all posts and displays the post ID, post title and the value for the custom field that has the bame of 'your_custom_field_name'
I'm working with Wordpress 3.5.2 and plugin Advanced CUstom Fields 4.1.8
I have a group of fields (called "P") and some fields.
I want to retreive all data from a specific select field, and I found some code in the plugin documentation:
$values = get_field('field_519a0279bc93e');
if($values)
{
foreach($values as $value)
{
echo '<li>' . $value . '</li>';
}
echo '</ul>';
}
In $values, getting two different select fields In a case I get a string and in another a boolean.
I supposed is so simple, but I can't find the solution.
Thanks in advance.
It is important to understand that when using the Advanced Custom Fields plugin all this does is create an easier method for admins to add custom fields for posts, so the WordPress get_post_meta() still applies.
Say you've created a custom field called 'sex' and you've assigned a dropdown or radio buttons to it and you wanted to display what this value in the post or the loop, you would do the following.
Outside the loop
## Returns sex custom field value for current page ID
$sex = get_post_meta( get_the_ID, 'sex', true);
Inside the loop
## Returns sex custom field value for current loop iteration
global $post;
$sex = get_post_meta( $post->ID, 'sex', true);