Advertisement
Osama_Mersal

Custom attribute function

Feb 21st, 2024
894
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.86 KB | None | 0 0
  1. function testing_atts_wpml() {
  2.     global $product;
  3.  
  4.     $product_id = 68; // Example product ID
  5.     $product = wc_get_product($product_id);
  6.  
  7.     // Get the translated product ID and object
  8.     $ru_product_id = apply_filters('wpml_object_id', $product_id, 'product', true, 'ru');
  9.     $ru_product = wc_get_product($ru_product_id);
  10.  
  11.     // Get translated description and title
  12.     $ru_description = $ru_product->get_description();
  13.     $ru_title = $ru_product->get_title();
  14.  
  15.     // Initialize the output HTML
  16.     $param_val = "<div><h2>" . $ru_title . "</h2><p>" . $ru_description . "</p><table><tr><th>Taxonomy ID</th><th>Taxonomy name</th><th>Translated taxonomy name</th><th>Translated attribute value</th></tr>";
  17.  
  18.     // Iterate over attributes
  19.     $attributes = $ru_product->get_attributes();
  20.     foreach ($attributes as $attribute) {
  21.         $taxonomy   = $attribute->get_name();
  22.         $taxonomy_name = wc_attribute_label( $taxonomy );
  23.        
  24.         // Assuming attribute is a taxonomy
  25.         if ($attribute->is_taxonomy()) {
  26.             $translated_taxonomy_name = apply_filters('wpml_translate_single_string', $taxonomy_name, 'WordPress', 'Product attribute name', 'ru');
  27.             $attribute_values = $ru_product->get_attribute($taxonomy);
  28.         } else {
  29.             // For custom attributes, this approach may vary
  30.             $translated_taxonomy_name = $taxonomy_name; // Custom attributes might not have a taxonomy name
  31.             $attribute_values = $attribute->get_data()['value']; // Directly get the value for custom attributes
  32.         }
  33.  
  34.         $param_val .= "<tr><td>" . $attribute->get_id() . "</td><td>" . $taxonomy . "</td><td>" . $translated_taxonomy_name . "</td><td>" . $attribute_values . "</td></tr>";
  35.     }
  36.  
  37.     $param_val .= '</table></div>';
  38.  
  39.     return $param_val;
  40. }
  41.  
  42. add_shortcode('testing_atts_wpml', 'testing_atts_wpml');
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement