Product Taxonomies and showing Variations in Single Product page
-
Hello experts! I’ll first provide some CONTEXT and then I’ll describe my ISSUE.
CONTEXT:
I’m currently building a woocommerce store for dropshipping several shopify stores. Each of shopify has its own particular taxonomy. I am successfully bringing information from the first shopify store such as title, variants, images, body_html, product_type, vendor and an attribute with “name”: “Size” and its variants such as “price” and “inventory_quantity”.
In order to register the custom taxonomies in woocommerce, the following lines were added to the theme’s functions.php file:
<?php
function register_product_custom_taxonomies() {
$attributes = [
'store' => 'Store',
'product_kind' => 'Product Kind',
'size' => 'Size',
'color' => 'Color',
'vendor' => 'Vendor',
'style' => 'Style',
'axle_size' => 'Axle Size',
'size_kids' => 'Size (Kids)',
'pads_vendor' => 'Pads Vendor',
'vol_wax' => 'Volume (Wax)',
'vol_degrease' => 'Volume (Degrease)',
'valve_color' => 'Valve Color',
'vol_sealant' => 'Volume (Sealant)',
'stem_diameter' => 'Stem Diameter',
'socks_design' => 'Socks Design'
];
foreach ($attributes as $slug => $label) {
$taxonomy_slug = 'custom_' . $slug; // Prefix for uniqueness
register_taxonomy(
$taxonomy_slug,
['product'],
[
'labels' => [
'name' => $label,
'singular_name' => $label,
],
'hierarchical' => false,
'show_ui' => true,
'public' => true,
'show_in_menu' => true,
'show_admin_column' => true,
'show_in_rest' => true,
'rewrite' => ['slug' => sanitize_title($label)],
]
);
}
}
add_action('init', 'register_product_custom_taxonomies');
?>The reason I decided to use custom taxonomies is because I want to use the plugin called “Filter Everything PRO” to created taxonomy-based filters in the Products Archive page.
It seems like registering taxonomies alone is not enough so I also performed Attribute Registration in WooCommerce, the following lines were ran once in the theme’s functions.php file:
<?php
function register_custom_global_attributes_with_sql() {
global $wpdb;
$attributes = [
'custom_store' => 'Store',
'custom_product_kind' => 'Product Kind',
'custom_size' => 'Size',
'custom_color' => 'Color',
'custom_vendor' => 'Vendor',
'custom_style' => 'Style',
'custom_axle_size' => 'Axle Size',
'custom_size_kids' => 'Size (Kids)',
'custom_pads_vendor' => 'Pads Vendor',
'custom_vol_wax' => 'Volume (Wax)',
'custom_vol_degrease' => 'Volume (Degrease)',
'custom_valve_color' => 'Valve Color',
'custom_vol_sealant' => 'Volume (Sealant)',
'custom_stem_diameter' => 'Stem Diameter',
'custom_socks_design' => 'Socks Design',
];
foreach ($attributes as $slug => $name) {
// Check if the attribute exists in the WooCommerce database
$exists = $wpdb->get_var($wpdb->prepare(
"SELECT attribute_id FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s",
$slug
));
if (!$exists) {
// Insert attribute into the database via SQL query
$wpdb->insert(
"{$wpdb->prefix}woocommerce_attribute_taxonomies",
[
'attribute_name' => $slug,
'attribute_label' => $name,
'attribute_type' => 'select',
'attribute_orderby'=> 'menu_order',
'attribute_public' => 1,
]
);
}
}
// Refresh WooCommerce's cached attributes
delete_transient('wc_attribute_taxonomies');
}
add_action('init', 'register_custom_global_attributes_with_sql');
?>ISSUE:
When any of the programatically created products are seen in the Single Product page, variations are not being shown. If I omit the custom taxonomies from the theme’s functions.php, variations on the single product page work just fine.
The page I need help with: [log in to see the link]
- You must be logged in to reply to this topic.