0
votes

So I have one issue with Wordpress and Woocommerce products, where on single product page in the loop I created custom variables like this:

// Quantity
$quantity = get_post_meta( $post->ID, "wccaf_quantity", true ); 
// Regular Price
$regular_price = get_post_meta( get_the_ID(), '_regular_price', true);
// Subtotal
$subtotal = $quantity * $regular_price;

Calculations are based on custom fields that user inputs through Wordpress admin panel. It all works on front-end, but I have faced an issue where I want to sort the products on product page with eg. $subtotal variable. Is there any possibility that I can achieve this somehow, to call this variable like post meta or something. I'm ok with PHP, but don't know how this works in Wordpress

You can see the site here, description tab in Investment details: http://aldinmujkic.website/yehuda1/product/amazonbasics-apple-certified-lightning-to-usb-cable/

1
You want to sort the products in your wp-admin based on the subtotal? Do I understand it correctly? - berend
@berend No, on the front end, on the products page. I'm just stating the example for $subtotal, there a lot more other variables. - revetinja

1 Answers

0
votes

Yes, you can query for posts using post meta. example below

<?php
    $posts_args = array(
        'post_type'         => 'products',
        'posts_per_page'    => 7,
        'meta_key'          => 'smount_post_views', //name your post meta
        'orderby'           => 'meta_value_num',
        'order'             => 'DESC'
    );

    $posts_query = new WP_Query( $posts_args );

    if( $posts_query->have_posts() ):
        while( $posts_query->have_posts() ): $posts_query->the_post();
            get_template_part( 'template-parts/content', get_post_format() );
        endwhile; 
    endif;
 ?>