resolve_product_id($attachment_id); } if (isset($product_id)) { $brand = $this->get_brand_if_exist($product_id); $product_title = $this->get_product_title_if_exist($product_id); $color_attr_name = get_option('alttextlab_ecomm_color_field', ''); $material_attr_name = get_option('alttextlab_ecomm_material_field', ''); if ($color_attr_name) { $color = $this->get_attribute_value_if_exist($product_id, $color_attr_name); } if ($material_attr_name) { $material = $this->get_attribute_value_if_exist($product_id, $material_attr_name); } } if($brand && (get_option('alttextlab_ecomm') === 'yes')){ $result_product_info['brand'] = $brand; } if($product_title && (get_option('alttextlab_ecomm_use_title') === 'yes')){ $result_product_info['product'] = $product_title; } if($material && (get_option('alttextlab_ecomm_use_material') === 'yes')){ $result_product_info['material'] = $material; } if($color && (get_option('alttextlab_ecomm_use_color') === 'yes')){ $result_product_info['color'] = $color; } return $result_product_info; } private function resolve_product_id($attachment_id) { global $wpdb; $sql = " SELECT parent_posts.ID as product_id FROM {$wpdb->posts} parent_posts INNER JOIN {$wpdb->posts} child_posts ON child_posts.post_parent = parent_posts.ID WHERE child_posts.ID = %d AND parent_posts.post_type = 'product' AND parent_posts.post_status <> 'auto-draft' "; $product = $wpdb->get_row($wpdb->prepare($sql, $attachment_id)); return !empty($product) ? $product->product_id : null; } private function get_brand_if_exist($product_id) { $brand_terms = get_the_terms($product_id, 'product_brand'); return !empty($brand_terms) ? $brand_terms[0]->name : null; } private function get_product_title_if_exist($product_id) { $product = wc_get_product($product_id); return (!$product || !is_a($product, 'WC_Product')) ? null : $product->get_name(); } private function get_attribute_value_if_exist($product_id, $attribute_name) { $product = wc_get_product($product_id); if (!$product || !$attribute_name || $product->get_type() !== 'simple') { return null; } $attributes = $product->get_attributes(); $taxonomy_attr_name = 'pa_' . $attribute_name; if (isset($attributes[$taxonomy_attr_name]) && $attributes[$taxonomy_attr_name]->is_taxonomy()) { $terms = get_the_terms($product_id, $taxonomy_attr_name); if (!empty($terms) && !is_wp_error($terms)) { return $terms[0]->name; } } if (isset($attributes[$attribute_name]) && !$attributes[$attribute_name]->is_taxonomy()) { $options = $attributes[$attribute_name]->get_options(); if (!empty($options)) { return $options[0]; } } return null; } /** * Retrieves and filters e-commerce information for use in alt text generation. * */ public function filtered_ecomm_data($attachment_id, $ecomm_data) { $ecomm_data = apply_filters('alttextlab_ecomm_data', $ecomm_data, $attachment_id); return $ecomm_data; } }