Accessing woocommerce product attributes using query_posts - php

I am querying the posts as shown below:
query_posts('post_type=Product&showposts=-1');
This returns all post of type product. But I have custom attributes on my products that have been created in wooCommerce but I can't seem find this info anywhere.
I tried using:
$thePost = get_post_custom()
I also tried:
$thePost = get_post_meta(get_the_ID)
when I print_r these I get a lot of information but I can't see the product attributes from woocommerce anywhere
the image below shows how this information is set on the product.
How do I access this information after querying the post?
specifically I need to extract the colours and sizes.

Product variations are saved as another child posts (custom post type product_variation). Following code is untested, but you should get the point.
query_posts('post_type=Product&showposts=-1');
while( have_posts() ){
the_post();
$product_id = get_the_ID();
$variations = get_posts( array('post_type' => 'product_variation', 'post_parent' => $product_id, 'posts_per_page' => -1 ) );
foreach( $variations as $var){
$var_customs = get_post_customs( $var->ID );
// now you can inspect "meta" fields
}
}
So you have Product with ID=7 ->post_type=product, its variations are post_type=product_variation&post_parent=7. And sizes and coulours are saved as meta values of these variations. Meta keys start with attribute_pa_....

Related

Creating custom buttons for WooCommerce product attribute

In WooCommerce, I have an attribute called size.
By default, WooCommerce spits out a dropdown to showcase this. But, I'm trying to create custom buttons to showcase size.
I have the following so far:
<?php
global $post;
global $product;
$args = array(
'post_type' => 'product',
'posts_per_page' => 2
);
$loop = new WP_Query($args);
while ( $loop->have_posts() ) : $loop->the_post();
$product_id = get_the_ID();
$product = wc_get_product($product_id);
$variations = $product->get_available_variations();
$variations_id = wp_list_pluck( $variations, 'variation_id' );
print_r($variations_id);
foreach ($product->get_available_variations() as $variation) {
foreach (wc_get_product($variation['variation_id'])->get_variation_attributes() as $attr) {
echo '<pre>'; var_dump(wc_attribute_label( $attr )); echo '</pre>';
}
}
endwhile; wp_reset_query(); ?>
The above successfully outputs the sizes for a product, i.e. it outputs:
But, I can't figure out how to define on click actions for my custom buttons, can't see anything documented either. So when a user clicks my custom button and then clicks add to cart, that it adds the correct size to the cart.
I'm trying to achieve this without the use of a plugin, is it achievable?
you can hide the the default size dropdown and onclick of your custom button you can change the values in the hidden input.
OR
You need to need to hit API from your custom add to cart button which will take variation id with product id
If you can send me link then maybe guide you better. Please accept if it works

Create and insert wordpress product tags programatically using wp_set_object_terms

I have around 2000 products which has no tags. Now i want to update tag for those products. I want to set post_title=tag for all 2000 products.
Below code works for a product to set CUSTOM TAG to PRODUCT
wp_set_object_terms($productID, array('product_tag1','product_tag2','product_tag3'), 'product_tag');
Instead of complicating the work from front end for each product.
Can someone please guide me how to set post_title=tag using php for all my products.
You can loop through your products and assign the tags, maybe something like this:
Update:
If you want to just add a tag to posts which dont have any existing tags, you could do something like this:
<?php
add_action('init', 'add_tags_products');
function add_tags_products()
{
$args = array(
'post_type' => 'products', // your product post type
'posts_per_page' => - 1,
);
$posts = get_posts($args);
foreach ($posts as $post):
setup_postdata($post);
// get the title of the post
$title = get_the_title($post->ID);
// check to see if the post has any tags
if( ! has_term( '', 'product_tag', $post->ID ) ) :
// create the term
wp_set_object_terms($post->ID, array($title), 'product_tag');
endif;
wp_reset_postdata();
endforeach;
}
Paste the above code in your index.php file for testing and visit the site, and after all the tags are set, remove it. Also backup first in case something happens.

WooCommerce execute function if product is from a certain category

I'm trying to translate my products in WooCommerce based on the category.
When WooCommerce is loaded, I run an action hook:
add_action( 'woocommerce_init', 'translate_products' );
In my function translate_products I run a WP_Query so I can translate the products based on the current ID of the loop. It only translates all products, not only the ones with the category 'en' though. I am unable to get only the ID of the products assigned to the 'en' category.
It would be more than great, if someone could help me. Thanks in advance!
I'm fairly new to Wordpress and especially WooCommerce coding, so please excuse me, if I'm doing a stupid mistake. ;)
Here's my code:
function translate_products() {
$args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => 'en' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$current_id = get_the_id();
pll_set_post_language($current_id,'en');
endwhile;
wp_reset_query();
}
Use own function for fetch WooCommerce product is not good idea of wordpress programming.
Please read official document of WooCommerce. Open the link and find "Product Categories".
https://docs.woocommerce.com/document/woocommerce-shortcodes/
If you want to use category name of that product then you have to find its term id and use id and use in shortcode

shortcode to show products by category woocommerce

I found on commerce website two following code
[Products ids="1, 2, 3, 4, 5"]
[Products skus="foo, bar, baz" orderby="date" order="desc"]
One is used to show product by ids and second by foot. I want to show the product with category, but I could not find any shortcut for this.
E.g [products product_cat="shoes"]
Can anyone help me?
Did you try like this:
PRODUCT CATEGORIES BY SLUG
This will output the shortcode: [[[product_category category=”” per_page=”12″ columns=”4″ orderby=”date” order=”desc”]]]. Similar to product categories but this will output all products within the specified category and can be arranged with the per_page=”” and columns=”” parameters. The category slug can be found by navigating to the product menu in your WP admin and clicking on categories where you’ll see a list of all available categories and corresponding slugs.
See the below image which has option also
Refer this link too for more shortcodes : https://gplclub.org/woocommerce-shortcodes-full-list/
You can create your own shortcode by sing following code. the code will return all the products in given category
<?php
function view_woocommerce_products($args){
$cat = $args['category'];
$args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_ cat' => $cat );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
// Here you can access the product detail by using the $product variable
<?php endwhile; ?>
<?php wp_reset_query();
}
add_shortcode('view-woocommerce-products', 'view_woocommerce_products');
Here the use of shortcode
[view-woocommerce-products category='[your category]']

Woocommerce product variations, Google product feed and nested loops in Wordpress

I'm trying to make a Google product feed from Woocommerce - the available plugins to do this don't support product variations, which is a bit of a nuisance.
I'm working on a clothing shop with multiple sizes and colours of item, so it's required that the Google product feed should list all available size/colour combinations, and as it's doing stock control for each variation, there is no point listing the Size 10 White as in stock when all we really have left is Size 14 Brown.
So, I reckon what I need to do is create a product feed that runs a loop for each product, then inside that loop, runs a nested loop for each variation? It's a bit slow, so please suggest a better way if there is one! At least it will only have to run once a month.
Here's what I have so far:
<?php
// First get the main product details.
$args = array( 'post_type' => 'product', 'posts_per_page' => 999 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
// Now do a second nested loop to get the variations.
$args2 = array( 'post_type' => 'product_variation', 'post_parent' =>'$id');
$variationloop = new WP_Query( $args2 );
while ( $variationloop->have_posts() ) : $variationloop->the_post();
// get the parent of each variation so we can use $parent (is this necessary???)
$parent = get_post($post->post_parent);
?>
I feel that really having done the first loop, I should be able to call things from that, but once the second, variation loop is done, I don't seem to be able to refer to anything within the first, product loop. Hence get_post($post->post_parent)
And then I do the feed. So far this seems to work:
<item>
<title><?php echo $parent->post_title;?></title>
<link>http://mysite.com/shop/<?php echo $parent->post_name;?></link>
<g:image_link><?php echo wp_get_attachment_url( get_post_thumbnail_id() ) ?></g:image_link>
<g:price><?php echo $product->price ?></g:price>
<g:condition>New</g:condition>
<g:id><?php echo $id; ?></g:id>
<g:availability><?php echo $product->is_in_stock() ? get_option('product_in_stock') : get_option('product_out_stock'); ?></g:availability>
<g:brand>My Brandname</g:brand>
<g:product_type>Clothing & Accessories > Clothing > Swimwear</g:product_type>
<g:google_product_category>Clothing & Accessories > Clothing > Swimwear</g:google_product_category>
<g:shipping_weight><?php echo $product->get_weight();?></g:shipping_weight>
<g:mpn><?php echo $product->get_sku(); ?></g:mpn>
<?php if (get_option('rss_use_excerpt')) : ?>
<description><![CDATA[<?php echo $parent->post_excerpt; ?>]]></description>
<?php else : ?>
<description><![CDATA[<?php echo $parent->post_excerpt; ?>]]></description>
<?php endif; ?>
Only this is incomplete - I'm not sure how to get at all the elements that I need. In particular, how do I retrieve the variation size and colour, which are in a different table?
I managed to get this working, so am leaving details in case it's useful to anyone.
As per brasofilo's suggestion, I removed the second loop. Then I got the variations like this, checking with meta_compare to see that they were in stock.
$args = array( 'post_type' => 'product_variation', 'post_count' =>'9999','meta_key' => '_stock', 'meta_value' => '0', 'meta_compare' => '>');
$variationloop = new WP_Query( $args );
while ( $variationloop->have_posts() ) : $variationloop->the_post();
// get the parent of each variation, so you can refer to things like product description that are set per-product not per-variation.
$parent = get_post($post->post_parent);
// is the parent product live?
if ($parent->post_status=="publish")
{
Then I got the colours, sizes, photos etc like this:
$colour= get_post_meta($id, 'attribute_pa_colour', true);
$size= get_post_meta($id, 'attribute_pa_size', true);
$price= get_post_meta($id, '_price', true);
$thephoto= get_post_meta($id, '_thumbnail_id', true); // get photo associated with this variation
$image_attributes =wp_get_attachment_image_src($thephoto, 'large');
Note that the exact name of the attributes will depend what you called that attribute when you set up your variations
Then I just echoed out the values:
post_title;?> - Size
http://mywebsite.com/shop/post_name;?>
ID; ?>
post_excerpt); ?>
GBP
I hope that is helpful to someone!
There is a great free plugin for woocommerce product feed which also support product variation. https://wordpress.org/plugins/webappick-product-feed-for-woocommerce/
This premium plugin developed by ELEX supports even variations. Hope this helps those who are still looking for Google Product Feed integration for WooCommerce which supports Product Variations.
https://elextensions.com/plugin/woocommerce-google-product-feed-plugin/

Categories