WooCommerce custom quantity available in URL - wordpress

Hi I have a web page with Wordpress and WooCommerce, but the stock (quantity available of each product) I get it in a URL like this (http://www.mystock.com/page.php&id_item=623533&coloritem=68)
I have to put in the link, the SKU (e.g. 623533-68) and I get in return a CVS whit some data
I need to update (or at least show in memory/in front-end) the real price_a and quantity available. How can I do that?
EDIT 1:
For now I donĀ“t need the code for get the CSV info, for information is:
$url = "http://www.mystock.com/page.php&id_item=623533&coloritem=68";
$csv = file_get_contents($url);
$data = str_getcsv($csv);
$data
is an array of string, and I can get the info that I need.
But I still don't know what php file of wordpress to change, for show the actual_quantity and the if the customer add in a cart the product, know if quantity is greater than zero.
EDIT 2:
In this page show the oficial way that update the quantity, whit the function
$product->set_stock_quantity(SomeFloat); but doesn't works. $product is a new WC_Product object. And desn't show any error, just, nothing happen.
Thanks

Related

Creating WooCommerce placeholder for Emails sent

My client, a courier company, is using delivery software that is able to track with both WooCommerce order no. and ID, which means it requires both order no. and ID ("wc_order_abcdefg") to work, I believe this falls in the phpmyadmin database of post_password column.
Full tracking ID goes like: wc_order_abcdefg.66
So in the WooCommerce email settings, it only has {order_number}. How do I create one for {order_id}?
It looks like, your shipment tracking number is {order_key}.{order_number}.
Once you get hold of the order object, you can get its key simply with: $order->get_order_key().
{order_key} is not available as an email placeholder. So in order to include it in the email sent to the customer, you have different options depending on where you want it to be displayed. For example:
You could include the tracking number in the body of the email. In this case, you can override one of the WooCommerce email templates (e.g. email-order-details.php). See the WooCommerce docs for more info on how to override templates.
You could include the tracking number in the subject of the email. In this case, you can use the woocommerce_email_subject_customer_completed_order filter. E.g. (this snippet should be added to your functions.php):
add_filter( 'woocommerce_email_subject_customer_completed_order', 'add_tracking_number_to_email_subject', 1, 2 );
function add_tracking_number_to_email_subject( $subject, $order ) {
return sprintf( 'Thank you for your order! Your shipment tracking number is: %s.%s', $order->get_order_key(), $order->get_id() );
}

Recover Custom Post Type Data

I have created CPT using CPT UI plugin and suddenly I lost all the important data. I don't have any backup and I need it backup. Is there any I can get it backup? I have SQL backup of last week.
Moreover I am seeing Count on CPT categories but they are showing Empty on main CPT page. for example CPT:
Animals --> showing (0) posts
Animal Categories is showing 100 counts that means It has data but something has hide it. I remember I updated Permalinks but I don't remember the previous settings
use this sqlQuery in your SQL Database (phpMyadmin or ...) and you will get a list of your custom post type posts with their info:
SELECT *
FROM {table prefix}_posts
WHERE post_type = 'your_custom_post_type_name'
AND post_status = 'publish'
your {table prefix} can be wp or some thing else if you've change it on wp setup step you can check it in your Database or wp-config.php file in your wp root.
you can replace * in query with whatever field name you want for example:
post_content, post_title, post_excerpt
to only get fields that you need.

Woocommerce get product subtotal in cart page

I am trying to implement ajax functionality to my cart page for updating quantity. So far so good. But I am stuck at one point - updating the product subtotal. I was trying to use this : get_product_subtotal($_product, $quantity), but could not succeed. Because the 1st argument ($_product) is an object. I don't know how to get that. I can get product id and quantity which I passed with ajax call. Now I need the project object with the id. Can anyone help with solution or suggestion?
You can create product object with the following code
$product = new WC_Product( $product_id );

Woocommerce: how do I add metadata to a cart item?

I have a digital product which is described by a quantity and a price, but which also needs 3 more numbers to completely specify it (Unix dates, etc). Problem: how do I get these numbers into the cart?
As far as I can see, there are 2 possible ways to handle this:
A product variation
A product custom field
It looks like variations can only handle discrete values with a limited range (ie. red/yellow/green, S/M/L, etc), and can't handle general integers, like dates. That leaves custom fields. I think I'm right in saying that custom fields are ordinary meta data on the product post page, so I can handle them with get_post_meta and update_post_meta.
So, if I go for custom fields, then I would update the product page field during ordering, and then I would read back the field during checkout, when the WC_Order is created, and add the field to the new order. However, this won't work. I can't change metadata on the product page, because the product is global to all customers, and this operation would interfere with other customers. In other words, you can't store order-specific information in a product, so neither of these options would work.
So, how do I store temporary product metadata and pass it between the ordering and checkout phases (ie. between WC_Cart and WC_Order)?
One option would be to store it as user metadata (or as session data?), but there's got to be a better way - any ideas?
It turns out to be easy to do this with session data. When you're adding an item to the cart (see the source for add_to_cart_action) you create a session variable, containing all your additional meta data:
WC()->session->set(
'my_session_var_name',
array(
'members' => $members,
'start' => $start,
'expiry' => $expiry,
'etc' => $etc));
When the user checks out, the cart data disappears, and a new order is created. You can hook into woocommerce_add_order_item_meta to add the session meta data to the order meta data:
add_action(
'woocommerce_add_order_item_meta', 'hook_new_order_item_meta', 10, 3);
function hook_new_order_item_meta($item_id, $values, $cart_item_key) {
$session_var = 'my_session_var_name';
$session_data = WC()->session->get($session_var);
if(!empty($session_data))
wc_add_order_item_meta($item_id, $session_var, $session_data);
else
error_log("no session data", 0);
}
That's it. You do have to figure out how to get the order metadata out and do something useful with it, though. You may also want to clear the session data, from hooks into woocommerce_before_cart_item_quantity_zero, and woocommerce_cart_emptied. There's gist here which has some example code for this.

Is it possible to convert wordpress custom post type to normal categories

Currently our site's videos are posted as custom post type. We have totally changed the theme and added so many new features. For our new site we are trying to change videos to category. (Custom post type to normal categories). Is this possible. I have exported the custom post type as XML file, but I don't know how to change it to categories.
Finally an answer. Before I start These are the conditions I'm using.
The Custom posttype (called video) need to be merged into the default post
Video's has it own custom categories, who needs to be merged into the default categories
All Video's need to get a category video, the old categories need to be children of the newly created video category.
Step Zero: BACKUP my sql BACKUP
First create a the video category in the default posttype. Note it's ID (it is found in the category edit page in the url under tag_ID. I'm assuming ID 4 in my examples
Second we assign this category to the video posttype with the following Sql:
INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`)
SELECT `ID`, '4' FROM `wp_posts` WHERE `post_type` = 'video';
Next we assign the existing custom categories as children. You need the video category slug, found on the custom-category page agian in the url with taxonomy.
UPDATE `wp_term_taxonomy` SET `parent` = 4, `taxonomy` = 'category' WHERE `taxonomy` = 'VIDEO CATEGORYSLUG';
The last step is assigning the normal posttype to the custom posttype.
UPDATE `wp_posts` SET `post_type` = 'post' WHERE `post_type` = 'video';
Now it should be done and everything should be merged. So TEST TEST TEST.
After that the code making the posttype can be disabled.
This should work, if you have questions just ask.

Resources