Woocommerce - could someone give me please a real world example with "WC_Product_Query" in a loop like WP_Query - woocommerce-theming

i'm developing a custom Woocommerce theme, i want to use "WC_Product_Query" with html tag like the example below, in a custom loop, to show custom woocommerce layout product in home page, seems like i tested it. it doesn't work. Since i don't master it, i'd like to see how it should be in action with HTML, because they say to not use the "WP_Query", it could break in future update of Woocommerce. Any help would be appreciated.
<?php
$args = array(
'post_type' => 'agenda',
'posts_per_page' => 2,
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
$agenda = new WP_Query($args);
while ($agenda->have_posts()) {
$agenda->the_post(); ?>
<div class="wapper_agenda">
<h3><?php the_title();?>
</div>
<?php }
?>

few days ago, i was asking how to make a custom loop with woocommerce to design the way i want a product to output in the front-end of the site or anywhere in the theme, without "WP_Query". After some search and practice, i found i could do this with "wc_get_products" in a foreach loop like the code below, you can output anything, just print the variable var_dump($products), and check the woocommerce documentation: https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
<section class="container-fluid container_product_home">
<div class="container">
<?php
// get the category Luxury
$args = array(
'post_type' => 'product',
'category' => array( 'luxury')
);
$products = wc_get_products( $args );
if ($products) {?>
<?php
foreach ($products as $prod) { ?>
<div class="row">
<div class="col-sm-6">
<?php
$val_img = $prod->image_id;
echo wp_get_attachment_image( $val_img, 'full', ["class" => "img-fluid"]); ?>
</div>
<div class="col-sm-6">
<div class="second_col_prod">
<div class="product_home" style="padding-top:10px;">
<p><?php echo $prod->description;?></p>
</div>
<div class="btn_product">
<a class="btn bnt default btn-primary btn-block btn_prod"href="<?php echo get_permalink( $prod->post->ID ) ?>">EXPLORE THIS </a>
</div>
</div>
</div>
</div>
<?php }
?>
<?php }
?>
</div>
</section>

Related

Cakephp Form helper not working in jQuery append

I am trying to put a div into append but I am always getting SyntaxError: missing ) after argument list
My div look like
<div class='col-md-6'>
<div class='form-group'>
<?= $this->Form->control('user_id', ['options' => $users,'class'=>'form-control']);?>
</div>
<div class='form-group'>
<?= $this->Form->control('phone_number',['class'=>'form-control']); ?>
</div>
</div>
I have tried in append like below
$('#cform').append("<div class='col-md-6'><div class='form-group'><?= $this->Form->control('user_id', ['options' => $users,'class'=>'form-control']);?></div><div class='form-group'><?= $this->Form->control('phone_number',['class'=>'form-control']); ?></div></div>");
I am not getting any syntax error here, How can I solve this ?
You can Use back tick `
$('#cform').append(`<div class='col-md-6'><div class='form-group'><?= $this->Form->control('user_id', ['options' => $users,'class'=>'form-control']);?></div><div class='form-group'><?= $this->Form->control('phone_number',['class'=>'form-control']); ?></div></div>`);
Back tick may be not support in IE, So you can use "\" before space
Example :
<div class="form-group">\
<?= $this->Form->control('user_id', ['options' => $users,'class'=>'form-control']);?>\
</div>\
<div class="form-group">\
<?= $this->Form->control('phone_number',['class'=>'form-control']); ?>\
</div>\

In my <a> tag, there are lots of code HTML. How to add Html link (CakePHP)

My code:
<a href="#">
<div class="list_content">
<p class="title"><?php echo $note['Note']['title']; ?></p>
<p class="create_at"><?php echo $note['Note']['create_at'] ?></p>
<p> <?php echo substr($note['Note']['content'], 0,100) . '...' ?></p>
</div>
</a>
How to add <?php echo $this->Html->link('...') ?> in CAKEPHP 2.x
If you want to insert HTML element in any HTML helper, you have to add 'escape' => false. Check the document https://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link
Simple Example:
$this->Html->link('<b>My Content</b>','#',[
'escape' => false
]);
For you case:
$this->Html->link(
$this->Html->div('list_content',
$this->Html->para('title',$note['Note']['title']).
$this->Html->para('create_at',$note['Note']['create_at']).
$this->Html->para(null,substr($note['Note']['content'], 0,100) . '...')
),
'#',
['escape' => false]
);
If you are going to use Aman's answer, remember that by setting 'escape' => false you are disabling a default security feature. So you probably want to make sure you then escape any user input using the h() method:-
$this->Html->link(
$this->Html->div('list_content',
$this->Html->para('title', h($note['Note']['title'])).
$this->Html->para('create_at', h($note['Note']['create_at'])).
$this->Html->para(null, substr(h($note['Note']['content']), 0,100) . '...')
),
'#',
['escape' => false]
);
If you've got a lot of markup you want inside your <a> tags it is sometimes simpler to use $this->Html->url() instead (and can lead to more readable code):-
<a href="<?= $this->Html->url('#') ?>">
<div class="list_content">
<p class="title"><?php echo $note['Note']['title']; ?></p>
<p class="create_at"><?php echo $note['Note']['create_at'] ?></p>
<p> <?php echo substr($note['Note']['content'], 0,100) . '...' ?></p>
</div>
</a>
The only real disadvantage I am aware of doing this second example is that you lose any functionality that you may add to $this->Html->link(), but I suspect this isn't a concern for the majority of users.

Advance Custom Fields - All fields from one field group in one frontent array?

I must stres out that i’m not a developer so i might use some “unsupported” terms :).
Ok the issue, i have created custom post type called Firm. Also i have created field group with 7 fields (text fields mainly, including website URL field and Google map field) and i have made template that displays those fields on frontend page. Once it’s saved all data is saved in database and new post under post type Firm is created. So that all works great. The main problem/question is:
How can i display all new posts in that post type (Firm) on one page? I know i must create some loop, array for those posts, but as i said i’m not developer so i’m kind of stuck with this one.
If someone could give me a hint or some link, or any kind of pointers so i could figure out in what direction to go. Thanks in advance for your answers.
You can refer Wordpress Codex.
Check sample code given below
$args = array( 'post_type' => 'product', 'posts_per_page' => 10 ); // for more parameter check link http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
// Displays Advanced custom field value
the_field('field-name');
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
Ok thanks so far, thank you #Arun, i have come up with this:
<?php
$args = array( 'post_type' => 'company', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
echo '<p>' . '<span>Official Wesite</span>' . '<span> : </span>' . get_field('web_site') . '</p>';
echo '</div>';
endwhile; ?>
<?php
$location = get_field('location');
if( !empty($location) ):
?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
</div>
<?php endif; ?>
So im strugling now with how to incorporate this google map in loop. I do have title and website link for all posts but i need to have maps for each post as well.
Ok this is what i have at the end, and it's working.
<?php
$args = array( 'post_type' => 'company', 'posts_per_page' => 15 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="boxy">
<div class="acf_company_name">
<h5>Company Name: </h5>
<p><?php the_field('company_name'); ?></p>
</div>
<div class="acf_full_name">
<h5>Full Name: </h5>
<?php the_field('full_name'); ?>
</div>
<div class="acf_vat">
<h5>VAT: </h5>
<?php the_field('vat'); ?>
</div>
<div class="acf_email">
<h5>E-mail: </h5>
<?php the_field('email'); ?>
</div>
<div class="acf_website">
<h5>Official website: </h5>
<?php the_field('website'); ?>
</div>
<?php if ( get_field('logo_company') ) : ?>
<div class="acf_logo_company"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php $image = get_field(logo_company); ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /></a></div>
<?php endif; ?>
<div class="acf_company_location"><?php $location = get_field('company_location');?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>" data-lng="<?php echo $location['address']; ?>"></div>
</div></div>
<div class="acf_company_location">
<h5>Company location: </h5>
<?php the_field('company_location'); ?>
</div>
</div>
<?php endwhile; ?>

Order WordPress posts by first letter in title not first number

I have a custom post type that's used to display properties on a client website. I am sorting the posts by the title but want to ignore any numbers included in that title.
so to clarify I would like to sort posts by the first letter [A-Z] not [0-9, A-Z].
This is my current loop.
<section id="grid" class="project clearfix">
<div class="wrapper">
<ul id="project" class="clearfix">
<?php
// select our project post types
$wptricckspost = array( 'post_type' => 'project',
'orderby'=> 'title',
'order' => 'ASC' );
// Create the new loop
$loop = new WP_Query( $wptricckspost );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<li class="item">
<header>
<h2><?php the_title(); ?></h2>
</header>
<article>
<?php the_content(); ?>
// Other html stuff below...
If anyone could help me with an example it would be greatly appreciated.
Thanks.
An example of how I need these to sort is.
Air W1
Angel Court
Basinghall Avenue
1 Eagle Court
31 Gresham Street
5 Hanover Square
Kings Quarter
LCW...

How to get controller view inside an element?

I have created an element having a text field(Status Bar same as Facebook) which i extended in home/display view.
Code for element:
<?php
echo $this->Html->css('notes');
echo $this->Html->script(array('jquery','jquery.elastic','notes'));
?>
<div class="notescontainer">
<div class="share">Share:</div>
<div class="notes">Notes</div>
<div class="loading"></div>
<div class="img_top"></div>
<div class="text_status">
<?php
echo $this->form->create('Note', array('action' => 'notes'));
echo $this->form->input('notes',array('class' => 'notesbar', 'label'=>'', 'placeholder' => 'Write down you notes ...'));
?>
</div>
<div class="button_outside_border" id="share">
<div class="button_inside_border">
<?php
echo $this->form->submit('Share',array('alt' => 'Add', 'class' => 'sharebutton'));
echo $this->form->end();
?>
</div>
</div>
<div class="clear"></div>
<div class="load_status_out"></div>
</div>
I want that when user shares the status it should re-appear on the same page that is display/home and for that i created a controller for retrieving the status entered by the user.
Controller:
<?php public function notes()
{
$this->set('allnotes', $this->Note->find('all'));
}?>
Controller View:
<?php
foreach($allnotes as $viewnotes):
{
echo $viewnotes['Note']['notes'];
echo "<br>";
}
endforeach;
?>
Now i want this controller view to be displayed on home/display view. Is there any way i can extend it in that element? I don`t want to add it directly on my home/display view as it already fully loaded.
You can use requestAction() to accomplish this, but in my experience it is buggy and a sign of design failure if you need it.
Alternatively you can turn all your view into elements, which can be embedded in each other in any arbitrary way. Then you can just call some shared code in the controller to populate all required models.

Resources