Last active
June 4, 2024 07:25
-
-
Save Auke1810/e1eea6a01f5029eb004ba612b3ffb627 to your computer and use it in GitHub Desktop.
Populate the <g:product_detail> attribute with the product variation details automaticaly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wppfm_generate_product_detail_attribute($attributes, $feed_id, $product_id) { | |
$product = wc_get_product($product_id); | |
if (!$product) { | |
return $attributes; | |
} | |
$attribute_names = []; | |
$attribute_values = []; | |
$product_attributes = $product->get_attributes(); | |
// Check wether the attributes are in object or simple value | |
foreach ($product_attributes as $attribute_name => $attribute) { | |
if (is_a($attribute, 'WC_Product_Attribute')) { | |
// Attribute is object | |
if ($attribute->is_taxonomy()) { | |
$terms = wp_get_post_terms($product_id, $attribute->get_name(), 'all'); | |
$values = []; | |
foreach ($terms as $term) { | |
$values[] = $term->name; | |
} | |
$attribute_label = wc_attribute_label($attribute_name); | |
$attribute_value = implode(', ', $values); | |
$attribute_names[] = $attribute_label; | |
$attribute_values[] = $attribute_value; | |
} | |
} else { | |
// Attribuut simple value | |
$attribute_label = wc_attribute_label($attribute_name); | |
$attribute_value = $attribute; | |
$attribute_names[] = $attribute_label; | |
$attribute_values[] = $attribute_value; | |
} | |
} | |
if ($attribute_names && $attribute_values) { | |
$attributes['product_detail-attribute_name'] = implode('||', $attribute_names); | |
$attributes['product_detail-attribute_value'] = implode('||', $attribute_values); | |
} | |
return $attributes; | |
} | |
add_filter('wppfm_feed_item_value', 'wppfm_generate_product_detail_attribute', 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please make sure to remember the following instructions:
This script is designed to automatically populate the <g:product_detail> with the product attributes, including their names and values. To achieve this, you need to add the "product_detail-attribute_name" and "product_detail-attribute_value" attributes to the product feed and populate them with data. This data will be replaced by the script.
If you are not familiar with using filters in your wp-admin, you can easily achieve this by using a plugin such as "Code Snippets" (https://wordpress.org/plugins/code-snippets/). A helpful video demonstrating the use of this plugin can be found at https://www.youtube.com/watch?v=I1_ZqnQmwJs.
Once you have activated the Code Snippets plugin, you can safely use the provided code to add the required functionality.