WooCommerce default variation display price - woocommerce

I need to change something here to show default variation price on my store page:
<div class="deal-part2-details">
<?php
if($product->get_type() == "simple"){
?>
<h5><?php echo get_field('network_price_text',$p_id); ?> <?php echo $currency; echo $product->regular_price; ?>pm</h5>
<h5 class="our-price"><?php echo get_field('our_price_text',$p_id); ?> : <?php echo $currency; echo $product->sale_price; ?>pm</h5>
<h6><?php echo get_field('upfront_cost',$p_id); ?> <?php echo get_field('upfront_cost_text',$p_id); ?></h6>
<?php
}else{
$product_variations = $product->get_available_variations();
$variation_product_id = $product_variations [0]['variation_id'];
$variation_product = new WC_Product_Variation( $variation_product_id );
?>
<h5 class="network-price"><?php echo get_field('network_price_text',$p_id); ?> <?php echo $currency; echo $variation_product->regular_price; ?>pm</h5>
<h5 class="our-price"><?php echo get_field('our_price_text',$p_id); ?> : <?php echo $currency; echo $variation_product->sale_price; ?>pm</h5>
<!--<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo $price_html; ?></span>
<?php endif; ?> -->
<h6><?php echo get_field('upfront_cost',$p_id); ?><?php echo get_field('upfront_cost_text',$p_id); ?></h6>
<?php
}
?>
</div>
Would you be able to help me?

I just change code like that, and working for me:
<div class="deal-part2-details">
<?php
if($product->get_type() == "simple"){
?>
<h5><?php echo get_field('network_price_text',$p_id); ?> <?php echo $currency; echo $product->regular_price; ?>pm</h5>
<h5 class="our-price"><?php echo get_field('our_price_text',$p_id); ?> : <?php echo $currency; echo $product->sale_price; ?>pm</h5>
<h6><?php echo get_field('upfront_cost',$p_id); ?> <?php echo get_field('upfront_cost_text',$p_id); ?></h6>
<?php
}else{
$product_variations = $product->get_available_variations();
$variation_product_id = $product_variations [0]['variation_id'];
$variation_product = new WC_Product_Variation( $variation_product_id );
$price_regular_min = $product->get_variation_regular_price();
$price_sale_min = $product->get_variation_sale_price();
?>
<h5 class="network-price"><?php echo get_field('network_price_text',$p_id); ?> <?php echo $currency; echo $price_regular_min; ?>pm</h5>
<h5 class="our-price"><?php echo get_field('our_price_text',$p_id); ?> : <?php echo $currency; echo $price_sale_min; ?>pm</h5>
<!--<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo $price_html; ?></span>
<?php endif; ?> -->
<h6><?php echo get_field('upfront_cost',$p_id); ?><?php echo get_field('upfront_cost_text',$p_id); ?></h6>
<?php
}
?>
</div>
For that point I just add two variable:
$price_regular_min = $product->get_variation_regular_price();
$price_sale_min = $product->get_variation_sale_price();
And then I activated them here:
<h5 class="network-price"><?php echo get_field('network_price_text',$p_id); ?> <?php echo $currency; echo $price_regular_min; ?>pm</h5>
<h5 class="our-price"><?php echo get_field('our_price_text',$p_id); ?> : <?php echo $currency; echo $price_sale_min; ?>pm</h5>
I made these changes in my loop-start.php
This is my simple solution and working pretty well.

Solution
Step1:
1st you can edit variable product & select variation values. Which variation
you want shown on product page Default Form Values See the screenshot Select Default value screenshot
Step:2
Then go to your active theme or child theme functions.php file put below code.
//woo_default_price_variation_price
add_filter('woocommerce_variable_price_html', 'woo_default_price_variation_price', 10, 2);
function woo_default_price_variation_price( $price, $product ) {
foreach($product->get_available_variations() as $pav){
$def=true;
foreach($product->get_variation_default_attributes() as $defkey=>$defval){
if($pav['attributes']['attribute_'.$defkey]!=$defval){
$def=false;
}
}
if($def){
$price = $pav['display_price'];
}
}
return woocommerce_price($price);
}
It's 100% working
Thanks

Related

ACF repeaater Not working even after coding the fields right

My code
<?php if( have_rows('mir_global_header') ): ?>
<div id="mid-bg1">
<div id="top-container1">
<?php while( have_rows('mir_global_header') ): the_row();
$count = get_row_index();
$header_image = get_sub_field('header_image');
$header_image_text = get_sub_field('header_image_text');
?>
<img src="<?php echo $header_image; ?>" id="<?php echo $count; ?>" alt="<?php echo $header_image_text; ?>" />
<?php endwhile; ?>
</div>
</div>
<?php endif; ?>
I've done everything as mentioned in the documentation. It's not working. Is it because ACF pro is not activated? Is there any other way to do this? Thanks in advance
If you're using ACF options page. You have to specify that in the have_rows. https://www.advancedcustomfields.com/resources/get-values-from-an-options-page/
<?php if( have_rows('mir_global_header', 'option') ): ?>
<div id="mid-bg1">
<div id="top-container1">
<?php while( have_rows('mir_global_header', 'option') ): the_row();
$count = get_row_index();
$header_image = get_sub_field('header_image');
$header_image_text = get_sub_field('header_image_text');
?>
<img src="<?php echo $header_image; ?>" id="<?php echo $count; ?>" alt="<?php echo $header_image_text; ?>" />
<?php endwhile; ?>
</div>
</div>
<?php endif; ?>
Are your fields in an options page or a classic post_type?
Test this for print a result
$mir_global_header = get_field('mir_global_header');
echo '<pre>';
print_r($mir_global_header );
echo '</pre>';die();
PS : ACF Gallery gives the same result ;)
https://www.advancedcustomfields.com/resources/gallery/

Display images knowing their ID

I'm trying to use a gallery field add-on to Wordpress' ACF plugin. The add-on is Navz Photo Gallery. All it does is to give you a way to get the gallery photos' IDs. Now I want to actually display the images.
By calling the following code
<?php if ( get_field( 'field_name') ) { ?>
<img src="<?php the_field( 'field_name' ); ?>" />
<?php } ?>
all I get are the actual IDs, like this:
<img src="21,22,23">
Does anybody know how to loop thru this group of images and display them individually, not just the ID, but the actual image?
For reference, ACF has an official (paid) gallery add-on, and it displays the attached images like this:
<?php $images = get_field('gallery'); if( $images ): ?>
<?php foreach( $images as $image ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endforeach; ?>
<?php endif; ?>
Navz has helped me with this:
<?php
$images = get_field('fotos_do_projeto'); if( $images ): $images = explode(',', $images); $images = array_filter($images); if( count($images)):
?>
<ul>
<?php foreach( $images as $image ): $alt = get_the_title($image); $url = get_post_meta($image, 'field_5802bd6e39e9b_url', true); ?>
<li>
<a href="<?php echo $url; ?>" title="<?php echo $alt; ?>">
<?php echo wp_get_attachment_image($image, "thumbnail", false, ['alt' => $alt]); ?>
</a>
</li>
<?php endforeach; endif; ?>
</ul>
<?php endif; ?>
You can try this,
$attachment_id = get_field( 'field_name');
$image_attributes = wp_get_attachment_image_src( $attachment_id );
if ( $image_attributes ) : ?>
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>" />
<?php endif; ?>

WP: acf image fields & simple lightbox plugin integration

I'm trying to integrate the Simple Lightbox plugin into Advanced Custom Fields image fields.
Basically I want any images on a page (populated via acf image fields) to open in the lightbox when clicked, and also be grouped together with all other images within the lightbox so they can be navigated through like a slideshow. This is built-in functionality for the Simple Lightbox plugin, but I don't know how to integrate it with my template code for image fields.
For acf WYSIWYG fields I'm using this...
<?php
$content = get_field('wysiwyg');
if ( function_exists('slb_activate') )
$content = slb_activate($content);
echo $content;
?>
...which works great, but adapting that to image fields is eluding me.
Here's my template code for image fields (without any attempts at integrating Simple Lightbox):
<?php
$image = get_field( 'img' );
if( ! empty( $image ) ) {
$url = $image['url'];
$alt = $image['alt'];
$caption = $image['caption'];
$size = 'large';
$thumb = $image['sizes'][ $size ];
$width = $image['sizes'][ $size . '-width' ];
?>
<?php if( $caption ): ?>
<div class="wp-caption" style="width: <?php echo $width; ?>px">
<?php endif; ?>
<a href="<?php echo $url; ?>">
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" />
</a>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php } ?>
Any suggestions for how to integrate Simple Lightbox into this? Thanks in advance!
Use php output buffering to store your template in a variable. Put ob_start() at the top, then at the bottom ob_get_clean() will stop the output buffering, and you can save the return value to a variable. Then pass that to slb_activate.
<?php
ob_start();
$image = get_field( 'img' );
if( ! empty( $image ) ) {
$url = $image['url'];
$alt = $image['alt'];
$caption = $image['caption'];
$size = 'large';
$thumb = $image['sizes'][ $size ];
$width = $image['sizes'][ $size . '-width' ];
?>
<?php if( $caption ): ?>
<div class="wp-caption" style="width: <?php echo $width; ?>px">
<?php endif; ?>
<a href="<?php echo $url; ?>">
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" />
</a>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php } ?>
<?php $content = ob_get_clean(); ?>
<?php echo slb_activate($content); ?>

Group Posts by Category in Monthly Archive

I am trying to group the posts by Category in a Monthly archive but don't know how to achieve that.
Can someone here help me please.
Regards
This is the php code I am using in archive.php
<?php
$terms = get_terms("publicationcat");
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
echo "<div class=\"publication-area\">";
echo '<h3 class="term-heading">' . $term->name . '</h3>';
echo "<div class=\"publication-txt\">";
echo "<ul>";
while ( have_posts() ) : the_post();
echo "<li><a target=\"_blank\" href=\"" . wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)) . "\">".$post->post_title."</a></li>";
endwhile;
echo "</ul>";
echo "</div>";
echo "</div>";
}
}
?>
The only problem is that its displaying same post titles for all the terms..
When you get to looping through the post in each category, you are not finding only the posts related to that category, and are instead looping through EVERY post. The below (untested, so comment if you get errors) should sort your problem.
Please note however that you may still get some repetition, if a post is assigned to more than one category.
Check each post to ensure it is in the desired category. Good point - only one query. Bad point - loops through all posts for every cat.
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php if($my_query->have_posts()) : ?>
<ul>
<?php if(in_category($term->slug)) : ?>
<?php while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
<?php endif; ?>
</div>
</div>
<?php
endforeach;
endif;
?>
Re-query the posts and output all that are grabbed. Good point - only pulls only the posts required for the category being looped. Bad point - lots of queries, so many queries could slow your site down.
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php
$args = array(
'cat' => $term->term_id
);
$my_query = new WP_Query($args);
?>
<ul>
<?php if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</ul>
</div>
</div>
<?php
endforeach;
endif;
?>

how to display portfolio category above the content in loop portfolio page

i want to display portfolio category above the content where all portfolio is displayed. i know some of the modification is required in loop portfolio.php but how put that so that it display above the content post. i want to display in ul li format how it possible?? i m using broadscope theme
my code for loop portfolio.php is given below
<?php
global $avia_config;
if(isset($avia_config['new_query'])) {
query_posts($avia_config['new_query']);
}
// check if we got a page to display:
if (have_posts()) :
$loop_counter = 1;
$extraClass = 'first';
//iterate over the posts
while (have_posts()) : the_post();
//get the categories for each post and create a string that serves as classes so the javascript can sort by those classes
$sort_classes = "";
$item_categories = get_the_terms( $id, 'portfolio_entries' );
if(is_object($item_categories) || is_array($item_categories))
{
foreach ($item_categories as $cat)
{
$sort_classes .= $cat->slug.'_sort ';
}
}
?>
<div class='post-entry one_third all_sort <?php echo $sort_classes.$extraClass; ?>'>
<?php echo avia_inc_display_featured_within_entry('portfolio', false); ?>
<?php the_title(); ?>
<span class='date_sort hidden'><?php the_time('Y m d H i s'); ?></span>
<div class="entry-content">
<!--<?php the_excerpt(); ?> -->
<!--<a class="more-link" href="<?php echo get_permalink(); ?>"><?php _e('Read more','avia_framework'); ?></a>
-->
</div>
<!-- end post-entry-->
</div>
<?php
$loop_counter++;
$extraClass = "";
if($loop_counter > 3)
{
$loop_counter = 1;
$extraClass = 'first';
}
endwhile;
else:
?>
<div class="entry">
<h1 class='post-title'><?php _e('Nothing Found', 'avia_framework'); ?></h1>
<p><?php _e('Sorry, no posts matched your criteria', 'avia_framework'); ?></p>
</div>
<?php
endif;
?>
i want to display portfolio category before
<a href="<?php echo get_permalink() ?>" rel="bookmark" title="<?php
_e('Permanent Link:','avia_framework')?> <?php echo avia_inc_display_featured_within_entry('portfolio', false); ?>">
inside
<div class='post-entry one_third all_sort <?php echo
$sort_classes.$extraClass; ?>'>
so please help me if anyone know this. hope friends you help me regarding this question
Try to insert the following code
<?php
$post_id = get_the_ID()
$item_terms = get_the_terms( $post_id, 'portfolio_entries' );
if($item_terms !== false && count($item_terms) > 0)
{
echo '<ul>';
foreach ($item_terms as $term)
{
echo '<li>'.$term->name.'</li>';
}
echo '</ul>';
}
?>
just after
<div class='post-entry one_third all_sort <?php echo $sort_classes.$extraClass; ?>'>

Resources