how to change column name in my orders table
this is solution
add_filter( 'woocommerce_account_orders_columns', 'add_account_orders_column', 10, 1 );function add_account_orders_column( $columns ){
$columns = array(
'order-number' => __( 'OrderTest', 'woocommerce' ),
'order-date' => __( 'DateTest', 'woocommerce' ),
'order-status' => __( 'StatusTest', 'woocommerce' ),
'order-total' => __( 'TotalTest', 'woocommerce' ),
'order-actions' => __( 'ActionsTest', 'woocommerce' ),
);
return $columns;}
Related
I am trying to get a custom min price excluding some of the attribute values that contain the words lid and deckel but I am getting a fatal error. Can anyone please help with this?
add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format', 10, 2 );
function bbloomer_variation_price_format( $price, $product ) {
$args = array(
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'pa_option',
'value' => array(Deckel),
'compare' => 'NOT LIKE',
)
)
);
}
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$min_var_reg_price = $product->get_variation_regular_price( 'min', true );
$min_var_sale_price = $product->get_variation_sale_price( 'min', true );
$max_var_reg_price = $product->get_variation_regular_price( 'max', true );
$max_var_sale_price = $product->get_variation_sale_price( 'max', true );
if ( ! ( $min_var_reg_price == $max_var_reg_price && $min_var_sale_price == $max_var_sale_price ) ) {
if ( $min_var_sale_price < $min_var_reg_price ) {
$price = sprintf( __( 'ab <del>%s</del><ins>%s</ins> %s', 'woocommerce' ), wc_price( $min_var_reg_price ), wc_price( $min_var_sale_price ), $product->get_price_suffix() );
} else {
$price = sprintf( __( 'ab %s %s', 'woocommerce' ), wc_price( $min_var_reg_price ), $product->get_price_suffix() );
}
}
return $price;
}
}
I need to make changes to the code that calculates prices for a large number of variants of one product according to the set price calculation logic
How to change the code below to:
all variants in white would have a price calculated according to "$price1 = round (... calculations ...);.
and variants in black would have a price calculated according to $price2 = round (... calculations ...);
<?php
(...)
$config = [
'product_id' => 123,
'attributes_values' => [
(...)
'attribute_pa_color' => [
'white' => 1,
'black' => 2,
]
]
];
$product_variations = $wpdb->get_results("SELECT * FROM `wp_posts` WHERE `post_parent` = ".$config['product_id']." AND `post_type` LIKE 'product_variation'");
if( $product_variations ){
foreach( $product_variations as $product_variation ){
$pva = [];
$product_variation_attributes_dba = $wpdb->get_results("SELECT * FROM `wp_postmeta` WHERE `post_id` = ".$product_variation->ID." AND `meta_key` LIKE 'attribute_pa_%'");
if ( $product_variation_attributes_dba ) foreach( $product_variation_attributes_dba as $prod_attr){
$value = $config['attributes_values'][$prod_attr->meta_key][$prod_attr->meta_value];
if( !empty( $config['attributes_values'][$prod_attr->meta_key] ) ){
if( !$value>0 ){
echo 'Błąd przy parsowaniu produktu #'.$product_variation->ID.' ('.$prod_attr->meta_key.' = '.$prod_attr->meta_value.')<br>';
exit;
}
$pva[ $prod_attr->meta_key ] = $value;
}
}
$price1 = round( ...calculations... );
$price2 = round( ...calculations... );
$regular_price = $wpdb->get_row("SELECT * FROM `wp_postmeta` WHERE `post_id` = ".$product_variation->ID." AND `meta_key` LIKE '_regular_price'");
if($regular_price){
$wpdb->update( 'wp_postmeta', array( 'meta_value'=> $price ), array( 'post_id' => $product_variation->ID, 'meta_key' => '_regular_price' ) ,array( '%s', '%s', '%d', ) );
}else{
$wpdb->insert( 'wp_postmeta', array( 'post_id' => $product_variation->ID, 'meta_key' => '_regular_price', 'meta_value'=> $price ), array( '%s', '%s', '%d', ) );
}
$regular_price = $wpdb->get_row("SELECT * FROM `wp_postmeta` WHERE `post_id` = ".$product_variation->ID." AND `meta_key` LIKE '_price'");
if($regular_price){
$wpdb->update( 'wp_postmeta', array( 'meta_value'=> $price ), array( 'post_id' => $product_variation->ID, 'meta_key' => '_price' ) ,array( '%s', '%s', '%d', ) );
}else{
$wpdb->insert( 'wp_postmeta', array( 'post_id' => $product_variation->ID, 'meta_key' => '_price', 'meta_value'=> $price ), array( '%s', '%s', '%d', ) );
}
$_thumbnail_id = $pva['attribute_pa_color']==2?4333:4278;
$wpdb->update( 'wp_postmeta', array( 'meta_value'=> $_thumbnail_id ), array( 'post_id' => $product_variation->ID, 'meta_key' => '_thumbnail_id' ) ,array( '%s', '%s', '%d', ) );
}
}
echo 'Successful price import!';
I'm bulding a store on woocommerce and I have variable products and every variation has its unique prices. What i'm trying to do is always show the lowest price, even if no option is selected. And this price should change when the options are selected later (like the one in the bottom after the options, but always displayed).
It's not important if it's show after or before the options.
I try to use this:
add_filter( 'woocommerce_show_variation_price', '__return_true' );
But It's not working in the latest version of woocommerce.
Thanks for your help.
To show the lowest price use below:
add_filter( 'woocommerce_variable_sale_price_html', 'custom_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'custom_variation_price_format', 10, 2 );
function custom_variation_price_format( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . $product->get_price_suffix() . '</del> <ins>' . $price . $product->get_price_suffix() . '</ins>';
}
return $price;
}
Please use below function:
add_filter( 'woocommerce_format_sale_price', 'addweb_solu_sale_price', 20, 3 );
function addweb_solu_sale_price( $price, $regular_price, $sale_price ) {
return wc_price( $sale_price );
}
This question already has answers here:
Get orders total purchases amount for the day in Woocommerce
(2 answers)
Closed 4 years ago.
I am making a wordpress plugin.
I want to fetch all the woo commerce order data into the plugin. How can I do?
This is not proper way that I am using.
$order = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() ),
) );
for($o=0;$o<count($order);$o++):
$order_details = get_post_meta( $order[$o]->ID );
$customer_name = $order_details[_billing_first_name][0].' '.$order_details[_billing_last_name][0];
$customer_phone = $order_details[_billing_phone][0];
$customer_email = $order_details[_billing_email][0];
$customer_city = $order_details[_billing_city][0];
$customer_state = $order_details[_billing_state][0];
$customer_state = $order_details[_billing_state][0];
}
$query_args = array(
'fields' => 'ids',
'post_type' => 'shop_order',
'post_status' => array_keys( wc_get_order_statuses() ),
'posts_per_page' => -1,
//'numberposts' => -1,
'date_query' => array(
array(
'before' => $end_date, // replace desired date
'after' => $start_date, // replace desired date
'inclusive' => true,
),
),
);
$query = new WP_Query($query_args);
I want to get minimum price and maximum price based on categories.
The woocommerce query which gives me minimum and maximum product price range but i want it on the bases of category.
for example: category="music,clothing".
Here is query for minimum and maximum price:
$min = floor( $wpdb->get_var(
$wpdb->prepare('
SELECT min(meta_value + 0)
FROM %1$s
LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
WHERE meta_key IN ("' . implode( '","', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price', '_min_variation_price' ) ) ) . '")
AND meta_value != ""
', $wpdb->posts, $wpdb->postmeta )
) );
$max = ceil( $wpdb->get_var(
$wpdb->prepare('
SELECT max(meta_value + 0)
FROM %1$s
LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
WHERE meta_key IN ("' . implode( '","', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price' ) ) ) . '")
', $wpdb->posts, $wpdb->postmeta, '_price' )
) );
Please suggest me how can i get it according to category selected.
SQL Query:
//SQL Query to get max price :
SELECT max(meta_value + 0) FROM wp_posts LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE meta_key = '_price' AND (wp_term_relationships.term_taxonomy_id IN (46,47));
//SQL Query to get min price :
SELECT min(meta_value + 0) FROM wp_posts LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE (meta_key = '_price' OR meta_key='_min_variation_price') AND (wp_term_relationships.term_taxonomy_id IN (46,47));
//46, 47 is the term id of category. So you need to have id of music and clothing
WP_Query :
This is the way how I do it :
<?php
$category = array('t-shirt');
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $category,
'operator' => 'IN'
)
),
'meta_query' => array(
array(
'key' => '_price',
)
)
);
$loop = new WP_Query($args);
echo "Max :" get_post_meta($loop->posts[0]->ID, '_price', true);
?>
I used 'order'=>'DESC' so that it will sort by Highest to Lower and hence we can get Highest from it.
If you want to have Min, change 'order'=>'DESC' to 'order'=>'ASC' and you will have echo "Min :" get_post_meta($loop->posts[0]->ID, '_price', true);
Rohil_PHPBeginner's answer is correct, but you do no need to get prices for all products to find the maximum. You can just alter the "posts_per_page" to be 1:
<?php
$category = array('t-shirt');
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $category,
'operator' => 'IN'
)
),
'meta_query' => array(
array(
'key' => '_price',
)
)
);
$loop = new WP_Query($args);
echo "Max :" get_post_meta($loop->posts[0]->ID, '_price', true);
wp_reset_postdata();
?>