public function __construct($attrs = null, $product_id = null) { if ($attrs == null) { return; } $this->product_id = $product_id; if (is_string($attrs['value'])) { $attrs['value'] = array_map('trim', explode('|', $attrs['value'])); } if (intval($attrs['is_taxonomy']) == 1 && $this->product_id) { $cat = new Category(); $cattrs = woocommerce_get_product_terms($this->product_id, $attrs['name'], 'all'); $attrs['value'] = array(); foreach ($cattrs as $catt) { $cat->fromDatabaseResult(Helpers::std2a($catt)); $attrs['value'][] = $cat->asApiArray(); } } $attrs['is_visible'] = $this->toWPBool($attrs['is_visible']); $attrs['is_variation'] = $this->toWPBool($attrs['is_variation']); $attrs['is_taxonomy'] = $this->toWPBool($attrs['is_taxonomy']); $this->attrs = $attrs; Helpers::debug("ProductAttribute::__construct" . var_export($attrs, true)); }
$alt = $alt * -1; ?> <tr class="<?php if ($alt == 1) { echo 'alt'; } ?> "> <th><?php echo $woocommerce->attribute_label($attribute['name']); ?> </th> <td><?php if ($attribute['is_taxonomy']) { $values = woocommerce_get_product_terms($product->id, $attribute['name'], 'names'); echo apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values); } else { // Convert pipes to commas and display values $values = array_map('trim', explode('|', $attribute['value'])); echo apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values); } ?> </td> </tr> <?php } ?> </table>
/** * Returns a single product attribute. * * @access public * @param mixed $attr * @return mixed */ function get_attribute($attr) { $attributes = $this->get_attributes(); $attr = sanitize_title($attr); if (isset($attributes[$attr]) || isset($attributes['pa_' . $attr])) { $attribute = isset($attributes[$attr]) ? $attributes[$attr] : $attributes['pa_' . $attr]; if ($attribute['is_taxonomy']) { return implode(', ', woocommerce_get_product_terms($this->id, $attribute['name'], 'names')); } else { return $attribute['value']; } } return false; }
public static function wc_get_product_terms($object_id, $taxonomy, $fields = 'all') { global $woocommerce; if (self::is_wc_version_gte_2_1()) { return wc_get_product_terms($object_id, $taxonomy, array('fields' => $fields)); } else { return woocommerce_get_product_terms($object_id, $taxonomy, $fields); } }
/** * Return the maximum points discount at the category level if set. If a percentage multiplier is set (e.g. 35%), * the maximum discount is equal to the product's price times the percentage * * @since 1.0 * @param object $product the product to get the maximum discount for * @return float|string the maximum discount */ private static function get_category_max_discount($product) { $category_ids = woocommerce_get_product_terms($product->id, 'product_cat', 'ids'); $category_max_discount = ''; foreach ($category_ids as $category_id) { $max_discount = get_woocommerce_term_meta($category_id, '_wc_points_max_discount', true); // if a percentage modifier is set, set the maximum discount using the price of the product if (false !== strpos($max_discount, '%')) { $max_discount = self::calculate_discount_modifier($max_discount, $product); } // get the minimum discount if the product belongs to multiple categories with differing maximum discounts if (!is_numeric($category_max_discount) || $max_discount < $category_max_discount) { $category_max_discount = $max_discount; } } return $category_max_discount; }
private function get_product_from_post($post_id) { $woocommerce_ver_below_2_1 = false; if (version_compare(WOOCOMMERCE_VERSION, '2.1', '<')) { $woocommerce_ver_below_2_1 = true; } if ($woocommerce_ver_below_2_1) { $product = new WC_Product_Simple($post_id); } else { $product = new WC_Product($post_id); } //$post_categories = wp_get_post_categories( $post_id ); //$categories = get_the_category(); try { $thumbnail = $product->get_image(); if ($thumbnail) { if (preg_match('/data-lazy-src="([^\\"]+)"/s', $thumbnail, $match)) { $thumbnail = $match[1]; } else { if (preg_match('/data-lazy-original="([^\\"]+)"/s', $thumbnail, $match)) { $thumbnail = $match[1]; } else { if (preg_match('/lazy-src="([^\\"]+)"/s', $thumbnail, $match)) { // Animate Lazy Load Wordpress Plugin $thumbnail = $match[1]; } else { if (preg_match('/data-echo="([^\\"]+)"/s', $thumbnail, $match)) { $thumbnail = $match[1]; } else { preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $thumbnail, $result); $thumbnail = array_pop($result); } } } } } } catch (Exception $e) { $err_msg = "exception raised in thumbnails"; self::send_error_report($err_msg); $thumbnail = ''; } // handling scheduled sale price update if (!$woocommerce_ver_below_2_1) { $sale_price_dates_from = get_post_meta($post_id, '_sale_price_dates_from', true); $sale_price_dates_to = get_post_meta($post_id, '_sale_price_dates_to', true); if ($sale_price_dates_from || $sale_price_dates_to) { self::schedule_sale_price_update($post_id, null); } if ($sale_price_dates_from) { self::schedule_sale_price_update($post_id, $sale_price_dates_from); } if ($sale_price_dates_to) { self::schedule_sale_price_update($post_id, $sale_price_dates_to); } } $product_tags = array(); foreach (wp_get_post_terms($post_id, 'product_tag') as $tag) { $product_tags[] = $tag->name; } $product_brands = array(); if (taxonomy_exists('product_brand')) { foreach (wp_get_post_terms($post_id, 'product_brand') as $brand) { $product_brands[] = $brand->name; } } $taxonomies = array(); try { $all_taxonomies = get_option('wcis_taxonomies'); if (is_array($all_taxonomies)) { foreach ($all_taxonomies as $taxonomy) { if (taxonomy_exists($taxonomy) && !array_key_exists($taxonomy, $taxonomies)) { foreach (wp_get_post_terms($post_id, $taxonomy) as $taxonomy_value) { if (!array_key_exists($taxonomy, $taxonomies)) { $taxonomies[$taxonomy] = array(); } $taxonomies[$taxonomy][] = $taxonomy_value->name; } } } } } catch (Exception $e) { } $acf_fields = array(); try { if (class_exists('acf') && function_exists('get_field')) { $all_acf_fields = get_option('wcis_acf_fields'); if (is_array($all_acf_fields)) { foreach ($all_acf_fields as $acf_field_name) { $acf_field_value = get_field($acf_field_name, $post_id); if ($acf_field_value) { $acf_fields[$acf_field_name] = $acf_field_value; } } } } } catch (Exception $e) { } $send_product = array('product_id' => $product->id, 'currency' => get_woocommerce_currency(), 'price' => $product->get_price(), 'url' => get_permalink($product->id), 'thumbnail_url' => $thumbnail, 'action' => 'insert', 'description' => $product->get_post_data()->post_content, 'short_description' => $product->get_post_data()->post_excerpt, 'name' => $product->get_title(), 'sku' => $product->get_sku(), 'categories' => $product->get_categories(), 'tag' => $product_tags, 'store_id' => get_current_blog_id(), 'identifier' => (string) $product->id, 'product_brand' => $product_brands, 'taxonomies' => $taxonomies, 'acf_fields' => $acf_fields, 'sellable' => $product->is_purchasable(), 'visibility' => $product->is_visible(), 'stock_quantity' => $product->get_stock_quantity(), 'is_managing_stock' => $product->managing_stock(), 'is_backorders_allowed' => $product->backorders_allowed(), 'is_purchasable' => $product->is_purchasable(), 'is_in_stock' => $product->is_in_stock(), 'product_status' => get_post_status($post_id)); try { $variable = new WC_Product_Variable($post_id); $variations = $variable->get_available_variations(); $variations_sku = ''; if (!empty($variations)) { foreach ($variations as $variation) { if ($product->get_sku() != $variation['sku']) { $variations_sku .= $variation['sku'] . ' '; } } } $send_product['variations_sku'] = $variations_sku; $all_attributes = $product->get_attributes(); $attributes = array(); if (!empty($all_attributes)) { foreach ($all_attributes as $attr_mame => $value) { if ($all_attributes[$attr_mame]['is_taxonomy']) { if (!$woocommerce_ver_below_2_1) { $attributes[$attr_mame] = wc_get_product_terms($post_id, $attr_mame, array('fields' => 'names')); } else { $attributes[$attr_mame] = woocommerce_get_product_terms($post_id, $attr_mame, 'names'); } } else { $attributes[$attr_mame] = $product->get_attribute($attr_mame); } } } $send_product['attributes'] = $attributes; $send_product['total_variable_stock'] = $variable->get_total_stock(); try { if (version_compare(WOOCOMMERCE_VERSION, '2.2', '>=')) { if (function_exists('wc_get_product')) { $original_product = wc_get_product($product->id); if (is_object($original_product)) { $send_product['visibility'] = $original_product->is_visible(); $send_product['product_type'] = $original_product->product_type; } } } else { if (function_exists('get_product')) { $original_product = get_product($product->id); if (is_object($original_product)) { $send_product['visibility'] = $original_product->is_visible(); $send_product['product_type'] = $original_product->product_type; } } } } catch (Exception $e) { } } catch (Exception $e) { $err_msg = "exception raised in attributes"; self::send_error_report($err_msg); } if (!$woocommerce_ver_below_2_1) { try { $send_product['price_compare_at_price'] = $product->get_regular_price(); $send_product['price_min'] = $variable->get_variation_price('min'); $send_product['price_max'] = $variable->get_variation_price('max'); $send_product['price_min_compare_at_price'] = $variable->get_variation_regular_price('min'); $send_product['price_max_compare_at_price'] = $variable->get_variation_regular_price('max'); } catch (Exception $e) { $send_product['price_compare_at_price'] = null; $send_product['price_min'] = null; $send_product['price_max'] = null; $send_product['price_min_compare_at_price'] = null; $send_product['price_max_compare_at_price'] = null; } } else { $send_product['price_compare_at_price'] = null; $send_product['price_min'] = null; $send_product['price_max'] = null; $send_product['price_min_compare_at_price'] = null; $send_product['price_max_compare_at_price'] = null; } $send_product['description'] = self::content_filter_shortcode_with_content($send_product['description']); $send_product['short_description'] = self::content_filter_shortcode_with_content($send_product['short_description']); $send_product['description'] = self::content_filter_shortcode($send_product['description']); $send_product['short_description'] = self::content_filter_shortcode($send_product['short_description']); try { if (defined('ICL_SITEPRESS_VERSION') && is_plugin_active('woocommerce-multilingual/wpml-woocommerce.php') && function_exists('wpml_get_language_information')) { if (version_compare(ICL_SITEPRESS_VERSION, '3.2', '>=')) { $language_info = apply_filters('wpml_post_language_details', NULL, $post_id); } else { $language_info = wpml_get_language_information($post_id); } if ($language_info && is_array($language_info) && array_key_exists('locale', $language_info)) { // WP_Error could be returned from wpml_get_language_information(...) $send_product['lang'] = $language_info['locale']; } } } catch (Exception $e) { } return $send_product; }
public function getProductAttributes($desc) { $name = "attributes"; // if ( isset($this->_meta_attributes[$name])) { // return $this->_meta_attributes[$name]; // } else { $attrs = maybe_unserialize(get_post_meta($this->_actual_model_id, '_product_attributes', true)); if (is_string($attrs)) { $attrs = explode("|", $attrs); } if (!is_array($attrs)) { return array('error', __('Your attributes could not be decoded by the API')); } foreach ($attrs as &$attr) { if (!$attr) { continue; } if (intval($attr['is_taxonomy']) == 1) { $cat = new Category(); $cattrs = woocommerce_get_product_terms($this->_actual_model_id, $attr['name'], 'all'); $attr['value'] = array(); foreach ($cattrs as $catt) { $cat->fromDatabaseResult(Helpers::std2a($catt)); $attr['value'][] = $cat->asApiArray(); } } foreach (array('is_visible', 'is_variation', 'is_taxonomy') as $key) { if (isset($attr[$key])) { $attr[$key] = Helpers::toWPBool($attr[$key]); } } if (isset($attr['value']) && is_string($attr['value'])) { $attr['value'] = explode("|", $attr['value']); } } $this->_meta_attributes['attributes'] = $attrs; return $this->_meta_attributes['attributes']; //} }