Retrieve instock[price] Woocommerce - wordpress

I override a function from woocommerce like this :
//Declarre custom function
function custom_add_cart_item($cart_item) {
print_r($cart_item);
return $cart_item;
}
When I print $cart_item, I want to retrieve [price] => 190 (last line)
Array ( [addons] => Array ( [0] => Array ( [name] => Nombre de pension - Simple [value] => 0 [price] => 0 [wc_booking_block_qty_multiplier] => 1 ) [1] => Array ( [name] => Nombre de pension - Demi-pension [value] => 1 [price] => 30 [wc_booking_block_qty_multiplier] => 1 ) [2] => Array ( [name] => Nombre de pension - Complete [value] => 0 [price] => 0 [wc_booking_block_qty_multiplier] => 1 ) [3] => Array ( [name] => Nombre de supplément - Petit-déjeuner [value] => 0 [price] => 0 [wc_booking_block_qty_multiplier] => 1 ) [4] => Array ( [name] => Nombre de supplément - Lit supplémentaire [value] => 0 [price] => 0 [wc_booking_block_qty_multiplier] => 1 ) ) [booking] => Array ( [_year] => 2016 [_month] => 1 [_day] => 30 [_persons] => Array ( ) [_date] => 2016-1-30 [date] => 30 janvier 2016 [_time] => [_qty] => 1 [_duration_unit] => night [_duration] => 1 [duration] => 1 night [_start_date] => 1454112000 [_end_date] => 1454198400 [_all_day] => 0 [_cost] => 80 [_booking_id] => 646 ) [product_id] => 411 [variation_id] => 0 [variation] => Array ( ) [quantity] => 1 [data] => WC_Product_Accommodation_Booking Object ( [availability_rules:WC_Product_Booking:private] => Array ( ) [id] => 411 [post] => WP_Post Object ( [ID] => 411 [post_author] => 1 [post_date] => 2016-01-27 10:55:23 [post_date_gmt] => 2016-01-27 10:55:23 [post_content] => [post_title] => Produit de test [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => closed [post_password] => [post_name] => produit-de-test [to_ping] => [pinged] => [post_modified] => 2016-01-27 16:04:41 [post_modified_gmt] => 2016-01-27 16:04:41 [post_content_filtered] => [post_parent] => 0 [guid] => http://xxx.xxx [menu_order] => 0 [post_type] => product [post_mime_type] => [comment_count] => 0 [filter] => raw ) [product_type] => accommodation-booking [shipping_class:protected] => [shipping_class_id:protected] => 0 [total_stock] => [supports:protected] => Array ( ) [wc_booking_duration_type] => customer [wc_booking_duration_unit] => night [wc_booking_duration] => 1 [manage_stock] => no [stock_status] => instock [price] => 110 ) )
Sorry for that ugly array..
I don't know how to retrive this value. Thanks for your help !

Related

what are difference between get_terms() and get_categories() in wordpress. I have a get_category and get_terms as follows

what are difference between get_terms() and get_categories() in wordpress. I have a get_category and get_terms as follows
get_categories array of object
array(
[0] => WP_Term Object
(
[term_id] => 2
[name] => Breakfast
[slug] => breakfast
[term_group] => 0
[term_taxonomy_id] => 2
[taxonomy] => menu_category
[description] => hello word
[parent] => 0
[count] => 4
[filter] => raw
[cat_ID] => 2
[category_count] => 4
[category_description] => hello word
[cat_name] => Breakfast
[category_nicename] => breakfast
[category_parent] => 0
)
[1] => WP_Term Object
(
[term_id] => 4
[name] => Dinner
[slug] => dinner
[term_group] => 0
[term_taxonomy_id] => 4
[taxonomy] => menu_category
[description] =>
[parent] => 0
[count] => 4
[filter] => raw
[cat_ID] => 4
[category_count] => 4
[category_description] =>
[cat_name] => Dinner
[category_nicename] => dinner
[category_parent] => 0
)
[2] => WP_Term Object
(
[term_id] => 3
[name] => Lunch
[slug] => lunch
[term_group] => 0
[term_taxonomy_id] => 3
[taxonomy] => menu_category
[description] =>
[parent] => 0
[count] => 4
[filter] => raw
[cat_ID] => 3
[category_count] => 4
[category_description] =>
[cat_name] => Lunch
[category_nicename] => lunch
[category_parent] => 0
)
)
//-----------------------------------------------------------------------------------------------------------
get_terms array of object
Array
(
[0] => WP_Term Object
(
[term_id] => 2
[name] => Breakfast
[slug] => breakfast
[term_group] => 0
[term_taxonomy_id] => 2
[taxonomy] => menu_category
[description] => hello word
[parent] => 0
[count] => 4
[filter] => raw
)
[1] => WP_Term Object
(
[term_id] => 4
[name] => Dinner
[slug] => dinner
[term_group] => 0
[term_taxonomy_id] => 4
[taxonomy] => menu_category
[description] =>
[parent] => 0
[count] => 4
[filter] => raw
)
[2] => WP_Term Object
(
[term_id] => 3
[name] => Lunch
[slug] => lunch
[term_group] => 0
[term_taxonomy_id] => 3
[taxonomy] => menu_category
[description] =>
[parent] => 0
[count] => 4
[filter] => raw
)
)
Yes I tried my way but I don't understand what is the difference between the two?
get_categories() is a wrapper function for get_terms('category').
Being that get_terms() is the main function it has a few more arguments that can be passed to it.
In general wordpress has quite a few wrapper functions that make for less coding per se, but if you want full control you can use the main function. A lot of them take similar arguments and return the same things with little difference.

How to pass variables to attributes to variations, of a Woocomerce variable product?

I have a function that it is supposed to create a variation and fill the variation with the values, the attributes of the variable product. To become more flexible i didn't create values for the products attributes besides
I have tried a hundred ways the make the values stick to the variation but they dont. The variation is created in the backend but it has none of the values atributed?????? PLEEEEASE HELP!!!! Im at this for 4 days now. ;(
The only values attributed to the product's atributes is ">>EMPTY<<" value. ///////////// Variables $data -> Array (Key -> Value), so that the keys == to the products atributes $product -> $product_id
function add_variable_product_to_cart($product_id, $data) {
$my_post = array(
'post_title'=> 'Order_Printaway',
'post_name' => 'product_' . $data["itemIdentifier"],
'post_status' => 'publish',
'post_parent' => $product_id ,//post is a child post of product post
'post_type' => 'product_variation',//set post type to product_variation
'guid'=>home_url() . '/?product_variation=product_' . $data["itemIdentifier"]
);
//Insert ea. post/variation into database:
$attID = wp_insert_post( $my_post );
update_post_meta($attID, '_price', $data["final_price"]);
update_post_meta($attID, '_regular_price', $data["final_price"]);
foreach($data as $key => $value){
$avail_attributes = array($value);
wp_set_object_terms($product_id, $avail_attributes, $key);
$thedata[$key]['name'] = $value;
}
update_post_meta( $attID,'_product_attributes',$thedata);
if ( ! is_admin() ) {
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
// if product not found, add it
if ( ! $found ){
WC()->cart->add_to_cart( $product_id, 1, $attID);
}
}
else{
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id, 1, $attID);
}
}
}
///////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////// OUTPUT //////////////////////////////////////////////////////////////////////
//Atributes of the Product ->
Array ( [a_color] => Array ( [name] => a_color [value] => >>EMPTY<< [position] => 0 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_creasing] => Array ( [name] => a_creasing [value] => >>EMPTY<< [position] => 1 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_finishing] => Array ( [name] => a_finishing [value] => >>EMPTY<< [position] => 2 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_finishing_desc] => Array ( [name] => a_finishing_desc [value] => >>EMPTY<< [position] => 3 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_format_dir] => Array ( [name] => a_format_dir [value] => >>EMPTY<< [position] => 4 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_page] => Array ( [name] => a_page [value] => >>EMPTY<< [position] => 5 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_paper] => Array ( [name] => a_paper [value] => >>EMPTY<< [position] => 6 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_paper_desc] => Array ( [name] => a_paper_desc [value] => >>EMPTY<< [position] => 7 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_perforation] => Array ( [name] => a_perforation [value] => >>EMPTY<< [position] => 8 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_processing] => Array ( [name] => a_processing [value] => >>EMPTY<< [position] => 9 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [product] => Array ( [name] => product [value] => >>EMPTY<< [position] => 10 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [shipping] => Array ( [name] => shipping [value] => >>EMPTY<< [position] => 11 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) )
////Array of data that i am trying to introduce in the Variation
Array ( [languageCode] => Array ( [name] => en ) [countryCode] => Array ( [name] => GB ) [saveItem] => Array ( [name] => save item by name ) [itemIdentifier] => Array ( [name] => asdasadasds ) [product] => Array ( [name] => flyer ) [a_page] => Array ( [name] => A_1stg ) [a_format] => Array ( [name] => A_105_x_148_mm_DIN-A-6 ) [a_format_dir] => Array ( [name] => A_panel_format ) [a_paper] => Array ( [name] => A_115gqm_Bilderdruck ) [a_paper_desc] => Array ( [name] => A_paper_gloss_no_core ) [a_color] => Array ( [name] => A_4/0-farbig_Euroskala ) [a_finishing] => Array ( [name] => A_Ohne_Finishing ) [a_finishing_desc] => Array ( [name] => A_Ohne_Veredelung ) [a_processing] => Array ( [name] => A_Cutt ) [a_creasing] => Array ( [name] => A_No_Groove ) [a_perforation] => Array ( [name] => A_No_Perfo ) [quantity] => Array ( [name] => 100_quantity ) [shipping] => Array ( [name] => standard ) [priceNet] => Array ( [name] => 8,31 ) [priceGross] => Array ( [name] => 9,97 ) [shippingNet] => Array ( [name] => 4,13 ) [shippingGross] => Array ( [name] => 4,96 ) [final_price] => Array ( [name] => 17.916 ) ) Atributes of the variation after get_post_meta(757, '_product_attributes', true)) Array ( [0] => Array ( [languageCode] => Array ( [name] => en ) [countryCode] => Array ( [name] => GB ) [saveItem] => Array ( [name] => save item by name ) [itemIdentifier] => Array ( [name] => asdasadasds ) [product] => Array ( [name] => flyer ) [a_page] => Array ( [name] => A_1stg ) [a_format] => Array ( [name] => A_105_x_148_mm_DIN-A-6 ) [a_format_dir] => Array ( [name] => A_panel_format ) [a_paper] => Array ( [name] => A_115gqm_Bilderdruck ) [a_paper_desc] => Array ( [name] => A_paper_gloss_no_core ) [a_color] => Array ( [name] => A_4/0-farbig_Euroskala ) [a_finishing] => Array ( [name] => A_Ohne_Finishing ) [a_finishing_desc] => Array ( [name] => A_Ohne_Veredelung ) [a_processing] => Array ( [name] => A_Cutt ) [a_creasing] => Array ( [name] => A_No_Groove ) [a_perforation] => Array ( [name] => A_No_Perfo ) [quantity] => Array ( [name] => 100_quantity ) [shipping] => Array ( [name] => standard ) [priceNet] => Array ( [name] => 8,31 ) [priceGross] => Array ( [name] => 9,97 ) [shippingNet] => Array ( [name] => 4,13 ) [shippingGross] => Array ( [name] => 4,96 ) [final_price] => Array ( [name] => 17.916 ) ) )
I struggled with same issue and the key was in attributes' taxonomies.
If attribute marked as taxonomy then it requires slug instead of string for text based attrs. So firstly check
if ( $attribute['is_taxonomy'] ) {
$options = wc_get_product_terms( $post_id, $attribute['name'], array( 'fields' => 'slugs' ) );
} else {
$options = explode( WC_DELIMITER, $attribute['value'] );
}
and fill $attr_val with slug or string value.
update_post_meta( $post_id, 'attribute_' . sanitize_title($attribute['name'] ), $attr_val);
WC_AJAX::save_attributes() may be helpful

To get the category from the post list page

I do this for get the category name and i got the array
Array ( [0] => stdClass Object ( [term_id] => 4 [name] => cinematography [slug] => cinematography [term_group] => 0 [avhec_term_order] => 1 [term_taxonomy_id] => 4 [taxonomy] => category [description] => [parent] => 0 [count] => 11 [object_id] => 2526 [cat_ID] => 4 [category_count] => 11 [category_description] => [cat_name] => cinematography [category_nicename] => cinematography [category_parent] => 0 ) )
For this i wrote
$category = get_the_category();
print_r($category);
Now how can i store the "[name] => cinematography" in a variable.
I assume $category is the array you got there, then
$cinematography = $category[0]->cinematography;
https://drupal.org/node/1245956

WP_Query with previous_posts_link() and next_posts_link(

I'm working on an archive page for my website. I've created a page with an archive template and I'm using WP_Query to get the posts that match certain variables. I use the following code:
$myposts = new WP_Query(array('author'=>$writer,'m'=>$date, 'cat'=>$category, 'posts_per_page'=>5)); ?>
and
<?php if($myposts->have_posts()){
while ( $myposts->have_posts() ) {
$myposts->the_post();
include 'article-box.php';
}
} ?>
where 'articles.php generates the posts. Then comes the part which doesn't work.
<div id="pages-nav">
<div class="alignleft"><?php previous_posts_link('« Previous Articles') ?></div>
<div class="alignright"><?php next_posts_link('Next Articles »','') ?></div>
</div>
The links for the next / previous pages don't show up at all. Is this to do with the fact that I use a page with archive template or is there anything else I'm missing?
If I enter
[http://localhost/wordpress/archives/page/2/?date=201307&category=5&writer=0&submit=search]
as URL I get the next posts as it should be with the "Previous" link but not the "Next" link
Edit:
print_r($myposts) returns:
WP_Query Object ( [query_vars] => Array ( [author] => 0 [m] => 201307 [cat] => 7 [posts_per_page] => 5 [paged] => 1 [error] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => carolynne [tag] => [tag_id] => [author_name] => [feed] => [tb] => [comments_popup] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [fields] => [menu_order] => [category__in] => Array ( [0] => 7 [1] => 10 [2] => 11 [3] => 12 [4] => 54 ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1 [post_type] => [nopaging] => [comments_per_page] => 50 [no_found_rows] => [order] => DESC ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( [0] => Array ( [taxonomy] => category [terms] => Array ( [0] => 7 [1] => 10 [2] => 11 [3] => 12 [4] => 54 ) [include_children] => [field] => term_id [operator] => IN ) ) [relation] => AND ) [meta_query] => WP_Meta_Query Object ( [queries] => Array ( ) [relation] => ) [post_count] => 5 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 22 [max_num_pages] => 5 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => 1 [is_date] => 1 [is_year] => [is_month] => 1 [is_day] => [is_time] => [is_author] => [is_category] => 1 [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_404] => [is_comments_popup] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash] => 5d9c9d82fe6cd2bec72ca33ab10482e7 [query_vars_changed] => [thumbnails_cached] => [query] => Array ( [author] => 0 [m] => 201307 [cat] => 7 [posts_per_page] => 5 [paged] => 1 ) [request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND YEAR(wp_posts.post_date)=2013 AND MONTH(wp_posts.post_date)=07 AND ( wp_term_relationships.term_taxonomy_id IN (7,10,11,12,56) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 5 [posts] => Array ( [0] => WP_Post Object ( [ID] => 43393 [post_author] => 1013 [post_date] => 2013-07-31 18:17:44 [post_date_gmt] => 2013-07-31 17:17:44 [post_content] => This film is a cross [...]
Send $paged variable to query as like below:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$myposts = new WP_Query(array('author'=>$writer,'m'=>$date, 'cat'=>$category, 'posts_per_page'=>5,'paged' => $paged));
And change your pagination code to below:
<div id="pages-nav">
<div class="alignleft">
<?php previous_posts_link('« Previous Articles',$myposts->max_num_pages); ?>
</div>
<div class="alignright">
<?php next_posts_link('Next Articles »',$myposts->max_num_pages); ?>
</div>
</div>

printing all the “maps” related to the content type

my content type, plan to have one or more 'maps'.
With the following php code, I can take all their link:
<?php print render($content['field_mappe_di_riferimento']); ?>
and I get the following result:
I would like to put before the link the image that is within the content type "Mappa", entered in the field "field_immagine_min."
this is the structure of my content type:
this is the content type "Mappa":
with: "print_r($field_mappe_di_riferimento);" I get it:
Array ( [0] => Array ( [nid] => 30 [access] => 1 [node] => stdClass Object ( [vid] => 30 [uid] => 1 [title] => Carta di Marignano, primo giorno, 13 Settembre 1515 [log] => [status] => 1 [comment] => 1 [promote] => 1 [sticky] => 0 [nid] => 30 [type] => mappa [language] => en [created] => 1352066743 [changed] => 1352127342 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1352127342 [revision_uid] => 1 [field_battaglia_di_riferimento] => Array ( [und] => Array ( [0] => Array ( [nid] => 2 ) ) ) [field_immagine_std] => Array ( [und] => Array ( [0] => Array ( [fid] => 23 [alt] => [title] => [width] => 672 [height] => 554 [uid] => 1 [filename] => polesella.jpg [uri] => public://polesella_0.jpg [filemime] => image/jpeg [filesize] => 337755 [status] => 1 [timestamp] => 1352066743 [rdf_mapping] => Array ( ) ) ) ) [field_immagine_min] => Array ( [und] => Array ( [0] => Array ( [fid] => 24 [alt] => [title] => [width] => 150 [height] => 110 [uid] => 1 [filename] => polesella_small.jpg [uri] => public://polesella_small.jpg [filemime] => image/jpeg [filesize] => 10672 [status] => 1 [timestamp] => 1352066743 [rdf_mapping] => Array ( ) ) ) ) [field_testo_opzionale] => Array ( [und] => Array ( [0] => Array ( [value] => pisello [format] => [safe_value] => pisello ) ) ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => sioc:Item 1 => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date 1 => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 1352066743 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => ant [picture] => 0 [data] => b:0; ) ) )
so I tried to take the nid of the node that I want to take the picture, with success:
print ($content['field_mappe_di_riferimento']['#items'][0][nid]);
I get 30...
I found the solution:
$array = $content['field_mappe_di_riferimento']['#items'];
$k = 0;
foreach($array as $k => $v){
print($k);
//carico il nodo
$node = node_view(node_load($content['field_mappe_di_riferimento']['#items'][$k]['nid']));
//stampa l'img del nodo
print render($node['field_immagine_min']);
}

Resources