woocommerce variable product price on dropdown - wordpress

Hello I m trying to make available the prices for the variations on the dropdown, at some point I were able to achieve it when were only one variable, but now I have two variables for the product but the price on the dropdown is not updating.
Based on Variable product attribute: Customizing each displayed radio buttons text value answer code, here is the code I use to get on the first step, but I need help to update the price of variations up to the second variable selected.
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
if ( empty( $term ) ) return $term;
if ( empty( $product->id ) ) return $term;
$id = $product->get_id();
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_key != 'attribute_pa_alto'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
//EDITADO PARA MOSTRAR CUANDO NO HAY STOCK MUESTRA AGOTADO
if($_product->managing_stock() && $_product->get_stock_quantity() < 1){
return $term . ' - AGOTADO';
}else{
return $term . ' - (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
}
}
return $term;
}

Related

Update Parent Product Quantity When Variation Quantity Changes

I am actually trying to update the stock quantity of parent products whenever the quantity of any of its variation increases or decreases.
What could be the error in following code.
function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
global $wpdb, $product;
// Get the product ID (can be defined)
$product_id = $product_id > 0 ? $product_id : get_the_id();
// Check and get the instance of the WC_Product Object
$product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);
// Only for variable product type
if( $product->is_type('variable') ){
// Get the stock quantity sum of all product variations (children)
$stock_quantity = $wpdb->get_var("
SELECT SUM(pm.meta_value)
FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish'
AND p.post_parent = '$product_id'
AND pm.meta_key = '_stock'
AND pm.meta_value IS NOT NULL
");
// Preparing formatted output
if ( $stock_quantity > 0 ) {
$html = '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
} else {
if ( is_numeric($stock_quantity) )
$html = '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
else
$html = '';
}
// Different output options
if( $output == 'echo_html' )
echo $html;
elseif( $output == 'return_html' )
return $html;
else
return $stock_quantity;
}
}

Update Product Stock When Variation Stock Changes in WooCommerce

I am trying the update total stock of the product when stock for any variation changes. I want to keep the total stock of all the variations updated.
Here is the code I am using but it's not working. What could be the reason.
add_action( 'woocommerce_product_set_stock', 'wc_get_variable_product_stock_quantity' );
function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
global $wpdb, $product;
// Get the product ID (can be defined)
$product_id = $product_id > 0 ? $product_id : get_the_id();
// Check and get the instance of the WC_Product Object
$product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);
// Only for variable product type
if( $product->is_type('variable') ){
// Get the stock quantity sum of all product variations (children)
$stock_quantity = $wpdb->get_var("
SELECT SUM(pm.meta_value)
FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish'
AND p.post_parent = '$product_id'
AND pm.meta_key = '_stock'
AND pm.meta_value IS NOT NULL
");
// Preparing formatted output
if ( $stock_quantity > 0 ) {
$html = '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
} else {
if ( is_numeric($stock_quantity) )
$html = '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
else
$html = '';
}
// Different output options
if( $output == 'echo_html' )
echo $html;
elseif( $output == 'return_html' )
return $html;
else
return $stock_quantity;
set_stock_quantity( $stock_quantity );
}
}

See last order on specific product

Hi how to see last order of specific product in woocommerce?
EXEMPLE:
PRODUCT NAME
PRICE 10 $ [ADD CART]
LAST ORDER
DATE QUT. PRICE
10.10.2021 2 9$
10.12.2021 6 3$
10.01.2022 39 5$
EDIT: I would like to place it on the product page and as a second viewer user sees his latest product orders.
Place the following function in your functions.php file
function latest_order_by_product_id() {
global $wpdb;
// Select Product ID
$product_id = 14;
$orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";
//Change get_row to get_col if you want all orders
//Change DESC to ASC if we need reverse order
$orders = $wpdb->get_row("
SELECT DISTINCT woi.order_id FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim,
{$wpdb->prefix}woocommerce_order_items as woi,
{$wpdb->prefix}posts as p WHERE woi.order_item_id = woim.order_item_id AND woi.order_id = p.ID
AND p.post_status IN ( $orders_statuses ) AND woim.meta_key IN ( '_product_id', '_variation_id' ) AND woim.meta_value LIKE '$product_id' ORDER BY woi.order_item_id DESC");
if($orders):
$data = array();
foreach($orders as $key => $order_id):
$order = wc_get_order( $order_id );
if ( $order ):
$data[$key]['order_date'] = $order_date = $order->get_date_completed(); // Change depending on type of date you are looking for - get_date_created(), get_date_completed(), get_date_paid()
//Loop order items
foreach ( $order->get_items() as $item_id => $item ):
if($product_id == $item->get_product_id()):
$data[$key]['qty'] = $product_qty = $item->get_quantity();
//Choose which price you want to get
$data[$key]['price'] = $product_price = $item->get_total() + $item->get_total_tax(); // Discounted total with tax
//OR
//$product_price = $item->get_subtotal() + $item->get_subtotal_tax(); // NON discounted total with tax
endif;
endforeach;
endif;
endforeach;
endif;
if($data):
$output = array();
$output[] .='<table><thead><tr><th>LAST ORDER DATE</th><th>QTY</th><th>PRICE</th></tr></theead><tbody>';
foreach($data as $order_data):
//Change date format if needed
$date = $order_data['order_date']->date("d.m.Y");
$output[] .= '<tr><td>'.$date.'</td><td>'.$order_data['qty'].'</td><td>'.$order_data['price'].'</td></tr>';
endforeach;
$output[] .= '</tbody></table>';
endif;
echo sprintf('%s',implode($output));
}
Call latest_order_by_product_id() where you need it. Result - https://prnt.sc/6AhgTPXFerBg
Example with shortcode. How to use [lobpid product_id="35"]
add_shortcode('lobpid','latest_order_by_product_id' );
function latest_order_by_product_id($attr) {
global $wpdb;
$args = shortcode_atts( array(
'product_id' => '',
), $attr );
// Select Product ID
$product_id = $args['product_id'];
//Skip the rest if we dont have product id defined
if(empty($product_id)): echo 'there is no product id defined'; return; endif;
$orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";
//Change get_row to get_col if you want all orders
//Change DESC to ASC if we need reverse order
$orders = $wpdb->get_row("
SELECT DISTINCT woi.order_id FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim,
{$wpdb->prefix}woocommerce_order_items as woi,
{$wpdb->prefix}posts as p WHERE woi.order_item_id = woim.order_item_id AND woi.order_id = p.ID
AND p.post_status IN ( $orders_statuses ) AND woim.meta_key IN ( '_product_id', '_variation_id' ) AND woim.meta_value LIKE '$product_id' ORDER BY woi.order_item_id DESC");
//Skip the rest if we dont have orders
if(empty($orders)): echo 'no orders found'; return; endif;
if($orders):
$data = array();
foreach($orders as $key => $order_id):
$order = wc_get_order( $order_id );
if ( $order ):
$data[$key]['order_date'] = $order_date = $order->get_date_completed(); // Change depending on type of date you are looking for - get_date_created(), get_date_completed(), get_date_paid()
//Loop order items
foreach ( $order->get_items() as $item_id => $item ):
if($product_id == $item->get_product_id()):
$data[$key]['qty'] = $product_qty = $item->get_quantity();
//Choose which price you want to get
$data[$key]['price'] = $product_price = $item->get_total() + $item->get_total_tax(); // Discounted total with tax
//OR
//$product_price = $item->get_subtotal() + $item->get_subtotal_tax(); // NON discounted total with tax
endif;
endforeach;
endif;
endforeach;
endif;
if($data):
$output = array();
$output[] .='<table><thead><tr><th>LAST ORDER DATE</th><th>QTY</th><th>PRICE</th></tr></theead><tbody>';
foreach($data as $order_data):
//Change date format if needed
$date = $order_data['order_date']->date("d.m.Y");
$output[] .= '<tr><td>'.$date.'</td><td>'.$order_data['qty'].'</td><td>'.$order_data['price'].'</td></tr>';
endforeach;
$output[] .= '</tbody></table>';
endif;
echo sprintf('%s',implode($output));
}

Woocommerce sort by price + products without prices at the end

I'm trying to sort prices in woocommerce, that products which don't have one will be added in the end of sorting.
My function looks like this:
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'sort-price' == $orderby_value ) {
$args['orderby'] = 'CASE WHEN meta_value_num<1 THEN meta_value_num=99999 END ASC,meta_value_num';
$args['order'] = 'ASC';
$args['meta_key'] = '_price';
}
After regenerating products lookup tables with this function sorting goes like:
1Category[1€] 1Category[2€] 1Category[No price] 2Category[1€]
Why second category products don't sorting properly?

Retrieve Wordpress 3.5 gallery images NOT attached

I am trying to find out how to use this code referencing the answer to the question here:
`global $post;
$post_subtitrare = get_post( $post->ID );
$content = $post_subtitrare->post_content;
$pattern = get_shortcode_regex();
preg_match( "/$pattern/s", $content, $match );
if( isset( $match[2] ) && ( "gallery" == $match[2] ) ) {
$atts = shortcode_parse_atts( $match[3] );
$attachments = isset( $atts['ids'] ) ? explode( ',', $atts['ids'] ) : get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID .'&order=ASC&orderby=menu_order ID' );
}`
to retrieve the same data as the get_children()? Currently, only ids are retrieved but I have tried get_post() and get_posts().
Serialized data from the get_children array:
{i:229;O:7:"WP_Post":24:{s:2:"ID";i:229;s:11:"post_author";s:1:"1";
s:9:"post_date";s:19:"2012-12-27 21:01:49";s:13:"post_date_gmt";
s:19:"2012-12-27 21:01:49";s:12:"post_content";s:0:""
;s:10:"post_title";s:8:"DSCN0703";s:12:"post_excerpt";
s:0:"";s:11:"post_status";s:7:"inherit";
s:14:"comment_status";s:6:"closed";
s:11:"ping_status";s:4:"open";s:13:"post_password";s:0:"";
s:9:"post_name";s:10:"dscn0703-6";s:7:"to_ping";s:0:"";s:6:"pinged";s:0:"";
s:13:"post_modified";s:19:"2012-12-27 21:01:49";s:17:"post_modified_gmt";
s:19:"2012-12-27 21:01:49";s:21:"post_content_filtered";s:0:"";
s:11:"post_parent";i:223;s:4:"guid";s:81:"http:/exampleurl.com/wp-content/uploads/2012/12/DSCN07031.jpg";s:10:"menu_order";i:1;s:9:"post_type";s:10:"attachment";s:14:"post_mime_type";s:10:"image/jpeg";s:13:"comment_count";s:1:"0";s:6:"filter";s:3:"raw";}
Serialized data from the code:
a:7:{i:0;s:3:"229";i:1;s:3:"225";i:2;s:3:"228";i:3;s:3:"230";i:4;s:3:"226";i:5;s:3:"227";i:6;s:3:"232";}
Can someone point me to the wp hook that will give me the same data as the get_children from the ids generated from the above code?
Not 100% sure what you're after but this is a copy of the code I used ( based on that answer with some additions ) ... it may help
$post_subtitrare = get_post( $post->ID );
$content = $post_subtitrare->post_content;
$pattern = get_shortcode_regex();
preg_match( "/$pattern/s", $content, $match );
if ( isset( $match[2] ) && ( "gallery" == $match[2] ) ) {
$atts = shortcode_parse_atts( $match[3] );
$images = isset( $atts['ids'] ) ? explode( ',', $atts['ids'] ) : get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID .'&order=ASC&orderby=menu_order ID' );
}
if ( $images ) :
$first_element = array_shift(array_values($images));
if ( ! is_object($first_element)) {
$image_count = count( $images );
$first_image = $images[0];
$featured = wp_get_attachment_image( $first_image, 'blog-featured-image' );
}
else {
$image_count = count( $images );
$first_image = array_shift(array_values($images));
$imageID = $first_image->ID;
$featured = wp_get_attachment_image( $imageID, 'blog-featured-image' );
}
... this gave me some variables which I could use including $featured.

Resources