/** * Add woo attributes to a custom field with the same name * * @param $custom_fields * @param $post_id * * @return mixed */ public function filter_custom_fields($custom_fields, $post_id) { if (!isset($custom_fields)) { $custom_fields = array(); } // Get the product correponding to this post $product = new WC_Product($post_id); foreach ($product->get_attributes() as $attribute) { //$terms = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) ); // Remove the eventual 'pa_' prefix from the attribute name $attribute_name = $attribute['name']; if (substr($attribute_name, 0, 3) == 'pa_') { $attribute_name = substr($attribute_name, 3, strlen($attribute_name)); } $custom_fields[$attribute_name] = explode(',', $product->get_attribute($attribute['name'])); } return $custom_fields; }
/** * Get the attributes for a product or product variation * * @since 2.1 * @param WC_Product|WC_Product_Variation $product * @return array */ private function get_attributes($product) { $attributes = array(); if ($product->is_type('variation')) { // variation attributes foreach ($product->get_variation_attributes() as $attribute_name => $attribute) { // taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_` $attributes[] = array('name' => wc_attribute_label(str_replace('attribute_', '', $attribute_name)), 'slug' => str_replace('attribute_', '', str_replace('pa_', '', $attribute_name)), 'option' => $attribute); } } else { foreach ($product->get_attributes() as $attribute) { // taxonomy-based attributes are comma-separated, others are pipe (|) separated if ($attribute['is_taxonomy']) { $options = explode(',', $product->get_attribute($attribute['name'])); } else { $options = explode('|', $product->get_attribute($attribute['name'])); } $attributes[] = array('name' => wc_attribute_label($attribute['name']), 'slug' => str_replace('pa_', '', $attribute['name']), 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => array_map('trim', $options)); } } return $attributes; }
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; }
/** * Get the attributes for a product or product variation. * * @param WC_Product|WC_Product_Variation $product * @return array */ protected function get_attributes($product) { $attributes = array(); if ($product->is_type('variation')) { // Variation attributes. foreach ($product->get_variation_attributes() as $attribute_name => $attribute) { $name = str_replace('attribute_', '', $attribute_name); // Taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`. if (0 === strpos($attribute_name, 'attribute_pa_')) { $attributes[] = array('id' => wc_attribute_taxonomy_id_by_name($name), 'name' => $this->get_attribute_taxonomy_label($name), 'option' => $attribute); } else { $attributes[] = array('id' => 0, 'name' => str_replace('pa_', '', $name), 'option' => $attribute); } } } else { foreach ($product->get_attributes() as $attribute) { // Taxonomy-based attributes are comma-separated, others are pipe (|) separated. if ($attribute['is_taxonomy']) { $attributes[] = array('id' => $attribute['is_taxonomy'] ? wc_attribute_taxonomy_id_by_name($attribute['name']) : 0, 'name' => $this->get_attribute_taxonomy_label($attribute['name']), 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => array_map('trim', explode(',', $product->get_attribute($attribute['name'])))); } else { $attributes[] = array('id' => 0, 'name' => str_replace('pa_', '', $attribute['name']), 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => array_map('trim', explode('|', $product->get_attribute($attribute['name'])))); } } } return $attributes; }