I am currently customizing my content-single-product.php template, and I need to add the simple_add_to_cart. It shows up fine and the quantity box seems to work, but when I click the add to cart button nothing happens.
Here is my full template code:
<?php
/**
* woocommerce_before_single_product hook.
*
* #hooked wc_print_notices - 10
*/
do_action( 'woocommerce_before_single_product' );
if ( post_password_required() ) {
echo get_the_password_form();
return;
}
?>
<div class="product_wrap">
<div class="product_image_wrap">
<div class="thumbnails navigator-2">
<?php
global $product;
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id )
{
//echo Image instead of URL
echo '<div class="slider_thumb">' . wp_get_attachment_image($attachment_id, 'thumbnail') . '</div>';
}
?>
</div>
<div class="main_image">
<div class="product-slider prod-nav">
<?php
global $product;
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id )
{
//echo Image instead of URL
echo '<div class="prod_img">' . wp_get_attachment_image($attachment_id, 'full') . '</div>';
}
?>
</div>
</div>
</div>
<div class="product_content_wrap">
<div itemscope itemtype="<?php echo woocommerce_get_product_schema(); ?>" id="product-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="summary entry-summary">
<h3><?php echo get_post_meta( $post->ID, 'comic_series', true ); ?></h3>
<h1><?php woocommerce_template_single_title(); ?></h1>
<h2><?php echo get_post_meta( $post->ID, 'author_info', true ); ?></h2>
<div class="product_description">
<?php woocommerce_product_description_tab(); ?>
</div>
<div class="add_to_cart">
<?php woocommerce_simple_add_to_cart(); ?>
</div>
</div><!-- .summary -->
</div>
</div>
</div>
<div class="product_details_wrap">
<div class="product_details">
<div class="details_content">
<h4>Details</h4>
<ul>
<li><p><span>Published:</span> <?php echo get_post_meta( $post->ID, 'published', true ); ?></p></li>
<li><p><span>Writer:</span> <?php echo get_post_meta( $post->ID, 'writer', true ); ?></p></li>
<li><p><span>Penciller:</span> <?php echo get_post_meta( $post->ID, 'penciller', true ); ?></p></li>
<li><p><span>Cover Artist:</span> <?php echo get_post_meta( $post->ID, 'cover_artist', true ); ?></p></li>
</ul>
<ul>
<li><p><span>Format:</span> <?php echo get_post_meta( $post->ID, 'format', true ); ?></p></li>
<li><p><span>Price:</span> <?php echo $product->get_price(); ?></p></li>
<li><p><span>UPC:</span> <?php echo $product->get_sku(); ?></p></li>
<li><p><span>FOC Date:</span> <?php echo get_post_meta( $post->ID, 'foc_date', true ); ?></p></li>
</ul>
</div>
</div>
</div>
Obviously I am loading a few custom fields for the product, but it is a simple product. I am owndering what I may be missing here...
As it turns out its a conflict with Advanced Custom Fields! I am going to research this a little more, and possibly post a new question. Pretty crazy!
Related
I have add a new field via admin panel in products product_icon
where I put an SVG icon code. I need to use this SVG plugin. I have put into woocommerce/includes/abstracts/abstract-wc-product.php new function.
public function get_icon( $context = 'view' ) {
return $this->get_prop( 'icon', $context );
}
when I put it into a while loop where I have list products name, prices, etc new function get_icon() I have null. In SQL post meta meta key is ok "product_icon" for a correct product with the correct content (SVG code)
My listing Code:
<?php
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$product = wc_get_product( get_the_ID() );
?>
<div class="eb-woo-products-col">
<?php if ( 'grid' === $layout && 'grid-preset-3' === $gridPreset ) { ?>
<div class="producttabwhite eb-woo-product">
<?php } else { ?>
<div class="producttabblue eb-woo-product">
<?php } ?>
<div id="mydiv" class="eb-woo-product-image-wrapper" style="margin-top:2.5rem;margin-right:1.25rem;">
<?php echo wp_kses_post( $product->get_image( 'woocommerce_thumbnail' ) ); ?>
<?php if ( 'grid' === $layout ) { ?>
<div class="eb-woo-product-overlay">
<div class="eb-woo-product-button-list">
<?php // woocommerce_template_loop_add_to_cart(); ?>
</div>
</div>
<?php } ?>
</div>
<div class="eb-woo-product-content-wrapper1">
<div class="ml40px mt33px col-sm-8">
<?php if ( 'grid' === $layout && 'grid-preset-3' === $gridPreset ) { ?>
<p class="producttitle">
<a class="producttitle" href="<?php echo esc_url( get_permalink() ); ?>"><?php echo get_the_title(); ?></a>
</p>
<?php } else { ?>
<p class="producttitlewhite">
<a class="producttitlewhite" href="<?php echo esc_url( get_permalink() ); ?>"><?php echo get_the_title(); ?></a>
</p>
<?php } ?>
<?php if ( 'grid' === $layout && 'grid-preset-3' === $gridPreset ) { ?>
<p class="productprice">
<?php if ( $showPrice ) { ?>
<p><?php echo $product->get_price_html(); ?></p>
<p>Icon code<?php echo $product->get_icon(); ?></p>
<div style="display:block" class="mt-4 eb-woo-product-button-list"><?php woocommerce_template_loop_add_to_cart(); ?></div>
<?php } ?>
</p>
<?php } else { ?>
<p class="productpricewhite">
<?php if ( $showPrice ) { ?>
<p><?php echo $product->get_price_html(); ?></p>
<div style="display:block" class="mt-4 eb-woo-product-button-list"><?php woocommerce_template_loop_add_to_cart(); ?></div>
<?php } ?>
</p>
<?php } ?>
</div>
</div>
</div>
</div>
<?php
}
wp_reset_postdata();
} else {
?>
<p><?php _e( 'No product found', 'essential-blocks' ); ?></p>
<?php
}
?>
Well, you don't have to edit woocommerce/includes/abstracts/abstract-wc-product.php file to fetch the meta key and the get_prop function doesn't work that way that you're assuming.
You can simply use get_post_meta function to fetch your meta key value for the product.
so you just need to replace <p><?php echo $product->get_icon(); ?></p>
with the below code:
<p><?php echo get_post_meta( $product->get_id(), 'product_icon', true ); ?></p>
My website started showing this strange error recently which I have been unable to resolve. When I try to view a product, it returns a critical error.
I’ve done some troubleshooting and I realized the theme is the culprit. I have also checked the error log and I saw that this specific line of code is causing the error:
Uncaught Error: Call to a member function is_in_stock() on string in /home/u306409103/domains/apdbrestore.com/public_html/wp-content/themes/custom-theme/woocommerce/single-product.php:45 Stack trace: #0
When you check that line, it says:
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
To be specific the error is pointing towards the first line:
<?php if( !$product->is_in_stock() ) : ?>
I know the problem is coming from here but don’t know what next to do. What statement can I use here to do away with this error?
Thanks
Full code
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
get_header( 'shop' ); ?>
<?php
/**
* woocommerce_before_main_content hook.
*
* #hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* #hooked woocommerce_breadcrumb - 20
*/
do_action( 'woocommerce_before_main_content' );
?>
<?php
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
?>
<div class="product-single">
<div class="container">
<div class="row">
<div class="col-md-7">
<div class="product-single-left product-contain">
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="product-single-holder">
<div class="product-single-gallery">
<div class="product-single-image active" data-img="<?php echo $img; ?>" style="background-image: url(<?php echo $img; ?>);"></div>
<?php global $product;
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id ) { ?>
<div class="product-single-image" data-img="<?php echo wp_get_attachment_url( $attachment_id ); ?>" style="background-image: url(<?php echo $image_link = wp_get_attachment_url( $attachment_id ); ?>);"></div>
<?php } ?>
</div>
</div>
<div class="product-single-featured">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
</div>
</div>
</div>
<div class="col-md-5">
<div class="product-single-content">
<h3><?php echo get_the_title(); ?></h3>
<div class="product-single-price">
<div class="home-sale-price">
<?php if( $product->get_sale_price() ) : ?>
<div class="home-sale-less">
Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
<?php if(get_field('dp_and_pay')): ?>
<div class="product-single-term"><?php echo get_field('dp_and_pay'); ?></div>
<?php endif; ?>
</div>
<div class="product-single-desc">
<?php echo apply_filters( 'the_content', $product->get_short_description() ); ?>
</div>
<i class="fas fa-plus-square"></i>Add to Cart
</div>
<?php while ( have_posts() ) : the_post(); ?>
<?php //wc_get_template_part( 'content', 'single-product' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
</div>
</div>
<div class="home-sale home-new">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-10">
<div class="header-excerpt">
<?php $salehead = get_field('new_arrivals_content',8); ?>
<h3>Recommmended Items</h3>
<div class="header-excerpt-text"><?php echo $salehead['content']; ?></div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="home-sale-slider">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'order_by' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
$product = wc_get_product(get_the_ID());
?>
<div class="home-sale-holder product-contain">
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="home-sale-top">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
<i class="fas fa-plus-square"></i>Add to cart
</div>
<div class="home-sale-bottom">
<a href="<?php echo get_permalink(); ?>">
<h5><?php echo get_the_title(); ?></h5>
<div class="home-sale-price">
<?php if( $product->get_sale_price() ) : ?>
<div class="home-sale-less">
Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
</a>
</div>
</div>
<?php endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</div>
</div>
</div>
</div>
View All Products
</div>
</div>
<?php
/**
* woocommerce_after_main_content hook.
*
* #hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action( 'woocommerce_after_main_content' );
?>
<?php
/**
* woocommerce_sidebar hook.
*
* #hooked woocommerce_get_sidebar - 10
*/
do_action( 'woocommercve_sidebar' );
?>
<?php get_footer( 'shop' );
/* Omit closing PHP tag at the end of PHP files to avoid "headers already sent" issues. */
Finally got the solution to this problem and in case anyone stumbles across this, all I had to do was to update how to get the reference post ID because the existing one is deprecated already.
<?php global $product;
$product = wc_get_product( $post->ID );
$stock_quantity = get_post_meta($post->ID, '_stock', true);
//var_dump($product->get_stock_status());
?>
<?php if ($product->get_stock_status() != 'instock') : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
Make sure that the $product is not a string or a boolean before checking for stock.
<?php if( is_object($product) && !$product->is_in_stock() ) : ?>
Or, you completly stop processing the template by bailing out at the top if no product was returned like this:
if (!is_object($product)) {
return;
}
Try to put this code, I updated your code a bit.
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
get_header( 'shop' );
global $product;
?>
<?php
/**
* woocommerce_before_main_content hook.
*
* #hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* #hooked woocommerce_breadcrumb - 20
*/
do_action( 'woocommerce_before_main_content' );
?>
<?php
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
?>
<div class="product-single">
<div class="container">
<div class="row">
<div class="col-md-7">
<div class="product-single-left product-contain">
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="product-single-holder">
<div class="product-single-gallery">
<div class="product-single-image active" data-img="<?php echo $img; ?>" style="background-image: url(<?php echo $img; ?>);"></div>
<?php
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id ) { ?>
<div class="product-single-image" data-img="<?php echo wp_get_attachment_url( $attachment_id ); ?>" style="background-image: url(<?php echo $image_link = wp_get_attachment_url( $attachment_id ); ?>);"></div>
<?php } ?>
</div>
</div>
<div class="product-single-featured">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
</div>
</div>
</div>
<div class="col-md-5">
<div class="product-single-content">
<h3><?php echo get_the_title(); ?></h3>
<div class="product-single-price">
<div class="home-sale-price">
<?php if( $product->get_sale_price() ) : ?>
<div class="home-sale-less">
Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
<?php if(get_field('dp_and_pay')): ?>
<div class="product-single-term"><?php echo get_field('dp_and_pay'); ?></div>
<?php endif; ?>
</div>
<div class="product-single-desc">
<?php echo apply_filters( 'the_content', $product->get_short_description() ); ?>
</div>
<i class="fas fa-plus-square"></i>Add to Cart
</div>
<?php while ( have_posts() ) : the_post(); ?>
<?php //wc_get_template_part( 'content', 'single-product' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
</div>
</div>
<div class="home-sale home-new">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-10">
<div class="header-excerpt">
<?php $salehead = get_field('new_arrivals_content',8); ?>
<h3>Recommmended Items</h3>
<div class="header-excerpt-text"><?php echo $salehead['content']; ?></div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="home-sale-slider">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'order_by' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
$new_product = wc_get_product(get_the_ID());
?>
<div class="home-sale-holder product-contain">
<?php if( !$new_product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="home-sale-top">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
<i class="fas fa-plus-square"></i>Add to cart
</div>
<div class="home-sale-bottom">
<a href="<?php echo get_permalink(); ?>">
<h5><?php echo get_the_title(); ?></h5>
<div class="home-sale-price">
<?php if( $new_product->get_sale_price() ) : ?>
<div class="home-sale-less">
Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
</a>
</div>
</div>
<?php endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</div>
</div>
</div>
</div>
View All Products
</div>
</div>
<?php
/**
* woocommerce_after_main_content hook.
*
* #hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action( 'woocommerce_after_main_content' );
?>
<?php
/**
* woocommerce_sidebar hook.
*
* #hooked woocommerce_get_sidebar - 10
*/
do_action( 'woocommercve_sidebar' );
?>
<?php get_footer( 'shop' );
I'm working on a Wordpress site and I have a header image on the homepage. It's only showing up on the homepage but I need it to show on every page. I found this code in the header.php file which I believe needs to be changed, but I'm not very familiar with php.
This is the code for the header image in the header.php file:
<?php $disable_page_title = get_post_meta( get_the_ID(), 'minimal_portfolio_page_title', true );
if( $disable_page_title !== 'on' ): ?>
<?php if( !is_front_page()): ?>
<section class="page-header jumbotron <?php if ( get_header_image() ) : ?>bg-image<?php endif; ?>" <?php if ( get_header_image() ) : ?> style="background-image:url('<?php echo esc_url( get_header_image() ); ?>');" <?php endif; ?>>
<?php if ( get_header_image() ) : ?><span class="bg-overlay"></span><?php endif; ?>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="title-wrap">
<?php if( is_page() || is_single() ){ ?>
<h2 class="page-title"><?php echo esc_html( get_the_title() ); ?></h2>
<?php } elseif( is_search() ){ ?>
<?php /* translators: %s: search term */
$page_title = sprintf( esc_html__( 'Search Results for: %s', 'minimal-portfolio' ), get_search_query() );
?>
<h2 class="page-title"><?php echo esc_html( $page_title ); ?></h2>
<?php }elseif( is_404() ){ ?>
<h2 class="page-title"><?php echo esc_html( 'Page Not Found: 404', 'minimal-portfolio' ); ?></h2>
<?php }elseif( is_home() ){ ?>
<h2 class="page-title"><?php single_post_title(); ?></h2>
<?php } else{
the_archive_title( '<h2 class="page-title">', '</h2>' );
}
if( $minimal_portfolio_breadcrumb_status ):
minimal_portfolio_breadcrumbs();
endif;
?>
</div>
</div>
</div>
</div>
</section>
<?php endif;
endif; ?>
Thank you!
Edit: This is how it shows on all pages but the home page. But I would like it to show the full header instead of only a section of it with the page title.
Header
This is the home page, where it shows the full header and how I'd like it to show on every page instead of how it does in the image above.
Home Header
You are not familiar with PHP so you can use this plugin for the header image.
https://wordpress.org/plugins/unique-headers/
I think its work for you
Remove the below-attached code from header.php
<?php $disable_page_title = get_post_meta( get_the_ID(), 'minimal_portfolio_page_title', true );
if( $disable_page_title !== 'on' ): ?>
<?php if( !is_front_page()): ?>
<section class="page-header jumbotron <?php if ( get_header_image() ) : ?>bg-image<?php endif; ?>" <?php if ( get_header_image() ) : ?> style="background-image:url('<?php echo esc_url( get_header_image() ); ?>');" <?php endif; ?>>
<?php if ( get_header_image() ) : ?><span class="bg-overlay"></span><?php endif; ?>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="title-wrap">
<?php if( is_page() || is_single() ){ ?>
<h2 class="page-title"><?php echo esc_html( get_the_title() ); ?></h2>
<?php } elseif( is_search() ){ ?>
<?php /* translators: %s: search term */
$page_title = sprintf( esc_html__( 'Search Results for: %s', 'minimal-portfolio' ), get_search_query() );
?>
<h2 class="page-title"><?php echo esc_html( $page_title ); ?></h2>
<?php }elseif( is_404() ){ ?>
<h2 class="page-title"><?php echo esc_html( 'Page Not Found: 404', 'minimal-portfolio' ); ?></h2>
<?php }elseif( is_home() ){ ?>
<h2 class="page-title"><?php single_post_title(); ?></h2>
<?php } else{
the_archive_title( '<h2 class="page-title">', '</h2>' );
}
if( $minimal_portfolio_breadcrumb_status ):
minimal_portfolio_breadcrumbs();
endif;
?>
</div>
</div>
</div>
</div>
</section>
<?php endif;
endif; ?>
And add this code in your page.php
<?php if( get_header_image() ) : ?>
<div class="header-banner">
<img src="<?php header_image(); ?>" width="<?php echo absint( get_custom_header()->width ); ?>" height="<?php echo absint( get_custom_header()->height ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>">
</div>
I've trying to display the attributes from size in woocommerce.
I'm using
<?php
$terms = get_terms("pa_size");
foreach ( $terms as $term ) {
echo "<p>. $term->name .</p>";
?>
However it just seems to break the page for some reason.
Full code
<div class="titleSP">
<h2><?php the_title(); ?></h2>
<h2><?php the_title(); ?></h2>
</div>
<div class="priceSP">
<?php echo $product->get_price_html(); ?>
<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
</div>
<?php
//code to pull in the attrs
$terms = get_terms("pa_size");
foreach ( $terms as $term ) {
echo "<p>. $term->name .</p>";
?>
<?php if($post->post_excerpt) { ?>
<div class="descriptionSP short">
<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ); ?>
</div>
<?php } ?>
I'm currently playing around with WOOCOMMERCE V2.0.13 and I'm trying to display each product from the current product category (e.g. Construction Products when on the Construction Page), I've managed to display the single products from within the current category but if the product is also in another category (e.g Construction and Enviroment) then the current category breaks and shows zero products either from Construction or Enviroment.
If I could get some advice/help on displaying products from the current category and allow it to work with products that are in multiple categories I'd trully apreciate the help and time.
I'm more than happy to recode this entire section to make it work, here is my code below please let me know if I've missed anything.
Thank you.
<ul class="products">
<?php
global $post, $product;
$categ = $product - > get_categories();
$categ2 = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $categ);
?>
<?php
global $product;
$args = array('post_type' = > 'product', 'posts_per_page' = > '999', 'product_cat' = > $categ2, );
$loop = new WP_Query($args);
while ($loop - > have_posts()): $loop - > the_post();
global $product;
?>
<li>
<a href = "<?php echo get_permalink(); ?>">
<?php
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail($post - > ID, apply_filters('single_product_large_thumbnail_size', 'shop_single'));
$image_title = esc_attr(get_the_title(get_post_thumbnail_id()));
$image_link = get_permalink($product_id);
$attachment_count = count($product - > get_gallery_attachment_ids());
echo apply_filters('woocommerce_single_product_image_html', sprintf('%s', $image_link, $image_title, $image), $post - > ID);
} else {
echo apply_filters('woocommerce_single_product_image_html', sprintf('<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src()), $post - > ID);
} ?>
</a>
<div>
<h3>
<?php the_title();?>
<span>
<?php
if ($price_html = $product - > get_price_html()) {
?>
<span class = "price">
<?php echo $price_html; ?>
</span>
<?php } ?>
</span>
</h3>
</div>
<div>
<p>
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt, 15);
?>
</p>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php if ( have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<div class="courses-main">
<ul class="products">
<?php while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php echo get_permalink(); ?>">
<?php
if ( has_post_thumbnail() ) {
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
$image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$image_link = get_permalink( $product_id );
$attachment_count = count( $product->get_gallery_attachment_ids() );
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src() ), $post->ID );
}
?>
</a>
<div>
<h3>
<?php the_title();?>
<span>
<?php if ( $price_html = $product->get_price_html()) { ?>
<span class="price"><?php echo $price_html; ?></span>
<?php } ?>
</span>
</h3>
</div>
<div>
<p>
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt,15);
?>
</p>
</div>
</li>
<?php endwhile;?>
</ul>
</div>
<?php woocommerce_product_loop_end(); ?>
<?php endif; ?>