I am trying to display a set of custom fields on WooCommerce Vendor pages via Advanced Custom Fields. For the base vendor functionality, I am using the following plugin: https://woocommerce.com/products/product-vendors/
I have added the custom fields under ACF Field Groups and set the display rules to "Taxonomy » Vendors". With this in place, the custom fields are displaying within each individual "Edit Vendor" dashboard.
Within the vendor plugin files I have found where I would like to display the custom fields, which is on line 210 of the file WooCommerce Product Vendors / includes / woocommerce-product-vendors/includes/class-wc-product-vendors-vendor-frontend.php
Initially I tried using the following basic ACF code to display the custom fields, to no avail:
<p id="sample"><?php the_field('field_name'); ?></p>
I have also tried saving the field as a variable and then displaying it, again to no avail:
<?php
$variable = get_field('field_name');
echo '<p>' . $variable . '</p>';
?>
In both of these examples, the <p> wrapping elements are showing up on the front end, but the custom fields/variables are not.
One interesting thing I found is that when I use ACF to create an additional options panel within the WordPress dashboard, I am then easily able to display these variables from the options panel within the vendor pages. Following is the functions.php code I use to create this options panel:
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'Additional Theme Options',
'menu_title' => 'Additional Theme Options',
'menu_slug' => 'additional-Theme-options',
'capability' => 'edit_posts',
'redirect' => false
));
}
And then within the plugin file referenced as above, if I insert the following script it displays the options panel fields perfectly:
<?php the_field('sample_option_field', 'option'); ?>
The problem with the options panel route is that these are then global, singular variables and not registered on a per-vendor basis.
Thanks!
Thanks Fresz, defining the terms worked perfectly. Following is the code I inserted into the plugin file to properly display the custom fields from the Vendor taxonomy:
// get the current taxonomy term
$term = get_queried_object();
// vars
$vendorphoto = get_field('vendor_secondary_photo', $term);
$street = get_field('vendor_street', $term);
$city = get_field('vendor_city', $term);
$state = get_field('vendor_state', $term);
$zip = get_field('vendor_zip', $term);
echo '<img src="'.$vendorphoto.'">';
echo '<p id="sample">'.$street. "<br />" .$city. ", " .$state. " " .$zip.'</p>';
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'm wondering if there is a way to borrow the "product gallery" feature of woocommerce and add it to a custom post type in wordpress.
I know i can go through the woocommerce source codes and search and find its nuts and bolts and pieces in the woocommerce plugin folder, put them together and customize them for a specific custom post type. I know these are some of the corresponding files in the woocommerce plugin folder:
For its javascript:
meta-boxes-product.js
single-product.js
For rendering its html template:
wc-template-functions.php > There is wc_get_gallery_image_html function
class-wc-admin-meta-boxes.php
add_meta_box( 'woocommerce-product-images', __( 'Product gallery', 'woocommerce' ), 'WC_Meta_Box_Product_Images::output', 'product', 'side', 'low' );
class-wc-meta-box-product-images.php
etc.
BUT this is prone to errors and bugs. I'm wondering if there is a better, easier, more elegant, less buggy way to implement this feature to a custom post type. Maybe there is a hook and/or a class somewhere that you guys know about and i'm missing here.
Just to clarify my question, i actually have a custom post type it's called "projects". I need a gallery for this custom post type which accepts new images for my custom post type, so i thought i could use the "product gallery" feature from woocommerce instead of writing a gallery from scratch for my custom post type.
Thank you.
Not tested but if you could get the
product_id
inside your CPT file try this code:
<?php
$product_id = '14';
$product = new WC_product($product_id);
$attachment_ids = $product->get_gallery_image_ids();
foreach( $attachment_ids as $attachment_id )
{
// Display the image URL
echo $Original_image_url = wp_get_attachment_url( $attachment_id );
// Display Image instead of URL
echo wp_get_attachment_image($attachment_id, 'full');
}
?>
This should work!
We are using carbon field plugin for custom field in WordPress site but problem is that WordPress is not able to store revision for carbon custom filed. Please any one can help?
There are two way to include carbon field plugin in theme.
1) adding carbon field plugin in plugin folder and activate.
2) adding carbon field plugin folder in theme folder.
/* Integrating Carbon Fields Plugin */
use Carbon_Fields\Container;
use Carbon_Fields\Field;
add_action( 'after_setup_theme', 'crb_setup_theme' );
function crb_setup_theme() {
// check if Carbon Fields is not already installed
if ( ! class_exists( "\\Carbon_Fields\\Field\\Field" ) ) {
require( get_template_directory() . '/carbon-fields/carbon-fields-plugin.php' );
}
}
add_action('carbon_register_fields', 'crb_register_custom_fields');
function crb_register_custom_fields() {
require get_template_directory() . '/theme_option/meta_boxes.php';
}
In meta_boxes.php file you need to create metafield. like this
use Carbon_Fields\Container;
use Carbon_Fields\Field;
Container::make('post_meta', 'Date Container') // New Added
->show_on_post_type(array('hotel')) // Hotel is custom post type
->add_fields(array(
Field::make('complex','hotel_details_page_content', 'Images & Content')
->add_fields('Entry Container', array(
Field::make("separator", "crb_image_options", "Image Entry"),
Field::make("image", "row_images", "Image"),
Field::make("text", "row_image_headlines", "Image Headline"),
Field::make("text", "image_row_subheadlines", "Image Sub Headline"),
Field::make("separator", "crb_style_options", "Content Entry"),
Field::make("text", "content_titles", "Title"),
Field::make("rich_text", "content_datas", "Content"),
)),
));
Container::make('post_meta', 'Gallery Container')
->show_on_post_type('gallery') // Gallery is custom post type
->add_fields(array(
Field::make('complex','galleryimages', 'Gallery')
->add_fields('Gallery Entry', array(
Field::make("image", "gallery_images", "Upload Image"),
))
));
In this way you can create multiple fields as repeater and value also save.
and Get values on frontend.
$carbvalue= carbon_get_post_meta(get_the_ID(), 'hotel_details_page_content', 'complex');
foreach ($carbvalue as $carbonvalues) {
$herorimageid = $carbonvalues['row_images'];
$heroimage = wp_get_attachment_url($herorimageid);
?>
<img src="<?php echo $heroimage; ?>" sizes="100vw" alt="heroimage" />
<?php
echo $carbonvalues['row_image_headlines'];
}
I’m trying to set a sidebar in the single product page that show all products of the same categories as the product displayed.
That's how I proceed:
1) First I’ve created a sidebar called “Products_of_same_Category” to put in there a widget to show what I needed, then in function.php of my child theme I added the following snippet to execute php code in text widget:
// Enable PHP in widgets
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
2) Then when I saw that the snippet runs ok I added that code to test it:
<?php
$prod=get_the_term_list( $post->ID, 'product_cat');
echo $prod;
?>
And all worked fine, it gave me the exact name of the current category of the product displayed in the single product page.
3) So I've tried another test, deleting the previous code, to view if a shortcode translated in php should works in a widget too (writing at this time the exact category name requested, in this case "towels" - in the code below I substitute it with THE-CATEGORY-I-LIKE):
<?php echo do_shortcode('[product_category category=“THE-CATEGORY-I-LIKE” per_page="20" columns="1" orderby="title" order="desc"]'); ?>`
And all was again well done!
4) Finally I mixed all in this code to show the list of products of same categories but something goes wrong:
<?php $prod=get_the_term_list( $post->ID, 'product_cat', '', '', '' );
echo do_shortcode('[product_category category="'.$prod.'" per_page="20" columns="1" orderby="title" order="desc"]'); ?>
In last case the code doesn't display anything. I don't understand where I made mistakes, the syntax is wrong or the solving approach is illogical?
I really appreciate any help about this.
The problem is how you get the category slug. get_the_term_list will give you a formatted linked list of the categories, so it will display category names, not category slugs, which are different things. "Towels" would be your category name, but the category slug would be "towels". And product_category shortcode expect a slug, not a name.
The correct approach to get the category product slug is the following:
$terms = get_the_terms($post, 'product_cat');
if($terms && ! is_wp_error($terms)) {
foreach($terms as $term) {
echo do_shortcode('[product_category category="'.$term->slug.'" per_page="20" columns="1" orderby="title" order="desc"]');
}
}
This will display the products of all the categories associated to your product. See get_the_terms doc for reference.
In order to remove from the results the current product shown, you can make use of the woocommerce_shortcode_products_query filter. It isn't documented, but you can find it by looking at the product_category shortcode code located in includes/class-wc-shortcodes.php. In the product_category() method you will find the following line:
$return = self::product_loop( $query_args, $atts, 'product_cat' );
Where $query_args is a WP_Query parameters array. In the same class you will find the method product_loop() called here and see the following:
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $query_args, $atts ) );
So the query arguments are filtered - you will be able to work with that to add the desirated behavour. What you want to do is to use the post__not_in parameter to the query like this:
function remove_current_product_from_wc_shortcode($args, $atts) {
if(is_product()) { // check if we're on a single product page
$args['post__not_in'] = array(get_queried_object_id());
}
return $args;
}
add_filter('woocommerce_shortcode_products_query', 'remove_current_product_from_wc_shortcode');
This code should go in your theme functions.php - please not this is untested, so if it doesn't work look at the get_queried_object_id() return if it contain the current product ID.
In wordpress, I have custom pages and inside the pages I'm trying to call the most recent posts for a specific category.
Doing so, I added this to create the variable...
$cat = get_post_meta($post->ID, "mom_cat", true);
And in the custom field of the page, I added mom_cat = (variable #)
In my post, I am trying to show the recent posts based o the variable category # I put in the custom field. I tried this, but it didn't work...
<?php
global $post;
$myposts = get_posts('numberposts=4&category=$cat' );
foreach($myposts as $post) :
?>
Hoever that is not working. How do I add a variable to "category = " in order to display a category based on my custom field setting?
Thanks
You shouldn't be using Custom Fields to define categories when Wordpress has all of the tools for you at your disposal. What you should do is find where your custom post is being registered, and add:
'taxonomies' => array('category')
...to your argument array. This will enable you to check off the categories that your custom posts require. If you can't find where the custom post type is being registered, add this instead to your functions.php file:
add_action('init', 'add_category_to_custom');
function add_category_to_custom()
{
register_taxonomy_for_object_type('category', 'custom_post_name');
}
THEN you can reference your custom post like so:
$posts = get_posts(array('numberposts' => 4, 'category' => $cat_ID, 'post_type' => 'custom_post_name'));
UPDATE: I wouldn't advise using Pages to display Category information, but I'm sure you have your reasons. I would still avoid placing category IDs within Custom Fields, simply because if one of your category IDs change, then it can cause LOTS of problems.
What I would advise in your case is to name those particular Pages exactly the same as their matching categories. Then do something like this:
<?php
/*
IF THE PAGE HAS A MATCHING CATEGORY,
DISPLAY 5 OF THE MOST RECENT POSTS IN THAT CATEGORY
*/
if($catID = get_cat_ID(get_the_title(get_the_ID())))
{
$posts = get_posts(array('numberposts' => 5, 'category' => $catID));
foreach($posts as $post) : setup_postdata($post);
?>
<!-- POST HTML GOES HERE -->
<?php
endforeach;
}
?>