Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Micemade/e44d3ed83bba2dd9779a734fc6ebf0f7 to your computer and use it in GitHub Desktop.
Save Micemade/e44d3ed83bba2dd9779a734fc6ebf0f7 to your computer and use it in GitHub Desktop.
Un-assign "Used for variations" from WooCommerce product attribute if the "pa_size" attribute is "Universal size"
<?php
/**
* Un-assign "Used for variations" from product attribute
* if the "pa_size" attribute is "Universal size"
* because of possible multiple version of universal sizes,
* we search the attribute which slug starts with a "uni"
*/
function unassign_variation_to_attribute() {
/**
* ### Styles for debug output
*/
echo '<style>pre{width: 49%;float:left;font-size:12px!important;line-height:1.25rem;background-image: linear-gradient(0deg, #f5d2d9 2.50%, #fafafa 2.50%, #fafafa 50%, #f5d2d9 50%, #f5d2d9 52.50%, #fafafa 52.50%, #fafafa 100%);background-size: 1.25rem 1.25rem;position: relative; z-index:10000; margin:0}</style>';
// Get all the products.
$args = array(
'post_type' => 'product',
'post_status' => 'any',
'posts_per_page' => -1, // -1 for all the products, can be performance heavy.
'suppress_filters' => true,
'cache_results' => false, // suppress errors when large number of posts (memory).
);
$products_obj = get_posts( $args );
if ( ! empty( $products_obj ) ) {
$has_uni_size = false;
// EACH PRODUCT.
$_pf = new WC_Product_Factory();
foreach ( $products_obj as $single_product ) {
$prod_id = $single_product->ID;
$prodname = $single_product->post_title;
$product = $_pf->get_product( $prod_id );
$attributes = array_filter( $product->get_attributes() );
// EACH ATTRIBUTE OF PRODUCT.
foreach ( $attributes as $attribute ) {
$attr_name = $attribute->get_name();
// IF ATTRIBUTE "PA_SIZE" and IS SET AS VARIATION.
if ( 'pa_size' === $attr_name ) {
// If attribute is used for variation(s).
$is_variation = $attribute->get_variation();
if ( $attribute->is_taxonomy() && $is_variation ) {
$attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) );
foreach ( $attribute_values as $attribute_value ) {
// If attribute term slug starts with "uni".
if ( 'uni' === substr( $attribute_value->slug, 0, 3 ) ) {
$has_uni_size = true;
unset( $attributes );
continue;
}
}
}
}
}
// Get array of product attributes (PA), saved as '_product_attributes' meta.
$meta_arr = get_post_meta( $prod_id, '_product_attributes', true );
// Duplicate array of meta PA.
$__meta = array();
foreach ( $meta_arr as $key => $value ) {
$__meta[ $key ] = $value;
if ( 'pa_size' === $key ) {
$old_value = $__meta[ $key ]['is_variation'];
if ( $has_uni_size ) {
// ### Output array pair, matching upper criteria - for debug
$__meta[ $key ]['is_variation'] = '<span style="color:white;background:red;font-weight: bold;padding:0 5px;">IS VARIATION WITH UNISIZE</span>';
// ### Replace the value
// $__meta[ $key ]['is_variation'] = 0;
} else {
// If criteria doesn't match - keep the old value.
$__meta[ $key ]['is_variation'] = $old_value;
}
}
}
/**
* ### THE DEBUG OUTPUT:
* Two preformatted columns - compare to check
* First is the original product meta
* Second is a copy with located values for replacement
* when everthing ok, comment debug output and,
* uncomment the update_post_meta line
*/
// Original meta.
echo '<pre>';
print_r( $prod_id . ' -' . $prodname . '<br>' );
print_r( $meta_arr );
echo '</pre>';
// Copied meta, with located values.
echo '<pre>';
print_r( $prod_id . ' - ' . $prodname . '<br>' );
print_r( $__meta );
echo '</pre>';
/**
* ### CAREFULL: WHEN UNCOMMENTED, THE LINE BELLOW CAN MESS UP
* PRETTY BADLY, IF EVERYTHING ABOVE ISN'T SET RIGHT
* FIRST, CHECK EVERTYHING WITH DEBUG OUTPUT ABOVE
*/
// update_post_meta( $prod_id, '_product_attributes', $__meta );
// Reset display of duplicated product meta and
// Universal size existance boolean.
unset( $__meta );
$has_uni_size = false;
} // end of each product.
}
}
/**
* We used admin_init hook here,but perhaps would be advisable to
* simply output the unassign_variation_to_attribute() function
* in newly created blank template, not used anywhere, especially
* it this is run in production (live) site.
*/
if ( is_admin() ) {
add_action( 'admin_init', 'unassign_variation_to_attribute' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment