Show ACF in customer account dashboard - Woocommerce - php

I would like to show a custom welcome message for each of my registered shop customers. Something like "Welcome CUSTOMERNAME! You have been with us for one year now. To say thanks, we are giving you a discount on everything in our shop of 10%."
I have created a custom field welcome_message with the plugin Advanced Custom Fields and would like to show the value of it in the account dashboard of my customers at frontend.
At the backend, I am showing the textarea at the user profile page and I am adding the field with this code:
$message = get_field( 'welcome_message' );
echo esc_attr( $message );
This code is placed here: /wp-content/themes/flatsome-child/woocommerce/myaccount/dashboard.php
But somehow the value is not coming up. I have also tried it with simply the_field('welcome_message'); or to add [acf field="{$welcome_message}"] at the account page in WordPress but that didn't work either. Somehow the value is not coming through.
I hope someone could help me out a litte.
Thanks in advance.
Best regards

It's necessary that get_field function is connected to user.
Eg:
<?php
$variable = get_field('field_name', 'user_1');
?>
In case of repeater field:
<?php if( have_rows('repeater', 'user_1') ): ?>
<ul>
<?php while( have_rows('repeater', 'user_1') ): the_row(); ?>
<li><?php the_sub_field('title'); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>

Related

Wordpress show different sidebar when one of two conditions is met (is_category('25')) II (in_category('25'))

I want to display different sidebars in wordpress for different categories and for posts belonging to these categories. I want to make this happen within one expression.
<?php if (is_category('25')) : ?>
<p>Sidebar for Category 25</p>
<?php elseif (is_category('26')) : ?>
<p>Sidebar for Category 26</p>
<?php else : ?>
<p>No custom sidebar for this post/category</p>
<?php endif; ?>
This works but when I try to display the custom sidebar for category page and single posts from cat25 at the same time with:
<?php if (is_category('25')) || (in_category('25')) : ?>
<p>Sidebar for category 25 archive and posts within category 25</p>
<?php elseif (is_category('26')) : ?>
<p>Sidebar for Category 26</p>
<?php else : ?>
<p>No custom sidebar for this post/category</p>
<?php endif; ?>
nothing happens.
I have a learning deficit and a hard time with logic. But still I try and I keep improving.
Please consider this when giving me an answer.
I made a simple mistake. I closed to early with the ) and used the ) one more time at the end.
At least this might help someone coming from a search engine.
This is the working code:
<?php if (is_category('25') || (in_category('25')) : ?>
<p>Sidebar for category 25 archive and posts within category 25</p>
<?php elseif (is_category('26')) : ?>
<p>Sidebar for Category 26</p>
<?php else : ?>
<p>No custom sidebar for this post/category</p>
<?php endif; ?>
If someone has a simpler solution or any changes according to best practices please still feel free to give your input :-)

Using PHP Wordpress variable do_shortcode for dynamic content

I'm building a Wordpress site and my aim to have users be able to place custom shortcodes in the backend of Wordpress on blog posts and have that content display on the site.
I'm using a simple text field Advanced Custom Field to do this (below.) but am struggling to integrate this with the do_shortcode variable.
Any help would be greatly appreciated, thanks.
Get Advanced Custom Field input to display as plaintext
<?php if( get_field('brand_video') ):?>
<?php the_field('brand_video');?>
<?php endif;?>
Get Wordpress to output video
<?php echo do_shortcode("[video id=204]"); ?>
My attempt of combining the two
<?php if( get_field('brand_video') ):?>
<?php echo do_shortcode(the_field('brand_video'));?>
<?php endif;?>
In do_shortcode replace the_field with get_field.
the_field is return a value.
get_field is print a value.
<?php if( get_field('brand_video') ):?>
<?php echo do_shortcode(get_field('brand_video'));?>
<?php endif;?>

How to add a custom block as a usual "product" to shop page / archive-product.php

I'm looking for a possibility to add a custom html block/post to the woocommerce "shop page" inside the products grid, as a product.
What I mean.. I have a grid of products on the "shop" page (archive-product) and I want to create a special post/page/html block with some text information, that will be inserted into the products grid as a one of "product", but with no price, with no title and unclickable. I've attached the screenshot of the final result I want to have, it's really self explaining - here it is exactly what I'm looking for.
As an idea probably I can create a special product with specific slug or title and the corresponding script with pre_get_posts hook will find this post/product and modify it to look like I need. I'm looking for some code/ideas how to insert this specific block/page/post into the archive-product page on some position in the grid. Thanks!
Thanks for help, guys! I've implemented the functionality I was looking for. I've found the corresponding loop in archive-product.php and as was suggested by JapanGuy, I've added a simple "if i equal let's say 5 then echo < li>[Custom block]< /li>" .
The original snippet from archive-product.php:
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
Modified code with inserted custom block:
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
if ($i == 5) {
echo "<li>[Custom block]</li>";
}
$i++;
?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
Im such simple way I can add any content to the created [Custom block] and have an usual products grid with extra custom designed block. I'm not very experienced programmer, so probably my code is not perfect, but it works. Thanks!
Edit: Previous code was wrong, changed it here
$i=0;
while ($row = mysqli_fetch_array($query))
{
if ($i == 2) {
echo "Cusom block";
}
echo "<p> Product block " . $row['column'] . " </p>";
$i++;
}
Creating WordPress Custom Post Archives : Hope this meets your requirement.
Custom Post Archives list your custom content. You probably already know the standard WordPress archives. So you can follow this to display both together .
Ref here : https://wp-types.com/documentation/user-guides/creating-wordpress-custom-post-archives/

Advanced Custom Fields after the main content

I am using a ACF select field to enable the page admin to select a category of posts to display under the page content. For example, if he selects "Televisions" in the ACF select field, all posts in that category will display after the page content. Here is the code for that bottom part of the page (after the main content), from the page template:
<h2>Learn more about <?php the_field('main_page'); ?></h2>
<ul>
<?php
$main_page=get_field('main_page');
$related_systems = new WP_Query( 'category_name='.$main_page );
while( $related_systems->have_posts() ) : $related_systems->the_post();
if($post->post_type == 'post'):
?>
<li><?php the_title();?></li>
<?php
endif;
endwhile; ?>
</ul>
Here is the ACF settings screenshot
The select field shows up fine on the admin side in all four pages that have the Main four page template, but both get_field('main_page') or the_field('main_page') end up blank (I tested get_field with echo and nothing shows up). How To get the field value in the page template?
I'm using WordPress 3.8.1 and ACF Version 4.3.5
Inside the main content, add $page_id = get_the_ID();.
And in the custom loop, call the fields:
the_field( 'main_page', $page_id );
get_field( 'main_page', $page_id );
Those functions work without an ID if used inside the loop, otherwise we need to specify what post we are requesting.
Also, you can filter the post type when calling WP_Query:
WP_Query ( 'post_type=post&category_name=' . $main_page );

Using Advanced Custom Fields and Contact Form 7 to display a form

I want my users to be able to put a Contact Form 7 shortcode into a custom field in the Wordpress editor. I've created the custom field using ACF and I can pull the value onto the page, but when I try to include it in the shortcode, it comes back with a 404.
This code:
<?php echo do_shortcode(get_field('contact_form_shortcode')); ?>
Returns:
[contact-form-7 404 "Not Found"]
If I create a variable out of the value like this:
<?php
$formCode = get_field('contact_form_shortcode');
echo $formCode;
?>
The echo returns:
[contact-form-7 id="473" title="Learn More Form"]
But I get the same 404 after putting that value into the echo do_shortcode function list this:
<?php echo do_shortcode($formCode); ?>
What am I missing?
To do it With ACF pro plugin and without other extra plugins.
Create a relation field ( example: contact_form )
add the below code into your page loop:
<?php $posts = get_field('contact_form');
if( $posts ):
foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT)
$cf7_id= $p->ID;
echo do_shortcode( '[contact-form-7 id="'.$cf7_id.'" ]' );
endforeach;
endif; ?>
I was able to resolve this issue by using the technique I discussed in my comment above. By using the WYSWIG field set to 'Run filter "the_content"' I'm able to pull the field value in the way I want it. The only drawback is that users could type something else in there besides a form shortcode.
Here's my final code:
<?php
if (get_field('contact_form_shortcode')):
echo get_field('contact_form_shortcode');
else:
echo do_shortcode('[contact-form-7 id="473" title="Learn More Form"]');
endif;
?>
Instead of using a WYSIWYG field, you can simply set a Text field to "Convert HTML into tags" under the Formatting setting on the field. This will stop the 404 errors from CF7 and properly process the form. The WYSIWYG fields tend to be too hard to control from bad user input.
Here's my solution:
Create a "Post Object" type Field which will returns the post ID.
the Filter by Post Type for this field should be sett to "Contact Form" and Filter by Taxonomy leave empty.
Then in page template: (php)
<?php $contact_form = get_field('contact_form'); ?>
HTML:
<div class="col-12 col-md-8 offset-md-2">
<?php echo do_shortcode('[contact-form-7 id="'. $contact_form .'"]'); ?>
</div>
acf field screenshot
Another Solution :
1) Install https://github.com/taylormsj/acf-cf7
Installation
> 1 Copy the acf-cf7 folder into your wp-content/plugins folder
> 2 Activate the Contact Form 7 plugin via the plugins admin page
> 3 Create a new field via ACF and select the Contact Form 7 type
> 4 Please refer to the description for more info regarding the field type settings
2) Insert following code into template :
<?php if( get_field('field_acf') ):
the_field('field_acf');
endif; ?>
This applies to create a contact form 7 in a modal or Pop Up, multiple forms on the same page.
To paste CF7 shortcode (or any other generated shortcode) into ACF field as you were asking, just use the #armadadrive solution, which is exactly what question was about and it works.
create ACF text field with some name, eg hero_form
paste your shortcode into it
display contents like so <?php echo do_shortcode(get_field('hero_form')); ?>
Thanks #armadadrive
Here you can try please check the code for the shortcode below:
<?php echo do_shortcode(get_field('YourACFfieldNameHere')); ?>
I tried it on my website and it works.

Categories