How do I get the date of RSS Feed? - rss

I have RSS feed link, but I need to obtain date from it. Then determine whether to do something based on how close it is to the current time.
$date = Get_RSS_Date();
$currenttime = getdate();
if $data = too far away from $currenttime then don't display RSS feed.
Make sense?

You can use Magpie_RSS function which will return an array of the RSS properties, and PubDate key contains the timestamp.

Related

Google Tag Manager: How can I access the basket content stored in an array?

I'm working on a Clients website, trying to send some data to an external tool using GTM. Products in basket are in an array in dataLayer and my goal is to send separate 'event' for each of the products.
So if I have 3 products in an array(basket) I need to send 3 'events' each with products name and price for example. I cannot use data layer variables here because I don't know how many products an array will have, unless there is a way to put a variable in the place of the object position in the array.
What I have tried is to store the array in a var and then use a simplest for loop to do an action as many times as there are products. This somehow works but I'm really stuck on how to go from there, how to access name, price etc to send this data further.
My test code
var harry = '{{dlv - Cart - Product}}';
for(var i = 0; i < harry.length; i++) {
console.log(harry[i]);
}
Resolved! Silly mistake that led to too much googling, when you put dataLayer variable in
' ', it returnes object Object, I just had to omit those signs at the first line and everything works like a charm(switched to forEach as well).
Answer: var harry = {{dlv - Cart - Product}}

WooCommerce custom quantity available in URL

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

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.

Drupal - Views. Setting a filter programmatically

I hope this is not a stupid question I have been searching for most of the day!
I have a Content Type (Documents) which simply contains a title, file and a category. The category value is required and is 'powered' by Taxonomy.
I now wish to create a view which will display these documents grouped and titled by the taxonomy term.
Using my limited Drupal knowledge I intent to iterate through the relevant terms IDs (using taxonomy_get_tree($vid)) and then render each view accordingly.
To do this I have been hoping to use this snippet.
view = views_get_view('documents');
$view->set_display($display_id);
$filter = $view->get_item($display_id, 'filter', 'field_dl_category');
$filter['value']['value'] = $filter_value;
$view->set_item($display_id, 'filter', 'field_dl_category', $filter);
$viewsoutput = $view->render();
But this is not working; when I query the value of the $filter ($view->get_item($display_id, 'filter', 'field_dl_category')) I get null returned.
Might this be that my filter name is not the same as the CCK field name?
I am using Drupal 7.
Any help much appreciated, I am running out of ideas (and time).
I finally managed to get this working but I took a slightly different approach.
I changed my view and added the relevant contextual filter and then used this function views_embed_view to get at my required results.
If this helps! this is my solution:
$display_id = 'default';
$vid = 7;
$terms = taxonomy_get_tree($vid);
foreach($terms As $term){
$content = views_embed_view('documents', $display_id, $term->tid);
//now we see if any content has been provided
if(trim($content) != ''){
print "<h3>" . $term->name . "</h3>";
print $content;
}
}
In my case the trim($content) returns '' with no data as the view template has been edited, this might not be the case for all.
I am a very new Drupal developer so I'm sure there are much better ways of doing this, if so please do post.
I am going to go ahead and assume that you want to show, using Views, a list of document nodes grouped by the category that they have been tagged with.
There are two (of maybe more) ways by which you can do this in Views 3:
(a) Choose a display style that allows you to select a Grouping field. (You could try the table style that ships with Views by default). Suppose you have properly related the node table to the taxonomy_term_data table through a Views relationship, you could choose taxonomy_term_data.name as the grouping field.
Note that this grouping is done before the view is just rendered. So, your query would just have to select a flat list of (content, tag) pairs.
(b) You could also make use of the Attachment display type to achieve something similar. Show the used categories first in a list view clicking on which will show a page (attachment) with all documents tagged in that chosen category.
To understand how to do (a) or (b), turn on the advanced_help module (which is not a Views requisite but is recommended) first.
For (a), read the section on Grouping in styles i.e. views/help/style-grouping.html and
For (b), read the section on Attachment display i.e. views/help/display-attachment.html
A couple of things about your approach:
(a) It will show all terms from that vocabulary irrespective of whether or not they were used to tag at least one document.
(b) views_embed_view() will return NULL even if the currently viewing user does not have access to the view. So, ensure that you catch that case.
Here's an alternative:
$view = views_get_view('view_machine_name');
$view->init_display('default');
$view->display_handler->display->display_options['filters']['your_filter_name']['default_value'] = 'your_value';
$view->is_cacheable = FALSE;
$view->execute();
print $view->render();
I know you can probably set this using some convoluted method and obviously that would be better. But if you just want a quick and dirty straight access without messing around this will get you there.

Get an RSS feed's title using YQL

I'm using YQL to retrieve an RSS feed using javascript (as json), for example i use the following query:
select * from rss where url = "http://feeds2.feedburner.com/ajaxian"
The response contains the feed items, already parsed as json and everything is cool so far.
Now, I also want to get the title of the entire feed (not the title of a specific item) - but it's not a part of the result (even though the original XML feed contains it).
There is the possibility of querying the original XML itself. for example:
select channel.title from xml where url = "http://feeds2.feedburner.com/ajaxian"
and it indeed returns the feed title for that specific RSS, but that query is only valid for a RSS 2.0 formatted feeds, which stores it under rss\channel\title.
What about atom feeds which store the title under feed\title ?
What about other formats?
My question is - is there any generic way to request the feed's title through YQL? maybe somehow along with the feed itself?
thanks,
You can use the feednormalizer table to convert the feed (regardless of its format) into one of the standard formats, then grab the title from the proper node for that format.
To take the Ajaxian feed, "normalize" it as Atom and get the feed title, the query would look like:
SELECT title
FROM feednormalizer
WHERE output="atom_1.0" AND url="http://feeds2.feedburner.com/ajaxian"
(Try this in the YQL console)
There are also other tables that you can use like feed, rss and atom.
Regarding your follow up question of how to find data tables:
Go to the YQL console, make sure that the Community Tables are loaded (should already be the case with this link) and then just type in the search box on the right hand side what you are looking for. Often you can find something useful.

Resources