Woocommerce additionnal variation images - How to display the second image - php

For the purpose of a product image flipper, I want to display the second image from the variation images for each product.
I'm using the WooCommerce Additional Variation Images plugin and I don't know how to get that image.
I tried this
$product->wp_get_attachment_image_src( $id )
but it returns the following : Call to undefined method WC_Product_Variation::wp_get_attachment_image_src() in...
Anyone who is familiar with this plugin could help me ? If you need more information let me know.
Thanks !

You can get the additional variation images IDs and use these IDs to get the image src like this:
$attachment_ids = get_post_meta( key($values), '_wc_additional_variation_images', true );
$variation_images_src = array();
if ( $attachment_ids ) {
foreach ( $attachment_ids as $attachment_id ) {
$variation_images_src[] = wp_get_attachment_image_src($attachment_id)
}
}

You can get all images for all product variations by this code:
$variations = $product->get_available_variations();
foreach ( $variations as $variation ) {
echo $variation['image_src'];
}

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

Moving custom field images to product Gallery Woocommerce

I've several custom image fields (ACF) from an old configuration, and would like to move those images in the Product Gallery (Woocommerce), now I've converted all the datas into a Product post type.
I tried to set this function (found in a similar post), but nothing happens, and no errors returned neither :
function upload_all_images_to_product($product_id, $image_id_array) {
//define the array with custom fields images
$image_1 = get_field('images'); // should returns image IDs
$image_2 = get_field('images-2');
$image_3 = get_field('images-3');
$image_4 = get_field('images-4');
$image_5 = get_field('images-5');
$image_6 = get_field('images-6');
$image_id_array = array($image_1, $image_2, $image_3, $image_4, $image_5, $image_6);
//take the first image in the array and set that as the featured image
set_post_thumbnail($product_id, $image_id_array[0]);
//if there is more than 1 image - add the rest to product gallery
if(sizeof($image_id_array) > 1) {
array_shift($image_id_array); //removes first item of the array (because it's been set as the featured image already)
update_post_meta($product_id, '_product_image_gallery', implode(',',$image_id_array)); //set the images id's left over after the array shift as the gallery images
}
}
Could someone help or explain me what's wrong ?
Depending on where you run this function, you should define the $product_id argument in ACF get_field() function.
Questions to clarify: How do you run this function? are you using a hook?
Update: Hooked the function in woocommerce_process_product_meta, so when a product is created or updated, it will trigger the code.
Also your code can be simplified, optimized and compacted as follow:
add_action( 'woocommerce_process_product_meta', 'save_my_custom_settings' );
function upload_all_images_to_product( $product_id, $image_ids = array(); ) {
// Loop from 1 to 6
for ( $i = 1; $i <= 6; $i++ ) {
$field_key = 'images'.( $i == 1 ? '' : '-'.$i );
// Check that the custom field exists
if( $field_value = get_field( $field_key, $product_id ) )
$image_ids[] = $field_value; // Set each ACF field value in the array
}
if( ! empty($image_ids) ) {
// Take the first image (removing it from the array) and set it as the featured image
set_post_thumbnail( $product_id, array_shift($image_ids) );
}
if( ! empty($image_ids) ) {
// Set the remaining array images ids as a coma separated string for gallery images
update_post_meta( $product_id, '_product_image_gallery', implode(',', $image_ids) );
}
}
Code goes in functions.php file of your active child theme (or active theme). untested it could work.

Get URL of variation image thumbnails in WooCommerce

I'm using the following code to grab the image of every variation for a particular product:
$product = new WC_Product_Variable( $product_id );
$variations = $product->get_available_variations();
foreach ( $variations as $variation ) {
echo "<img src=" . $variation['image']['url'] .">";
}
This returns the full size image.
Can anyone tell me how I would modify this to return the 'thumbnail' URL? (or any other size)
I'm sure it's a fairly simple change but I just can't figure it out.
Use thumb_src instead of url.
$product = new WC_Product_Variable( $product_id );
$variations = $product->get_available_variations();
foreach ( $variations as $variation ) {
echo "<img src=" . $variation['image']['thumb_src'] .">";
}
If you know the product variation ID, you can do
// find out $variation_id, it's different from product_id
$variation = new WC_Product_Variation( $variation_id );
$image_tag = $variation->get_image();
// The below is the whole image tag including <img> and some classes that will help customize it.
echo $image_tag;
See related docs found at the Woocommerce reference for WC_product_variation class and the WC_Product class. The function get_image() is actually inherited from WC_Product, but you can use it in either class.
Since WooCommerce version 3.0.0 you can get the ID of a variation image as:
$image_id = $variation->get_image_id();
This will return the variation image ID if available, otherwise, the main product image ID will be returned. A variation image could be found in the product back-end as in the image below:
You can then use this image ID to get the URL to your variation or product thumbnail image as:
$image_array = wp_get_attachment_image_src($image_id, 'thumbnail');
$image_src = $image[0];
The code snippet is tested and working.
i have different colors and sizes of clothing.
Is it possible to omit the size and only show the thumbnail of a single color
Right now, it is showing thumbnails for color red in small, medium, and large size. Three duplicated thumbnails.

how get the correct term_id for each article

I am using two plugins WPCustom Category Image for Be able to insert an image in each category and W4 Post List for list the categories on the screen.
In this moment give me always the last image of categories. I pretend the image of each category corresponding to each ID.
The plugin have one shortcode [term_id] which shows the corresponding category id but i can't use one shortcode inside from another shortcode.
Example - [wp_custom_image_category onlysrc="false" size="full" term_id="[term_id"]]
Any solution?
foreach( get_categories(['hide_empty' => false]) as $category) {
$image = do_shortcode('[wp_custom_image_category onlysrc="false" size="full" term_id="'.$category->term_id.'" ]');
echo $image.'<br/>';
// $id = 4;
$options['template'] =
'[terms]
<div class="col-sm-3 news-cat-links" id="[term_id]">
[term_name]<img src="'.$image.'" /></a>
</div>[/terms]';
}
Go to corresponding plugin files and find the functions of [term_id] shortcode.
below code returns array of custom texanomy named "eventcategory". define $post as global variable .
$terms = get_the_terms( $post->ID, 'eventcategory' );
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->name;
}
print_r($draught_links);

Get for a specific attribute of a variation the corresponding data value

I'm editing the variable-subscription.php file in attempt to display a specific WooCommerce Variable Subscription attribute, along with certain attribute data. Here's my code so far, you can see I'm trying to display the attribute name along with the variation image and description.
<?php
// get all variations
$variations = $product->get_available_variations();
// get shirt values
$shirt_values = get_the_terms( $product_id, 'pa_shirt');
// for each shirt value
foreach ( $shirt_values as $shirt_value ) { ?>
<h1>
<?php // name
echo echo $shirt_value->name;
?>
</h1>
<?php // description
foreach ( $variations as $variable_array ){
$variation = new WC_Product_Variation( $variable_array['variation_id'] );
echo $variation->variation_description;
}
?>
<?php // image
foreach ( $variations as $variation ) { ?>
<img src="<?php echo $variation['image_src']; ?>">
<?php }
?>
Currently, I have 3 shirts for sale (defined in the custom product attributes) and then I have several variations. The problem is this PHP shows ALL possible description/image variations for each shirt, and I only want to show the variations related to each specific shirt attribute.
You need to use with the class WC_Product_Variation the method get_variation_attributes() that will returns an array of the attributes and their values for this variation.
So for attribute pa_shirt your code to display the value for a specific variation is going to be:
$variations = $product->get_available_variations();
// Here we use our method to get the array of attribute values for the variation
$variation_attribute = $variation->get_variation_attributes();
// Here is the value for this variation and "pa_shirt" attribute
$variation_attrib_shirt_value = $variation_attribute['attribute_pa_shirt'];
As you can notice the attributes keys slugs in the array have all 'attribute_' in the beginning.

Categories