If statement stops working after 9 items - php

Within woo commerce I am pulling all of the featured items into the home page. Within the featured Items I have an if statement nested so that it checks to see what category each featured item is in.
I am doing so so that if its in the category flower I can ad the suffix OZ to the price and within concentrates I can add the suffix GM to the price.
It works great with 9 items or less. Once more than 9 items are applied they are all displaying OZ no matter what the category.
There are a few other things in there checking for logged in vs not logged in. but here is the whole snippet
<!------ check cat -->
<?php
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
?>
<?php if ( in_array( 'flower', $categories ) ) { ?>
<div class="price_home">
<div class="product__inside__price price-box">
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo wp_kses_post($price_html); ?> oz</span>
<?php endif; ?>
</div>
</div>
<?php } elseif ( in_array( 'concentrates', $categories ) ) { ?>
<div class="price_home">
<div class="product__inside__price price-box">
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo wp_kses_post($price_html); ?> gm</span>
<?php endif; ?>
</div>
</div>
<?php } else { ?>
<div class="price_home">
<div class="product__inside__price price-box">
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo wp_kses_post($price_html); ?
</span>
<?php endif; ?>
</div>
</div>
<? } ?>
I simplified this code but as I stated in the comments. This is used to split out the featured products. So I'm not 100% sure this is the issue, but people were having an issue viewing so much code.

Related

Adding ACF to Woocommerce ul.products on Shop Page

I'm using WooCommerce and Advanced Custom Fields, where ACF group is set up with post type for products. I would like to add a couple of simple text fields to the products box below the product title, and it will display on all products.
I have looked and found the hook for this to be woocommerce_after_shop_loop_item_title
Image attached for visual description.
Like this I'm looking to add Address value ($location), Bedrooms value ($bed) and Bathrooms ($bath).
Please understand I am very new to PHP and still learning. I have tried to make an attempt, however I am unsure how to extract the field data from the product's post.
Any tips in the right direction to learn would really be much appreciated.
Thank you in advance.
add_action( 'woocommerce_after_shop_loop_item_title', 'woo_products_property', 1 );
function woo_products_property() {
?>
<div class="property_loop_bottom_sec">
<?php $location = get_field_object('address'); ?>
<?php if( ! empty( $location ) ) { ?>
<div class="feature">
<div class="value"><i class="et-pb-icon map-marker"></i><?php echo $location['value'];?></div>
</div>
<?php } ?>
<?php $bed = get_field_object('bedroom'); ?>
<?php if( ! empty( $bed ) ) { ?>
<div class="feature">
<div class="value"><i class="fas fa-bed"></i><?php echo $bed['value'];?></div>
<span>Bed</span>
</div>
<?php } ?>
<?php $bath = get_field_object('bathroom'); ?>
<?php if( ! empty( $bath ) ) { ?>
<div class="feature">
<div class="value"><i class="fas fa-bath"></i><?php echo $bath['value'];?></div>
<span>Bath</span>
</div>
<?php } ?>
</div>
<?php
} ?>
I revised your code. Try the below code.
function woo_products_property() {
global $post;
?>
<div class="property_loop_bottom_sec">
<?php
$location = get_field( 'address', $post->ID );
if( ! empty( $location ) ) { ?>
<div class="feature">
<div class="value"><i class="et-pb-icon map-marker"></i><?php echo $location['value'];?></div>
</div>
<?php }
$bed = get_field( 'bedroom', $post->ID );
if( ! empty( $bed ) ) { ?>
<div class="feature">
<div class="value"><i class="fas fa-bed"></i><?php echo $bed['value'];?></div>
<span>Bed</span>
</div>
<?php }
$bath = get_field( 'bathroom', $post->ID);
if( ! empty( $bath ) ) { ?>
<div class="feature">
<div class="value"><i class="fas fa-bath"></i><?php echo $bath['value'];?></div>
<span>Bath</span>
</div>
<?php } ?>
</div>
<?php }
add_action( 'woocommerce_after_shop_loop_item_title', 'woo_products_property', 1 );
Tested and works

How to display a specific category in wordpress post by id or other

I am trying to create a related post section at the bottom of each post. I am using YARPP plugin to generate the related posts. I want customise the a template to show, a thumbnail(Done), title(Done) and a specific category(Issue).
My issue is its displaying all assigned categories, I only want to display one child category. I have a speaker category with child categories of the names of the speakers.
Let me know if you need more information. Thanks
<h3>Related Posts</h3>
<?php if ( have_posts() ) : ?>
<div>
<?php
while ( have_posts() ) : the_post(); ?>
<div>
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail( $dimensions['size'], array( 'data-pin-nopin' => 'true' ) ); ?></a>
<a href="<?php the_permalink(); ?>" rel="bookmark norewrite" title="<?php the_title_attribute(); ?>">
</div>
<div>
<?php the_title(); ?>
<!-- How to add specific category -->
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if ( ! empty( $categories ) ) {
foreach( $categories as $category ) {
$term_id = '328';
$output .= '|| ' . esc_html( $category->name ) . '' . $separator;
}
echo trim( $output, $separator );
}
?>
<!-- How to add specific category END -->
</div>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No related photos.</p>
<?php endif; ?>
<?php $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 6, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) { setup_postdata($post); ?>
<div class="post">
<div class="post-title">
<h6 class="title"><?php the_title() ?></h6>
</div>
<div class="post-img" style="background-image: url(<?php the_post_thumbnail_url(); ?>)"></div>
</div>
<?php } wp_reset_postdata(); ?>

WooCommerce: Get meta fields from product attributes on product page

I want to display a specific product attribute on my product page in WooCommerce.
But I want to show not only the name but some extra information like a description and an image.
At the moment I'm using wc_get_product_terms for that:
<?php
global $product;
$values = wc_get_product_terms( $product->id, 'pa_attribute', array( 'fields' => 'all' ) ); if( $values ) : ?>
<div class="woocommerce-product-details>
<div class="content">
<ul class="list-unstyled">
<?php foreach ( $values as $term ) : ?>
<li class="">
<?php $icon = get_field('pa_attribute_icon', 'pa_attribute_'.$term->term_id); if( !empty($icon) ): ?>
<div class="highlight-img"><img src="<?php echo $icon['url']; ?>" alt="<?php echo $icon['alt']; ?>" /></div>
<?php endif; ?>
<div class="highlight-label"><?php echo $term->name; ?></div>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php endif; ?>
I have read, that wc_get_product_terms is deprecated. Is there a better/newer solution to display the attributes with the additional informations?
wc_get_product_terms is not deprecated. woocommerce_get_product_terms it is.

WooCommerce get sale price

I want to be able to show the current price and sale price if it exists within my WP_Query products loop. At the moment my code just replaces the current price with the sale price. I want an if statement that checks if the sale price is there or not. If it is I want to display both;
Here's my code;
<?php
$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 4,
);
$query = new WP_Query( $args );
?>
<?php if($query->have_posts()): ?>
<div class="container" id="best_sellers">
<div class="row">
<?php while( $query->have_posts() ): $query->the_post(); ?>
<?php $product = wc_get_product(get_the_ID());?>
<div class="col-md-3">
<div class="cont">
<a href="<?php the_permalink(); ?>">
<div class="image_cont">
<?php the_post_thumbnail(); ?>
</div>
<p class="sku"><?php echo $product->get_sku(); ?></p>
<p class="title"><?php the_title(); ?></p>
</a>
<div class="quantity">
<p>Quantity: </p>
<button class="increment">+</button>
<input type="text" value="1" min="1" max="<?php echo $product->get_stock_quantity(); ?>">
<button class="decrement">-</button>
</div>
<p class="price"><?php echo wc_price( wc_get_price_including_tax( $product ) ); ?></p>
Add to Basket
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php endif; ?>
If you look at the price class you can see my code for getting the price.
update: I have been looking through the WooCommerce templates files and found this;
<?php if ( $product->is_on_sale() ) : ?>
This works, I just need to be able to grab both prices separately as at the moment
<?php echo wc_price( wc_get_price_including_tax( $product ) ); ?>
just gets overwritten by the sale price
Do you mean regular and sale prices?
You can work with below code but this will only with simple product, it won't work on product variation.
global $product;
if( $product->is_on_sale() ) {
$sale_price = $product->get_sale_price();
}
$regular_price = $product->get_regular_price();
I managed to find this
<?php echo $product->get_price_html(); ?>
It does everything I wanted without having to make an if statement
If you want to get the sale price with taxes, try this:
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price()) );
You will have to prefix the currency which can be achieved via get_woocommerce_currency_symbol()

Open wordpress post in a different page

I have two separate pages showing a list of different categories linking to the full post in wordpress. These both currently open on single.php in my theme but The categories need to be styled differently on each page.
I have created the template page but do not know how to open posts on another page other than single.php
to simplify: how do I open posts on another version of single.php
the code I have that opens the full posts are:
<?php // PAGE LINK/TITLE
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=case-study&posts_per_page=10");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
?>
<div class="serve-inner-split">
<div id="case-split">
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
<div id="case-right" class="serve-grey">
<?php
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
</div>
</div>
<?php endforeach;
}
}
?>
Hello :) I would achieve this by creating custom post type for each case you need to display the single post differently. You may refer to WP Codex https://codex.wordpress.org/Post_Types on this case. Eventually it will allow you to create as much single post templates as you want.
I'd create extra category fields. In functions.php add
add_action ( 'edit_category_form_fields', 'mytheme_extra_category_fields');
add_action ( 'category_add_form_fields', 'mytheme_extra_add_category_fields');
if ( ! function_exists( 'mytheme_extra_category_fields' ) ){
function mytheme_extra_category_fields( $tag ) {
$t_id = $tag->term_id;
$cat_meta = get_option( "category_$t_id");
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="extra1"><?php esc_attr_e('Blog Layout', 'mytheme'); ?></label></th>
<td>
<select name="Cat_meta[blog_layout]">
<?php
echo '<option value="layout1" '.selected( $cat_meta['blog_layout'], 'layout1', false).'>'.esc_html__('Layout 1', 'mytheme').'</option> ';
echo '<option value="layout2" '.selected( $cat_meta['blog_layout'], 'layout2', false).'>'.esc_html__('Layout 2', 'mytheme').'</option> ';
?>
</select>
</td>
</tr>
<?php
}
}
if ( ! function_exists( 'mytheme_extra_add_category_fields' ) ){
function mytheme_extra_add_category_fields( $tag ) {
$t_id = (is_object($tag))?$tag->term_id:'';
$cat_meta = get_option( "category_$t_id");
?>
<div class="form-field">
<label for="extra1"><?php esc_attr_e('Blog Layout', 'mytheme'); ?></label></th>
<select name="Cat_meta[blog_layout]">
<?php
echo '<option value="layout1" '.selected( $cat_meta['blog_layout'], 'layout1', false).'>'.esc_html__('Layout 1', 'mytheme').'</option> ';
echo '<option value="layout2" '.selected( $cat_meta['blog_layout'], 'layout2', false).'>'.esc_html__('Layout 2', 'mytheme').'</option> ';
?>
</select>
</div>
<?php
}
}
add_action ( 'edited_category', 'mytheme_save_extra_category_fileds');
add_action ( 'created_category', 'mytheme_save_extra_category_fileds');
if ( ! function_exists( 'mytheme_save_extra_category_fileds' ) ){
function mytheme_save_extra_category_fileds( $term_id ) {
if ( isset( $_POST['Cat_meta'] ) ) {
$t_id = $term_id;
$cat_meta = get_option( "category_$t_id");
$cat_keys = array_keys($_POST['Cat_meta']);
foreach ($cat_keys as $key){
if(isset($_POST['Cat_meta'][$key])){
$cat_meta[$key] = $_POST['Cat_meta'][$key];
}
}
update_option( "category_$t_id", $cat_meta );
}
}
}
This should output the select 'Blog layout' when you add categories to your posts. Then in your index.php add
$cat_id = get_query_var('cat');
$cat_data = get_option("category_$cat_id");
<?php if(isset($cat_data['blog_layout']) && $cat_data['blog_layout'] == 'layout1'): ?>
\\Your layout1 here
<?php else: ?>
\\Your layout2 here
<?php endif; ?>
This should work.
To fix this issue I replaces the entire single.php file with this code:
<?php
if (in_category('2')) {include (TEMPLATEPATH . '/page-case-study-post.php');
}
else { include (TEMPLATEPATH . '/page-services-post.php');
}
?>;
sending either categories to two different pages depending on what its set to.

Categories