how to customize shop page query by woocommerce hook? - woocommerce

I need a help regarding archive-product hook.
i am writing this code in functions.php file then the products will shown before a header(screenshot is given),
how i can display products in properly??
please help me!
function so_27975262_product_query( $q, $details = [] ){
$details = warehouse_detail();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post();
do_action('woocommerce_shop_loop');
$prod_img = wp_get_attachment_url(get_post_thumbnail_id(get_the_id()));
$product = wc_get_product(get_the_id());
$price = $product->get_price();
$werehouseDetails = unserialize(get_post_meta(get_the_ID(), 'warehouse_id', true));
$billing_state = get_user_meta(get_current_user_id(), 'billing_state', true);
$warehouse_qty = [];
foreach ($werehouseDetails as $werehouseDetail) {
if (in_array($werehouseDetail, $details)) {
$warehouse_qty = get_post_meta(get_the_ID(), 'warehouse_qty_' . $werehouseDetail, true);
if ($warehouse_qty > 0) { // Shifa End 20-10-22
$products = wc_get_template_part('content', 'product');
}
}
}
endwhile; wp_reset_query();
endif;
$q->set('query',$products);
}
add_action( 'woocommerce_product_query', 'so_27975262_product_query' );

Related

List All posts under its own category title WP Query

Hi Hope You All doing well, I implemented a search for posts that are connected to categories child and then the parent of that child everything works perfect in search but I want to bring the search post under its own category and not to repeat that category title again and again for the post title here is my code script and check screenshot here what I mean to say
screenshot before search https://prnt.sc/10kzkvp
screenshot after search https://prnt.sc/10kzmxt As you can see here I want to list posts inside its own category and not repeat the category title for its post, let me know if need more explanation
<?php
$args = array(
'post_type' => 'post',
'extend_where' => "(post_title like '%$s_word%')",
'posts_per_page' => -1,
'orderby'=> 'title',
'order' => 'ASC'
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
$icon_flag = false;
$child_c = "";
$parent_c = "";
while ( $the_query->have_posts() ) {
$the_query->the_post();
$split_title = explode(',', get_the_title(), 2);
$id = get_the_ID();
$current_cate = get_the_terms($id,'category');
$parent = $current_cate[0]->parent;
//load object for parent category
$parent_name = get_the_category_by_ID($parent);
if($parent_name != $parent_c && $parent != 0)
{
$parent_c = $parent_name;
?>
<h1 class="grand-p-title p-parent"><?php echo $parent_c; ?></h1>
<?php
}
if($current_cate[0]->name != $child_c )
{
$child_c = $current_cate[0]->name;
?>
<!-- <h1 class="grand-p-title p-child"><?php //echo $current_cate[0]->name; ?></h1> -->
<?php
}
?>
<p class="post-letters"><span class="first-w"><?php echo $split_title[0].", ";?></span><?php echo $split_title[1]; ?></p>
<?php
}
}else
{
?>
<p class="post-letters"><a>No posts founds</a></p>
<?php
}
wp_reset_query();
?>
I think you are doing wrong. you need to loop of categories first and then based on the category you have to get posts. for fetch all categories you can use WP get_terms()
<?php
$terms = get_terms( array (
'taxonomy' => 'category',
'hide_empty' => false
) );
// run a query for each term
foreach( $terms as $term ) {
// Define the query
$args = array(
'post_type' => 'post',
'cat' => $term->term_id,
'extend_where' => "(post_title like '%$s_word%')",
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC'
);
// run the query
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ) {
$icon_flag = false;
$child_c = "";
$parent_c = "";
while ( $the_query->have_posts() ) { $the_query->the_post();
$split_title = explode(',', get_the_title(), 2);
$id = get_the_ID();
$current_cate = get_the_terms($id,'category');
$parent = $current_cate[0]->parent;
//load object for parent category
$parent_name = get_the_category_by_ID($parent);
if($parent_name != $parent_c && $parent != 0) {
$parent_c = $parent_name; ?>
<h1 class="grand-p-title p-parent"><?php echo $parent_c; ?></h1>
<?php }
if($current_cate[0]->name != $child_c ) {
$child_c = $current_cate[0]->name; ?>
<!-- <h1 class="grand-p-title p-child"><?php //echo $current_cate[0]->name; ?></h1> -->
<?php } ?>
<p class="post-letters"><span class="first-w"><?php echo $split_title[0].", ";?></span><?php echo $split_title[1]; ?></p>
<?php }
}else { ?>
<p class="post-letters"><a>No posts founds</a></p>
<?php } wp_reset_query();
}

Wordpress meta box not saving and updating

function wporg_add_custom_box(){
$screens = ['product', 'wporg_cpt'];
foreach ($screens as $screen) {
add_meta_box(
'wporg_box_id', // Unique ID
'Select a Authors Name ', // Box title
'wporg_custom_box_html', // Content callback, must be of type callable
$screen // Post type
);
}
}
add_action('edit_form_after_title', 'wporg_add_custom_box');
function wporg_custom_box_html($post){
$value = get_post_meta($post->ID, '_getwriter_id', true);
?>
<select name="book_writer_id" id="book_writer_id" class="postbox">
<?php
$type = 'book_authors'; // your post type
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); $id = get_the_ID();?>
<option value="<?=$id; ?>" <?php selected($value, $id); ?>><?=the_title(); ?></option>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
</select>
<?php }
function wporg_save_postdata($post_id)
{
if (array_key_exists('book_writer_id', $_POST)) {
update_post_meta(
$post_id,
'_getwriter_id',
$_POST['book_writer_id']
);
}
}
add_action('save_post', 'wporg_save_postdata');
I did not used there save_postdate function that why, it not saving value and updating data
function wporg_add_custom_box(){
$screens = ['product', 'wporg_cpt'];
foreach ($screens as $screen) {
add_meta_box(
'wporg_box_id', // Unique ID
'Select a Authors Name ', // Box title
'wporg_custom_box_html', // Content callback, must be of type callable
$screen // Post type
);
}
}
add_action('edit_form_after_title', 'wporg_add_custom_box');
function wporg_custom_box_html($post)
{
$value = get_post_meta($post->ID, '_getwriter_id', true);
?>
<select name="book_writer_id" id="book_writer_id" class="postbox">
<?php
$type = 'book_authors'; // your post type
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); $id = get_the_ID();?>
<option value="<?=$id; ?>" <?php selected($value, $id); ?>><?=the_title(); ?></option>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
</select>
<?php
}
function wporg_save_postdata($post_id)
{
if (array_key_exists('book_writer_id', $_POST)) {
update_post_meta(
$post_id,
'_getwriter_id',
$_POST['book_writer_id']
);
}
}
add_action('save_post', 'wporg_save_postdata');

How to get all products with pagination in WordPress Woo commerce

Maybe this questions already asked but that not helpful for my question.
Am creating a API for my WordPress project. So i want to send API for to get all products with pagination.
I get products list using this code:
android/all_products.php
<?php
require_once('../wp-load.php');
$args = array(
'post_type' => 'product',
'posts_per_page' => 10
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
wp_reset_query();
?>
I got all products, but i want to show products with pagination.
Note: all the API's are written in inside android folder.
Thanks in advance.
below pagination code is to show next products you can also do it to show previous product by applying same method
if(isset( $_GET['page_num'] ) ) {
$page_number = $_GET['page_num'];
} else {
$page_number = 2;
}
$args = array (
'limit' => 3,
'page' => $page_number,
);
$products = wc_get_products( $args );
foreach( $products as $product ) {`enter code here`
//your data here
// e.g $product_name = $product->get_name();
}
Next
If you want to create API's then just use Woocommerce rest API's which are built in function given in wordpress please use below url it will give you list of products with lots of options:
{{your_url}}/wp-json/wc/v2/products
Go to wp-admin and go to Woocommerce -> settings -> API
-> Enable rest API
-> Go to Key/Apps and create Auth Keys both secret and Private (Copy that keys)
Then give this above url to add it for use in application with o_auth of secret key and private key.
Please see this link for all woocommerce API's : https://woocommerce.github.io/woocommerce-rest-api-docs/#retrieve-a-product
Finally i creating a code and send the date using API.
<?php
require_once('../wp-load.php');
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$brand_product_list = new WP_Query( $args);
$pagination_count = 1;
while($brand_product_list->have_posts()) : $brand_product_list->the_post();
$pagination_count++;
endwhile; wp_reset_query();
//echo'<pre>';print_r($pagination_count/12); exit;
wp_reset_query();
?>
<?php
$pagination = round($pagination_count/12);
for($i=1;$i<=$pagination;$i++)
{
$pagination_no[] = $i;
?>
<!-- <a class="product-category-view-all" href="?pagination=<?php echo $i; ?>"><?php echo $i; ?></a> -->
<?php } ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : $_GET['pagination'];
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => 12,
);
$wp_query = new WP_Query($args);
while($wp_query->have_posts()) : $wp_query->the_post();
$product_data = wc_get_product( $post->ID );
if(!empty(get_the_post_thumbnail_url($product_data->get_id())))
{
$img = get_the_post_thumbnail_url($product_data->get_id());
}
else
{
$img = "";
}
$product_list[] = array(
'product_id' => $product_data->get_id(),
'product_name' => $product_data->get_title(),
'product_regular_price' => $product_data->get_regular_price(),
'product_sale_price' => $product_data->get_sale_price(),
'product_price' => $product_data->get_price(),
'img' => $img,
'rating' => $product_data->get_average_rating(),
'stock_quantity' => $product_data->get_stock_quantity(),
'stock' => $product_data->is_in_stock(),
);
endwhile; wp_reset_query();
$data[] = array(
'pagination' => $pagination_no,
'product_list' => $product_list
);
//echo json_encode($peoduct_list, $pagination)
echo json_encode($data)
?>

How to display all pdf files with custom fields

I have found a code to display all media files, but how do I list all pdf's only with custom fields
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => null, // any parent
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) {
setup_postdata($post);
the_title();
the_attachment_link($post->ID, false);
the_excerpt();
}
}
?>
Read this answer in detail and try this.
$extension = 'pdf';
$uploads = wp_upload_dir();
$files = new \FilesystemIterator(
$uploads['path'],
\FilesystemIterator::SKIP_DOTS
| \FilesystemIterator::FOLLOW_SYMLINKS
);
$html = [];
foreach ( $files as $pdf )
{
/** #noinspection PhpIncludeInspection */
if (
! $files->isDir()
&& $extension === $files->getExtension()
)
$html[] = $files->getRealPath();
}
You can then easily craft your final MarkUp using for e.g. native PHPs explode() function:
printf(
"<ul><li>%s</li></ul>",
explode( "</li><li>", $html )
);
Helping Link:
https://wordpress.stackexchange.com/questions/214515/list-and-show-uploaded-pdf-files-dynamically#answer-215516

wp how to exclude filter from the custom query

I am trying to exclude a filter (slug) from a loop. I am researched exclude and have tried it in various places in the code. Usually I break the page. This is the code, I am attempting to change to exclude the slug 20. It is a filter named 'american'. I have tried to exclude at the beginning of the array, didn't work; then, I tried after the foreach($catz as $cat) section. I tried this ‘exclude=20&title_li=' . I tried cat=-20 and various other combinations. Any help would be very very appreciated.
// The Custom Query
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => $counter_folio,
'paged' => $paged,
'order' => 'DESC'
);
query_posts( $args );
while( have_posts() ) : the_post();
$color = substr(get_option('dcb_dynamic_color'), 1);
// Get the original thumbnail of the post
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), false, '' );
$excerpt = get_the_excerpt();
// Get the custom fields
$vimeo = get_post_meta($post->ID, "vimeo", true);
$youtube = get_post_meta($post->ID, "youtube", true);
// Get the filter > Category of item
$catz = wp_get_object_terms($post->ID,'filters');
foreach($catz as $cat){
$currcat = $cat->slug;
$catname = $cat->name;
break;
}
$counter++;
?>
If you want to filter the post with id 20 simple exclude it with the following code
// The Custom Query
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => $counter_folio,
'paged' => $paged,
'order' => 'DESC'
);
query_posts( $args );
while( have_posts() ) : the_post();
$color = substr(get_option('dcb_dynamic_color'), 1);
// Get the original thumbnail of the post
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), false, '' );
$excerpt = get_the_excerpt();
// Get the custom fields
$vimeo = get_post_meta($post->ID, "vimeo", true);
$youtube = get_post_meta($post->ID, "youtube", true);
// Get the filter > Category of item
$catz = wp_get_object_terms($post->ID,'filters');
foreach($catz as $cat){
if($cat->slug != 'american')
{
$currcat = $cat->slug;
$catname = $cat->name;
break;
}
}
$counter++;

Resources