Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AnanthFlycart/dd280e028bdc51219398a4b6aec664f6 to your computer and use it in GitHub Desktop.
Save AnanthFlycart/dd280e028bdc51219398a4b6aec664f6 to your computer and use it in GitHub Desktop.
UpsellWP: Brief "Choose any" attribute variants
add_filter('cuw_get_product_data', function ($data, $product, $args) {
if (empty($args['to_display'])) {
return $data;
}
if (!empty($data['is_variable']) && method_exists($product, 'get_available_variations')) {
$has_choose_any_variant = false;
$available_variations = $product->get_available_variations();
if (!empty($available_variations) && is_array($available_variations)) {
foreach ($available_variations as $variation_data) {
if (!empty($variation_data['attributes'])) {
foreach ($variation_data['attributes'] as $value) {
if ($value === '') {
$has_choose_any_variant = true;
break;
}
}
}
}
}
if (!$has_choose_any_variant || empty($data['variants']) || !function_exists('wc_attribute_label')) {
return $data;
}
$attribute_labels = [];
$parent_attributes = $product->get_attributes();
foreach ($parent_attributes as $slug => $attribute) {
$attribute_labels[$slug] = wc_attribute_label($attribute->get_name());
}
$variants = [];
foreach ($data['variants'] as $variant) {
$is_choose_any_variant = false;
if (!empty($variant['attributes'])) {
foreach ($variant['attributes'] as $value) {
if ($value === '') {
$is_choose_any_variant = true;
break;
}
}
}
if ($is_choose_any_variant) {
$standalone_attributes = [];
$choose_any_attributes = [];
foreach ($variant['attributes'] as $slug => $value) {
if (!empty($value)) {
$standalone_attributes[$slug][] = [
'value' => $value,
'slug' => $slug,
'slug_label' => $attribute_labels[$slug],
'value_label' => $value,
];
} elseif (!empty($parent_attributes[$slug])) {
$attribute = $parent_attributes[$slug];
$terms = wc_get_product_terms($product->get_id(), $slug, ['fields' => 'all']);
foreach ($attribute['options'] as $attr_id) {
if (is_string($attr_id)) {
$choose_any_attributes[$slug][] = [
'value' => $attr_id,
'slug' => $slug,
'slug_label' => $attribute_labels[$slug] ?? $slug,
'value_label' => $attr_id,
];
} elseif (!empty($terms)) {
foreach ($terms as $term) {
if ($attr_id == $term->term_id) {
$choose_any_attributes[$slug][] = [
'value' => $term->slug,
'slug' => $slug,
'slug_label' => $attribute_labels[$slug],
'value_label' => $term->name,
];
break;
}
}
}
}
}
}
$i = 0;
$attribute_combinations = [];
if (!empty($standalone_attributes)) {
foreach ($standalone_attributes as $slug => $values) {
foreach ($values as $value) {
if (!empty($choose_any_attributes)) {
foreach ($choose_any_attributes as $c_slug => $c_values) {
foreach ($c_values as $c_value) {
$attribute_combinations[$i][] = $value;
$attribute_combinations[$i][] = $c_value;
$i++;
if ($i >= 1000) break 4;
}
}
}
}
}
} else {
if (count($choose_any_attributes) == 1) {
foreach ($choose_any_attributes as $slug => $values) {
foreach ($values as $value) {
$attribute_combinations[$i][] = $value;
$i++;
if ($i >= 1000) break 2;
}
}
} elseif (count($choose_any_attributes) == 2) {
foreach ($choose_any_attributes as $slug => $values) {
foreach ($values as $value) {
foreach ($choose_any_attributes as $c_slug => $c_values) {
foreach ($c_values as $c_value) {
if ($slug != $c_slug) {
$attribute_combinations[$i][] = $value;
$attribute_combinations[$i][] = $c_value;
$i++;
if ($i >= 1000) break 4;
}
}
}
}
unset($choose_any_attributes[$slug]);
}
} else {
return $data;
}
}
$id = $variant['id'];
foreach ($attribute_combinations as $key => $attributes) {
$vid = $id;
$info = [];
foreach ($attributes as $attribute) {
$vid .= '|' . $attribute['slug'] . ':' . $attribute['value'];
$info[] = $attribute['slug_label'] . ': ' . $attribute['value_label'];
}
$variant['id'] = $vid;
$variant['info'] = implode(', ', $info);
$variants[] = $variant;
}
} else {
$variants[] = $variant;
}
}
if (!empty($variants)) {
$data['variants'] = $variants;
}
} elseif (method_exists($product, 'get_attributes')) {
$data['attributes'] = $product->get_attributes();
}
return $data;
}, 100, 3);
if (!function_exists('cuw_reformat_item_data')) {
function cuw_reformat_item_data($data)
{
if (!empty($data['product']['variation_id']) && !is_numeric($data['product']['variation_id'])) {
$parts = explode('|', $data['product']['variation_id']);
$data['product']['variation_id'] = $parts[0];
unset($parts[0]);
$data['product']['variation'] = [];
foreach ($parts as $part) {
$attr = explode(':', $part);
$data['product']['variation']['attribute_' . $attr[0]] = $attr[1];
}
}
return $data;
}
add_filter('cuw_product_item_data', 'cuw_reformat_item_data', 100);
add_filter('cuw_offer_item_data', 'cuw_reformat_item_data', 100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment