I am using woocommerce and have 5 variable products on one page. They all use a attributes of height, width and color. What I would like to do is, if product one selects color of red, I want it also to be selected in the other 4 products. Same for height and width.
I thought this would be as simple as just forcing the dropdown selection with jQuery. So I started looking and I found a few things. They all seemed to be cloning the html into the field of product-2. So if we selected red in product-1, product-2 would have 2 red as options in the dropdown with the clone selected.
I ultimately tweaked something I had found to .change instead of .clone. I used the name because the select id's are the same on all products.
jQuery("select[name='wccp_attribute_pa_length[1488195634]'").change(function() {
alert('working');
if (jQuery(this).data('options') == undefined) {
/*Taking an array of all options-2 and kind of embedding it on the select1*/
jQuery(this).data('options', jQuery('select[name="component_1486761669_bundle_attribute_pa_length_186695"] option').change());
}
var id = jQuery(this).val();
var options = jQuery(this).data('options').filter('[value=' + id + ']');
jQuery('select[name="component_1486761669_bundle_attribute_pa_length_186695"]').html(options);
});
At first glance it seemed to be doing the trick. It visually changed the dropdown in the second product. However if I select a different variation option manually(such as color) it reverts to the first option of the dropdown. And secondly this is not updating the product price. Its as if woocommerce doesnt know the variation is selected.
I am not married to using the above code if anyone knows of a better way. I tried looking for some kind of product filter but I came up empty in my search. I am a noob with woocommerce.
I am really stuck. any help is appreciated, as always.
Cheers
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
return $term . ' (' . woocommerce_price( $_product->get_price() ) . ')';
}
return $term;
}
Related
i need a simply function that allow me to add or replace empty sku code for variation in my published product with parent sku of product.
is there a way to obtain this work?
Updated: To replace empty SKU code for variation in your published product with the parent SKU (the variable product SKU), try this function that will only work for admin user role.
Do a database backup before runing this
To run this function, browse your shop page:
It will update 2500 empty variations SKU each time.
A message will display the count of updated variations.
Each time you will need to reload the page to process 2000 variations SKUs.,
When all variations SKUs will be updated, a message let you know that the job is done.
The code:
add_action('woocommerce_before_main_content', 'update_variations_sku', 100 );
function update_variations_sku(){
global $wpdb;
$results = $results2 = array();
$limit = 2500; // Number of variations to process
// The SQL query (get the variations with empty sku and their parent product ID)
$query = $wpdb->get_results( "
SELECT p.ID, p.post_parent
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type LIKE 'product_variation'
AND p.post_status LIKE 'publish'
AND pm.meta_key LIKE '_sku'
LIMIT $limit
" );
if( count($query) == 0 ){
echo "<p><pre>The job is finished, you can remove the code</pre></p>";
return; // exit
}
// Loop through variation Ids and get the parent variable product ID
foreach( $query as $value )
$results[$value->post_parent][] = $value->ID;
// Loop through variation Ids and set the parent sku in related empty variations skus
foreach($results as $parent_id => $variation_ids){
$parent_sku = get_post_meta( $parent_id, '_sku', true );
$count = 0;
foreach($variation_ids as $variation_id){
$count++;
update_post_meta( $variation_id, '_sku', $parent_sku.'-'.$count );
}
}
if( count($query) < $limit ){
echo "<p><pre>The job is finished, you can remove the code <br>$count variations SKUs have been updated</pre></p>";
} else {
echo "<p><pre>$count variations SKUs have been updated (continue reloading the page again)</pre></p>";
}
}
Code goes in function.php file of your active child theme (or theme).
Tested and works.
(Hopefully this make sense)
I need to be able to either display the individual prices of a variation as you go in woocommerce so a customer knows how much the variation difference is or I need the drop down prices to change so that they make sense visually.
I have used the following code
add_filter( 'woocommerce_variation_option_name',
'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
$_currency = get_woocommerce_currency_symbol();
return $term . ' ('.$_currency.' '. $_product->get_price() . ')';
}
return $term;
}
but as you will see this from this image it does not display correctly and looks a little confusing.
[https://i.stack.imgur.com/oQ9RH.png][1]
Any help will be much appreciated.
Many of my products have prices like £3.4560 etc. I need them to rounded and stripped to just 2 in the database.
The output of 2 decimal places in WooCommerce is not enough and fails to work with a Tax Toggle plugin.
Is there a database query that can do this?
I've seen some bits on Rounding and Truncate but not sure how to execute this as my knowledge is poor.
Any help is appreciated.
Update 2: (Added some code to clear all related product transient cache)
Here is some code that will update all the product prices (make a database backup before):
global $wpdb;
$postmeta = $wpdb->prefix . "postmeta";
$posts = $wpdb->prefix . "posts";
// 1. First query: Get all prices
$results = $wpdb->get_results( "
SELECT $postmeta.*
FROM $postmeta
INNER JOIN $posts ON $postmeta.post_id = $posts.ID
WHERE $posts.post_type LIKE '%product%'
AND $postmeta.meta_key LIKE '%price%'
AND $postmeta.meta_value != ''
ORDER BY $postmeta.meta_id ASC
" );
// iterating through each price and update it
foreach($results as $result){
$meta_id = $result->meta_id;
$post_id = $result->post_id;
$meta_key = $result->meta_key;
$meta_value = number_format( $result->meta_value, 2 );
// 2. Udating prices query
$wpdb->query( $wpdb->prepare( "
UPDATE $postmeta
SET meta_id = $meta_id, post_id = $post_id, meta_key = '$meta_key', meta_value = '$meta_value'
WHERE $postmeta.meta_id = %d
", $meta_id ) );
// 3. Clear all related product transient cached data (refresh prices)
wc_delete_product_transients($post_id);
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Once saved, just browse any page of your site, check your database wp_postmeta table searching for ‰price‰ LIKE meta_key. Now you can remove this code.
I want to display variation with the prices instead of regular prices at the front page, not in single item page.
Now: 10-100$
I need:
- Rent 10$
- Buy 100$
To show the prices for variations I use this function in functions.php.
//Add prices to variations
add_filter( 'woocommerce_variation_option_name', `enter code here`'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
//this is where you can actually customize how the price is displayed
return $term . ' (' . woocommerce_price( $_product->get_price() ) . ')';
}
return $term;
}
But how can I request the variations in the loop of the shop page? I try to use get_available_variations(); but it didn't work for me.
as in title, i'm trying to get sum of my variable product (for each product) in admin instead of 'out-of-stock' or 'in-stock' indication. I've variable product so i need sum of one variation to get all my product (ive a pant in different size and different colour but i need only sum of all size). Anyone got this before me?
Thanks in advance, Francesco
Finally i found solution.
First of all added this
function add_qty_admin( $column ) {
if (!isset($columns['total_qty']))
$columns['total_qty'] = "Quantità";
return $columns;
}
add_filter( 'manage_posts_columns', 'add_qty_admin' );
to functions.php in order to have a column in my backend.
Then i've added also this
function admin_post_data_row($column_name, $post_id)
{
global $wpdb;
switch($column_name)
{
case 'total_qty':
$query = "SELECT sum(meta_value)
FROM $wpdb->posts AS p, $wpdb->postmeta AS s
WHERE p.post_parent = %d
AND p.post_type = 'product_variation'
AND p.post_status = 'publish'
AND p.id = s.post_id
AND s.meta_key = '_stock'";
$product_qty = $wpdb->get_var($wpdb->prepare($query,$post_id));
if ($product_qty) echo $product_qty;
break;
default:
break;
}
}
add_action( 'manage_posts_custom_column', 'admin_post_data_row', 10, 2);
where $query is SQL query that sum all _stock value in postmeta table for post_parent posts that are the variations of post_id.
Sorry for my poor english (i'm italian), i hope this code can be helpful fot other people.
Francesco.