woocommerce function to get svg code product icon - php

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>

Related

ACF if have_rows( ) not returning anything

I'm trying to use a repeater field and can't seem to get it to work. I think it's an issue with my if statement because if I remove the while loop and try echo out anything from <?php if( have_rows($aboutInfo['cards']): ?> I get nothing. I've tried without the ID, and a hardcoded ID as the 2nd param. Also, just to test I did <?php if( !have_rows($aboutInfo['cards']): ?> and was able to get something to echo out.
The print_r above the if statement displays the array.
<?
/*
Template Name: 01-Homepage
*/
get_header(); ?>
<? $aboutInfo = get_field( 'about' ) ?>
<?$postid = get_the_ID(); ?>
<div class="row">
<div class="columns small-12 medium-7">
<h2>
<?= $aboutInfo['title'] ?>
</h2>
<p class="lead"> <?= $aboutInfo['content'] ?></p>
<pre><?php print_r($aboutInfo['cards']) ?></pre>
<?php if( have_rows($aboutInfo['cards'], $postid) ): ?>
<?php while(have_rows($aboutInfo['cards'])) : the_row(); ?>
<?php $image = get_sub_field('image') ?>
<p><?= $image['url'] ?></p>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?>
Here is what my ACF looks like
I think you are doing it wrong. There are so many bugs in your code. check
https://www.advancedcustomfields.com/resources/group/ and have_rows() the first param need to be selector. check below code.
<?php
/* Template Name: 01-Homepage */
get_header();
$aboutInfo = get_field( 'about' );
$postid = get_the_ID();
if( have_rows('about') ):
$title = get_sub_field('title');
$content = get_sub_field('content');
?>
<div class="row">
<div class="columns small-12 medium-7">
<?php while( have_rows( 'about' ) ): the_row(); ?>
<h2><?php echo $title; ?></h2>
<p class="lead"><?php echo $content; ?></p>
<?php if( have_rows( 'cards' ) ): while( have_rows( 'cards' ) ) : the_row(); ?>
<?php $image = get_sub_field( 'image' ); ?>
<img src="<?php echo $image['url']; ?>" />
<?php endwhile; endif;
endwhile; ?>
</div>
</div>
<?php endif;
get_footer(); ?>
The issues was that I created a group called "about" and the "cards" were nested in that group and to access that field I needed to use "about_cards".
<?
/*
Template Name: 01-Homepage
*/
get_header(); ?>
​
<?php while ( have_posts() ) : the_post();
​
// group field
$about = get_field( 'about' );
if ( !empty( $about ) ) { ?>
​
<div class="row">
<div class="columns small-12 medium-7">
​
<?php if ( !empty( $about['title'] ) ) { ?>
<h2><?php echo esc_html( $about['title'] ); ?></h2>
<?php }
if ( !empty( $about['content'] ) ) { ?>
<p class="lead"><?php echo wp_kses_post( $about['content'] ); ?></p>
<?php }
​
if( have_rows( 'about_cards' ) ) : // repeater
​
while ( have_rows( 'about_cards' ) ) : the_row();
​
$about_card_image = get_sub_field('image');
$about_card_title = get_sub_field('title');
$about_card_content = get_sub_field('content');
​
if ( !empty( $about_card_image ) ) {
echo wp_get_attachment_image( $about_card_image, 'medium' );
}
​
if ( !empty( $about_card_title ) ) {
echo '<h3>' . esc_html( $about_card_title ) . '</h3>';
}
​
if ( !empty( $about_card_content ) ) {
echo '<p>' . esc_html( $about_card_content ) . '</p>';
} ?>
​
<?php endwhile;
endif; ?>
</div>
</div>
​
<?php } // about field not empty ?>
​
<?php endwhile; // End of the loop. ?>
​
<?php get_footer(); ?>

Wordpress - How to show header on every page?

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>

add badge to thumbnail image

What I want to do on the front page where I have recent posts, is to add a small upper corner badge or ribbon that says "NEW" to the thumbnails of posts that were published on today's date. Below is my index.php which controls the front page layout:
<?php
/**
* The main template file.
*
* Used to display the homepage when home.php doesn't exist.
*/
?>
<?php
// Get options
$mts_options = get_option(MTS_THEME_NAME);
// Featured Section 1 settings ( slider is here) --------------------------------
$slider_enabled = ( $mts_options['mts_featured_slider'] === '1' ) ? true : false;
if ( empty($mts_options['mts_featured_slider_cat']) || !is_array($mts_options['mts_featured_slider_cat']) ) {
$mts_options['mts_featured_slider_cat'] = array('0');
}
$slider_cat = implode(",", $mts_options['mts_featured_slider_cat']);
// Featured Section settings --------------------------------------------------
$f2_title = $mts_options['mts_featured_section_2_title'];
if ( empty($mts_options['mts_featured_section_2_cat']) || !is_array($mts_options['mts_featured_section_2_cat']) ) {
$mts_options['mts_featured_section_2_cat'] = array('0');
}
$f2_cat= implode(",", $mts_options['mts_featured_section_2_cat']);
// Featured Section 3 settings --------------------------------------------------
get_header(); ?>
<div id="page">
<div class="article">
<?php if ( $slider_enabled && is_home() && !is_paged() ) { ?>
<section id="featured-section-1" class="featured-section featured-section-1-1 clearfix">
<div class="slider-container">
<div class="primary-slider-container clearfix loading">
<div id="slider" class="primary-slider">
<?php
$slider_full = 'best-featuredfull';
if ( empty( $mts_options['mts_custom_slider'] ) ) {
$my_query = new WP_Query('cat='.$slider_cat.'&posts_per_page='.$mts_options['mts_featured_slider_num']);
while ( $my_query->have_posts() ) : $my_query->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail($slider_full,array('title' => '')); ?>
<div class="slider-caption">
<?php if(isset($mts_options['mts_home_headline_meta_info']['enabled']) && $mts_options['mts_home_headline_meta_info']['enabled']){ ?>
<div class="sliderdate">
<?php if(isset($mts_options['mts_home_headline_meta_info']['enabled']['date']) == '1') { ?>
<span class="thetime updated"><?php the_time( get_option( 'date_format' ) ); ?></span>
<?php } ?>
</div>
<?php } ?>
<h2 class="slide-title"><?php the_title(); ?></h2>
</div>
<div class="post-day"><?php if (function_exists('wp_review_show_total')) wp_review_show_total(true, 'review-total-only'); ?></div>
</a>
</div>
<?php endwhile; wp_reset_postdata();
} else { ?>
<?php foreach( $mts_options['mts_custom_slider'] as $slide ) : ?>
<div>
<a href="<?php echo esc_url( $slide['mts_custom_slider_link'] ); ?>">
<?php echo wp_get_attachment_image( $slide['mts_custom_slider_image'], $slider_full, false, array('title' => '') ); ?>
<div class="slider-caption">
<div class="sliderdate"><?php echo esc_html( $slide['mts_custom_slider_title'] ); ?></div>
<h2 class="slide-title"><?php echo esc_html( $slide['mts_custom_slider_text'] ); ?></h2>
</div>
</a>
</div>
<?php endforeach; ?>
<?php } ?>
</div>
</div>
</div><!-- slider-container -->
</section>
<?php } ?>
<?php if( $mts_options['mts_featured_section_2'] === '1' && is_home() && !is_paged() ) {// featured section 2 ?>
<section id="featured-section-3" class="featured-section clearfix">
<?php if (!empty($f2_title)) { ?><h4 class="featured-section-title"><?php echo $f2_title;?></h4><?php } ?>
<?php
$f2_query = new WP_Query('cat='.$f2_cat.'&posts_per_page=4');
$f2_count = 1; if ($f2_query->have_posts()) : while ($f2_query->have_posts()) : $f2_query->the_post();
$f2_loop_params = best_mixed_layout_params( 1, 2, $f2_count );// see functions.php
$f2_clear_class = $f2_loop_params['clear_class'];
$f2_type_class = $f2_loop_params['box_class'];
$f2_loop_thumb = $f2_loop_params['thumb'];
$f2_show_excerpt = $f2_loop_params['show_excerpt'];
$f2_show_author = $f2_loop_params['show_author'];
$begin_f2_extra_wrappers = $f2_loop_params['open_wrappers'];
$end_f2_extra_wrappers = $f2_loop_params['close_wrappers'];
?>
<article class="post-box latestPost mixed <?php echo $f2_type_class;?> <?php echo $f2_clear_class;?>">
<?php echo $begin_f2_extra_wrappers; ?>
<div class="post-img">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="nofollow">
<?php the_post_thumbnail($f2_loop_thumb,array('title' => '')); ?>
</a>
</div>
<div class="post-data">
<div class="post-data-container">
<header>
<h2 class="title post-title"><?php the_title(); ?></h2>
<?php mts_the_postinfo(); ?>
</header>
<?php if ($f2_show_excerpt) : ?>
<div class="post-excerpt">
<?php echo mts_excerpt(40); ?>
</div>
<?php endif; ?>
</div>
</div>
<?php echo $end_f2_extra_wrappers; ?>
</article><!--.post-box-->
<?php $f2_count++; endwhile;
endif; wp_reset_postdata();
?>
</section><!--#featured-section-3-->
<?php } ?>
<div id="content_box">
<?php $latest_posts_used = false;
if ( !empty( $mts_options['mts_featured_categories'] ) ) {
foreach ( $mts_options['mts_featured_categories'] as $section ) {
$category_id = $section['mts_featured_category'];
$featured_category_layout = $section['mts_featured_category_layout'];
$posts_num = $section['mts_featured_category_postsnum'];
if ( $category_id === 'latest' && ! $latest_posts_used ) {
$latest_posts_used = true;
$fc_section_class = ( in_array( $featured_category_layout, array( 'vertical', 'mixed' ) ) ) ? '' : ' '.$featured_category_layout;
?>
<section id="latest-posts" class="clearfix<?php echo $fc_section_class ?><?php //echo $fc_section_no_gap ?>">
<h4 class="featured-section-title"><?php _e( "Latest", "best" ); ?></h4>
<?php $j = 1; if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php best_the_homepage_article( $featured_category_layout, $j, true );?>
<?php $j++; endwhile; endif; ?>
<!--Start Pagination-->
<?php if (isset($mts_options['mts_pagenavigation_type']) && $mts_options['mts_pagenavigation_type'] == '1' ) { ?>
<?php $additional_loop = 0; mts_pagination($additional_loop['max_num_pages']); ?>
<?php } else { ?>
<div class="pagination pagination-previous-next">
<ul>
<li class="nav-previous"><?php next_posts_link( '<i class="fa fa-chevron-left"></i> '. __( 'Previous', 'best' ) ); ?></li>
<li class="nav-next"><?php previous_posts_link( __( 'Next', 'best' ).' <i class="fa fa-chevron-right"></i>' ); ?></li>
</ul>
</div>
<?php } ?>
<!--End Pagination-->
</section><!--#latest-posts-->
<?php } elseif ( $category_id !== 'latest' && !is_paged() ) {
$fc_section_class = ( in_array( $featured_category_layout, array( 'vertical', 'mixed' ) ) ) ? '' : ' '.$featured_category_layout;
?>
<section class="featured-section clearfix<?php echo $fc_section_class ?>">
<h4 class="featured-section-title"><?php echo get_cat_name($category_id); ?></h4>
<?php $cat_query = new WP_Query('cat='.$category_id.'&posts_per_page='.$posts_num); ?>
<?php $j = 1; if ($cat_query->have_posts()) : while ($cat_query->have_posts()) : $cat_query->the_post(); ?>
<?php best_the_homepage_article( $featured_category_layout, $j );?>
<?php $j++; endwhile; endif; wp_reset_postdata();?>
</section>
<?php } ?>
<?php } ?>
<?php } ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
What I have is this: to check the date and display the badge (not including css) but not sure where to input it exactly:
<?php
$days_old = (current_time(timestamp) - get_the_time('U') - (get_settings('gmt_offset') *3600 ) ) / (60*60*24);
if ($days_old <=1) {
echo <div class="post-badge">NEW</div>;
}
?>
I want to insert it into The Latest Post Section. Any help would be greatly appreciated:)

PHP Conditional Issue

I have a small problem. And I am not sure why am I getting this problem. See what I did was I limited a page to "Public Member" and I want this page to be access Library members too because library members would have access to all pages. So what I did was I added a simple condition like this:
if ( $user_role == "library-Member" && $pageLevel == "public-member" ) {
$displayPost = "true";
}
And in my post I added this condition
if ( $displayPost == "true" ) {
.. execute html stuff ..
}
else {
wp_redirect( 'http://sample.org/restricted-area' );
exit;
}
But somehow the condition is not working. I have also checking var_dump($displayPost) the output is true
Can someone help me please? Thanks.
EDIT
Image Samples:
1. Sample 1
2. Sample 2
CODE:
<?php
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if ( !is_user_logged_in() ) { $user_role = array( 'public-member' ); }
$pageLevel = get_post_meta( $post->ID, 'user_type', true );
if ( $user_role == "library-Member" && $pageLevel == "public-member" ) {
$displayPost = "true";
}
if ( $displayPost == "true" ) {
?>
<div class="main wrap cf">
<div class="row">
<div class="col-8 main-content">
<?php if (have_posts()): the_post(); endif; // load the page ?>
<div id="post-<?php the_ID(); ?>" <?php post_class('page-content'); ?>>
<?php if (Bunyad::posts()->meta('page_title') != 'no'): ?>
<header class="post-header">
<?php if (has_post_thumbnail()): ?>
<div class="featured">
<a href="<?php $url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full'); echo $url[0]; ?>" title="<?php the_title_attribute(); ?>">
<?php if ((!in_the_loop() && Bunyad::posts()->meta('layout_style') == 'full') OR Bunyad::core()->get_sidebar() == 'none'): // largest images - no sidebar? ?>
<?php the_post_thumbnail('main-full', array('title' => strip_tags(get_the_title()))); ?>
<?php else: ?>
<?php the_post_thumbnail('main-slider', array('title' => strip_tags(get_the_title()))); ?>
<?php endif; ?>
</a>
</div>
<?php endif; ?>
<h1 class="main-heading">
<?php the_title(); ?>
</h1>
</header><!-- .post-header -->
<?php endif; ?>
<?php Bunyad::posts()->the_content(); ?>
</div>
</div>
<?php Bunyad::core()->theme_sidebar(); ?>
</div> <!-- .row -->
</div> <!-- .main -->
<?php
} else {
wp_redirect( 'http://sample.org/restricted-area' );
exit;
}
?>

Displaying attributes in woocommerce

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 } ?>

Categories