Thanks to bbloomer I could retrieve the products from all orders, what I'm now trying to do is to seperate these products per ordernumber, I've tried to come up with a way to do this but every attempt resulted in a blank text editor, does anybody have suggestions for me how to retrieve all the products per order with a header of the ordernumber?
add_shortcode( 'my_purchased_products', 'bbloomer_products_bought_by_curr_user' );
function bbloomer_products_bought_by_curr_user()
{
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) return;
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $current_user->ID,
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_is_paid_statuses() ),
) );
if ( ! $customer_orders ) return;
$product_ids = array();
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order->ID );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
$product_ids[] = $product_id;
}
}
$product_ids = array_unique( $product_ids );
$product_ids_str = implode( ",", $product_ids );
return do_shortcode("[products ids='$product_ids_str']");
}
Related
Trying to display related products in WooCommerce first by same subcategory and when there are no products to show in that, display another products from parent category, avoiding being empty.
For example:
Fashion > Clothes > T-Shirts
When there are no t-shirts available to show, display another products from parent category (clothes).
Tried it but it is not working:
function wc_related_products_by_last_available_depth_term( $related_posts, $product_id, $args ) {
$product = wc_get_product( $product_id );
$terms = wp_get_post_terms( $product_id, 'product_cat' );
$hierarchy = array();
$cat_id = '';
// find the depth of terms
foreach ( $terms as $key => $term ) {
$ancestors = get_ancestors( $term->term_id, 'product_cat' );
if( $ancestors && count( $ancestors ) > 1 ) {
$hierarchy[$term->term_id] = max($ancestors);
}elseif( $ancestors ) {
$hierarchy[$term->term_id] = $ancestors[0];
}
$cat_id = $term->term_id;
}
// if level of depth term available replace $cat_id
if( $hierarchy ){
$cat_id = max( array_keys( $hierarchy ) );
}
$related_posts = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'posts_per_page' => -1,
'exclude' => array( $product_id ),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array( $cat_id )
)
)
));
return $related_posts;
}
add_filter( 'woocommerce_related_products', 'wc_related_products_by_last_available_depth_term', 99, 3 );
for my website, I need a page to display the low in stock products only, as same as choosing to display only top rated, sales, recent products.
I found a code to display the out of stock .. so I just replace "outofstock" value to "lowinstock", but that doesn't work, it shows all of the products
add_shortcode( 'out_of_stock_products', 'bbloomer_out_of_stock_products_shortcode' );
function bbloomer_out_of_stock_products_shortcode() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'outofstock',
)
),
'fields' => 'ids',
);
$product_ids = get_posts( $args );
$product_ids = implode( ",", $product_ids );
return do_shortcode("[products ids='$product_ids']");
}
Do you have any solutions for this??
I appreciate your help
You could filter the product IDs and check if the amount in stock is lower than the low stock threshold but greater than zero (out of stock).
add_shortcode( 'out_of_stock_products', 'bbloomer_out_of_stock_products_shortcode' );
function bbloomer_out_of_stock_products_shortcode() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'fields' => 'ids',
);
$product_ids = get_posts( $args );
foreach ( $product_ids as $key => $product_id ) {
if ( $product = wc_get_product( $product_id ) ) {
$stock = $product->get_stock_quantity();
$low_stock_amount = $product->get_low_stock_amount();
if ( !empty( $stock ) && !empty( $low_stock_amount ) ) {
if ( $stock > $low_stock_amount || $stock < 1 ) {
unset( $product_ids[$key] );
}
} else { //Not enough stock data
unset( $product_ids[$key] );
}
}
}
$product_ids = implode( ",", $product_ids );
return do_shortcode("[products ids='$product_ids']");
}
I'm trying to add the product category to the below (working) code but I'm not sure how to go about it.
I've tried using product_cat but as I'm not that experienced with php, I'm just guessing how to achieve this.
add_filter( 'woocommerce_get_item_data', 'display_custom_product_field_data', 10, 2 );
function display_custom_product_field_data( $cart_data, $cart_item ) {
$meta_keys = array('time','date');
$dictionary = array('time'=>'Time:','date'=>'Date:' );
$product_id = $cart_item['product_id'];
foreach($meta_keys as $key=>$meta_key){
$meta_value = get_post_meta( $product_id, $meta_key, true );
if( !empty( $cart_data ) ) $custom_items = $cart_data;
if( !empty($meta_value) ) {
$custom_items[] = array(
'key' => __( $dictionary[$meta_key] , 'woocommerce'), //or user $meta_key
'value' => $meta_value,
'display' => $meta_value,
);
}
}
return $custom_items;
}
Following code will also display the product categories on the WooCommerce checkout page
function display_custom_product_field_data( $cart_item_data, $cart_item ) {
// Product ID
$product_id = $cart_item['product_id'];
// Get post meta
$time = get_post_meta( $product_id, 'time', true );
// NOT empty
if( ! empty( $time ) ) {
$cart_item_data[] = array(
'key' => __( 'Time', 'woocommerce'),
'value' => $time,
);
}
// Get terms
$term_names = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'names' ) );
if( ! empty( $term_names ) ) {
$cart_item_data[] = array(
'key' => __( 'Categories', 'woocommerce'),
'value' => implode( ", ", $term_names )
);
}
// Get post meta
$date = get_post_meta( $product_id, 'date', true );
// NOT empty
if( ! empty( $date ) ) {
$cart_item_data[] = array(
'key' => __( 'Date', 'woocommerce'),
'value' => $date,
);
}
return $cart_item_data;
}
add_filter( 'woocommerce_get_item_data', 'display_custom_product_field_data', 10, 2 );
I am trying to do an insert to a MySQL database in WordPress using $wpdb->insert. It is just a simple log, but it is not working.
I am trying to save some basic items from an order I received after a purchase made in WooCommerce. The page to which I added the code is order-details.php and belongs to WooCommerce.
The code:
<?php
do_action( 'woocommerce_order_details_before_order_table_items', $order );
$fecha = date('Y-m-d H:i:s');
$creditos =0;
foreach ( $order_items as $item_id => $item ) {
$item_quantity = $item->get_quantity();
$product = $item->get_product();
$product_name = $product->get_name();
if( $product_name == "orián (paquete mínimo)"){
$creditos=15;
}
if( $product_name == "Andrómeda (paquete medio)"){
$creditos=30;
}
if( $product_name == "Lira (paquete básico)"){
$creditos=50;
}
if( $product_name == "Cisne (paquete real + 15 créditos)"){
$creditos=65;
}
if( $product_name == "Perseo (Paquete Premium + 30 créditos)"){
$creditos=120;
}
echo $item_quantity." ".$product_name." ".$creditos." ".$fecha." ".get_current_user_id();
$wpdb->insert(
'registrorden',
array(
'idusuario' => get_current_user_id(),
'paquete' => $product_name,
'cantidad' => $item_quantity,
'creditos' => $creditos
),
array(
'%d',
'%s',
'%d',
'%d'
)
);
wc_get_template( 'order/order-details-item.php', array(
'order' => $order,
'item_id' => $item_id,
'item' => $item,
'show_purchase_note' => $show_purchase_note,
'purchase_note' => $product ? $product->get_purchase_note() : '',
'product' => $product,
) );
}
do_action( 'woocommerce_order_details_after_order_table_items', $order );
?>
The code I added:
$wpdb->insert(
'registrorden',
array(
'idusuario' => get_current_user_id(),
'paquete' => $product_name,
'cantidad' => $item_quantity,
'creditos' => $creditos
),
array(
'%d',
'%s',
'%d',
'%d'
)
);
Here is a snapshot of the table, and here is the error.
What can I do to fix this?
There are 860+ products with different variations. The task is to allow user bulk edit "used for variations" field for some attributes. For example, we have size, gender that is still needed for variations, but also we have material, that is needed for filters only, so deleting this attribute is not a solution.
Totally untested and not particularly ideal if you have a lot of products as it could time out, but theoretically you could loop through all the variable products and update the _product_attributes meta value. I highly suggest you have a data backup and/or do this is a staging site.
function so_41978670_run_once(){
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'post_type',
'field' => 'slug',
'terms' => 'variable',
),
),
);
$products = new WP_Query( $args );
if( $products->have_posts() ){
while ( $products->have_posts() ) {
$products->the_post();
$product_id = get_the_ID();
$product = wc_get_product( $product_id );
$attributes = $product->get_attributes();
$meta_values = array();
if ( $attributes ) {
foreach ( $attributes as $attribute_key => $attribute ) {
if( in_array( $attribute_key, array( 'size', 'gender' ) ) ){
// Modify the attributes meta.
$attributes[ $attribute_key ]['is_variation'] = 1;
}
}
}
update_post_meta( $product->get_id(), '_product_attributes', $attributes );
}
wp_reset_postdata();
}
}
Potential update for WooCommerce 3.0 CRUD functions, but still untested:
function so_41978670_run_once() {
$args = array(
'limit' => -1,
'type' => 'variable',
);
$products = wc_get_products( $args );
if( ! empty ( $products ) ) {
foreach ( $products as $product ) {
$attributes = $product->get_attributes();
if ( $attributes ) {
foreach ( $attributes as $attribute_key => $attribute ) {
if( in_array( $attribute_key, array( 'size', 'gender' ) ) ){
// Modify the attribute settings.
$attribute->set_variation( true );
}
}
}
$product->set_attributes( $attributes );
$product->save();
}
}
}
add_action( 'admin_init', 'so_41978670_run_once' );