In our country we have to appear the discounted product previous sales price. (cheapest sales price from the last 30 days)
I found a plugin on github, which is saving the previous product prices, and save it into woocommerce_prices_history_products database table.
I created a custom field in the available product variation section. Is it possible to get data from that database table?
Other question. The plugin saves prices, but I need a filter to appear the cheapest price from the last 30 days.
https://github.com/pogla/Woocommerce-Save-Product-Price-History
Any idea how can i do this?
Yes, you can use $wpdb global object to get the value from desired table. You can do something like this:
$product_id = xxx; //PUT your product id here
$serialized_data = $wpdb->get_var( "SELECT data FROM woocommerce_prices_history_products WHERE product_id=$product_id" );
$price_array = unserialize($serialized_data);
With this array you can iterate, check that saved date is less than 30 days and retrieve the cheaper price:
foreach($price_array as $date => $price) {
//Check that $data is less than 30 days
//Check that $price['s_p'] is the cheapest one.
}
Related
based off this link Payfast split payments which appears to work. How does one run a multi vendor woocommerce store and then split the payment to the associated merchant based on the product purchased?
Typically, this information is stored at the order item level. So, you need to loop through the items, get the vendor id and do your splitting payment action.
E.g. like this on the admin order page:
global $order;
$items = $order->get_items();
foreach ( $items as $item_key => $item ) {
$vendor_id = wc_get_order_item_meta( $item_key, '_vendor_id', true );
// Now get the merchant id with $vendor_id (=user id) and do split payment.
// You can also create an array with $merchant_id => $item->get_subtotal()
// and do the split payment after the loop.
}
The meta key "_vendor_id" depends on your plugin - I used WCMP (Multivendor Marketplace Solution for WooCommerce – WC Marketplace). You may need to look it up in your database how the field is called.
I want to remove/exclude any orders with the order status "On Hold" from woocommerce sales reports. By default On hold orders are included in the sales reports.
There is a function "exclude_from_order_sales_reports" found here - https://docs.woothemes.com/wc-apidocs/function-wc_register_order_type.html - that will exclude a post type from sales reports.
Also the woo order types and order status are handled here - https://docs.woothemes.com/wc-apidocs/source-function-wc_register_order_type.html#167-221
Unfortunately my php coding is basic (im still learning) and i have no clue where to start with all this code. How can I use the above function to remove on hold order from sales reports?
i seen on woocommerce main report class WC_Admin_Report on line 67 they have filter like this $order_status = apply_filters( 'woocommerce_reports_order_statuses', $order_status );
so i think you can easily alter the default order status for reports by adding filter like this on your functions.php
add_filter( 'woocommerce_reports_order_statuses', 'my_custom_order_status_for_reports', 10, 1 );
function my_custom_order_status_for_reports($order_statuses){
$order_statuses = array('completed'); // your order statuses for reports
return $order_statuses;
}
I'm building a site with Wordpress and WooCommerce for someone for the purposes of a business, but they have certain items that cannot be sold together - essentially the opposite of a force sell or chained products. If a customer puts product A in their cart, I don't want them to be able to put product C in with it, but B and D are fine. Alternatively, when it goes to checkout, separating the products into two separate orders would work as well. Is there any way to do this? This is my first time using Wordpress, so I'm a bit at a loss.
You can use this code below :
add_action('woocommerce_add_to_cart_handler','mycustomfuncion',11,2);
function mycustomfuncion($p,$q)
{
global $woocommerce;
$cartItem = $woocommerce->cart->cart_contents;
$currentProductId = $q->id;
foreach($cartItem as $item)
{
$productItemId = $item['product_id'];
///your condition will be here
}
return $q;
}
You will get all product ids which are in cart by the $productItemId. And you will get current product id (which user wants to add to cart ) by $currentProductId. After your conditions if you want to allow to add to cart the product then return $q from the function otherwise don't return any value.
I currently have a form where the admin can bulk update sale prices of products within one screen.
When the form is submitted i simply use update_post_meta like this:
update_post_meta($id,'_sale_price', 'new sale price here');
This updates the _sale_price meta key. When i go into the admin to check this, the new sale price has been inserted. When i view the product on the front end, the item is not marked as on sale. I have to go back in and re-save the product.
My question is, does woocommerce add some other meta_key to mark the product as on sale? I have had a dig round in the database for all the custom fields inserted, but can only see _sale_price.
Any help would be greatly appreciated
Having a look at the Product abstract class in WooCommerce the following php code gets whether or not the product is on sale:
return ( $this->sale_price != $this->regular_price && $this->sale_price == $this->price );
Which seems to indicate that the _price has to be the same as the sale_price in order for it to be classed as on sale. So just
update_post_meta($id, '_price', 'new sale price here too');
as well as the code in your question and it should work.
I need do sell a product whose price depends on a complex calculation over non-discrete parameters set by the customer on the product page, and also on a custom database query result.
How can i calculate the price server-side every time the customer changes parameter-values and apply that price when the customer adds to cart?
i read a similar post whose answer suggests a WC plugin, but even that plugin doesn't satisfy my needs.
Thanks
Probably, you should try to use woocommerce_get_price filter
add_filter('woocommerce_get_price', 'get_dynamically_generated_price', 10, 2);
function get_dynamically_generated_price($price, $product) {
// ... here doing your magic with $price based on $product
// ...
return $price;
}