-
-
Save jevgen/dd4c31115bf27ade907ad4ff326dc1a3 to your computer and use it in GitHub Desktop.
Extract different woocommerce fields easily
This file contains 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 woo_field($property) { | |
$date_format = 'd - M - Y'; //Format Date | |
global $woocommerce, $product; | |
$product_id = $product->get_id(); | |
$info = null; | |
switch ($property) { | |
case 'id': // Extracts the product ID | |
$info = $product->get_id(); | |
break; | |
case 'type': // Extracts the product type | |
$info = $product->get_type(); | |
break; | |
case 'name': // Extracts the product name | |
$info = $product->get_name(); | |
break; | |
case 'slug': // Extracts the product slug | |
$info = $product->get_slug(); | |
break; | |
case 'date_created': // Formats the creation date of the product | |
$info = date_i18n($date_format, strtotime($product->get_date_created()->date('Y-m-d'))); | |
break; | |
case 'date_modified': // Formats the modification date of the product | |
$info = date_i18n($date_format, strtotime($product->get_date_modified()->date('Y-m-d'))); | |
break; | |
case 'status': // Extracts the product status | |
$info = $product->get_status(); | |
break; | |
case 'featured': // Extracts if the product is featured and converts it to 'yes' or 'no' | |
$featured = $product->get_featured(); | |
$info = $featured ? 'yes' : 'no'; | |
break; | |
case 'catalog_visibility': // Extracts the product catalog visibility | |
$info = $product->get_catalog_visibility(); | |
break; | |
case 'description': // Extracts the product description | |
$info = $product->get_description(); | |
break; | |
case 'short_description': // Extracts the product short description | |
$info = $product->get_short_description(); | |
break; | |
case 'sku': // Extracts the product SKU | |
$info = $product->get_sku(); | |
break; | |
case 'menu_order': // Extracts the product menu order | |
$info = $product->get_menu_order(); | |
break; | |
case 'virtual': // Extracts if the product is virtual and converts it to 'yes' or 'no' | |
$virtual = $product->get_virtual(); | |
$info = $virtual ? 'yes' : 'no'; | |
break; | |
case 'price': // Adds the currency symbol before the product price | |
$info = get_woocommerce_currency_symbol() . $product->get_price(); | |
break; | |
case 'regular_price': // Adds the currency symbol before the regular product price | |
$info = get_woocommerce_currency_symbol() . $product->get_regular_price(); | |
break; | |
case 'sale_price': // Adds the currency symbol before the sale product price | |
$info = get_woocommerce_currency_symbol() . $product->get_sale_price(); | |
break; | |
case 'is_on_sale': // Verifies if the product is on sale | |
$is_on_sale = $product->is_on_sale(); | |
$info = $is_on_sale ? 'yes' : 'no'; | |
break; | |
case 'date_on_sale_from': // Formats the start date of sale of the product | |
$info = date_i18n($date_format, strtotime($product->get_date_on_sale_from())); | |
break; | |
case 'date_on_sale_to': // Formats the end date of sale of the product | |
$info = date_i18n($date_format, strtotime($product->get_date_on_sale_to())); | |
break; | |
case 'total_sales': // Extracts the total sales of the product | |
$info = $product->get_total_sales(); | |
break; | |
case 'tax_status': // Extracts the tax status of the product | |
$info = $product->get_tax_status(); | |
break; | |
case 'tax_class': // Extracts the tax class of the product | |
$info = $product->get_tax_class(); | |
break; | |
case 'manage_stock': // Extracts if the product manages stock and converts it to 'yes' or 'no' | |
$manage_stock = $product->get_manage_stock(); | |
$info = $manage_stock ? 'yes' : 'no'; | |
break; | |
case 'stock_quantity': // Extracts the stock quantity of the product | |
$info = $product->get_stock_quantity(); | |
break; | |
case 'stock_status': // Extracts the stock status of the product | |
$info = $product->get_stock_status(); | |
break; | |
case 'backorders': // Extracts the backorder status of the product | |
$info = $product->get_backorders(); | |
break; | |
case 'sold_individually': // Extracts if the product is sold individually | |
$info = $product->get_sold_individually(); | |
break; | |
case 'purchase_note': // Extracts the purchase note of the product | |
$info = $product->get_purchase_note(); | |
break; | |
case 'shipping_class_id': // Extracts the shipping class ID of the product | |
$info = $product->get_shipping_class_id(); | |
break; | |
case 'weight': // Extracts the weight of the product | |
$info = $product->get_weight() . ' ' . get_option('woocommerce_weight_unit'); | |
break; | |
case 'length': // Extracts the length of the product | |
$info = $product->get_length() . ' ' . get_option('woocommerce_dimension_unit'); | |
break; | |
case 'width': // Extracts the width of the product | |
$info = $product->get_width() . ' ' . get_option('woocommerce_dimension_unit'); | |
break; | |
case 'height': // Extracts the height of the product | |
$info = $product->get_height() . ' ' . get_option('woocommerce_dimension_unit'); | |
break; | |
case 'dimensions': // Extracts the dimensions (length, width, and height) of the product | |
$info = $product->get_dimensions(); | |
break; | |
case 'upsell_ids': // Extracts the upsell product IDs of the product | |
$upsell_ids = implode(',', $product->get_upsell_ids()); | |
$info = $upsell_ids !== '' ? $upsell_ids : 'none'; | |
break; | |
case 'cross_sell_ids': // Extracts the cross-sell product IDs of the product | |
$cross_sell_ids = implode(',', $product->get_cross_sell_ids()); | |
$info = $cross_sell_ids !== '' ? $cross_sell_ids : 'none'; | |
break; | |
case 'parent_id': // Extracts the parent product ID of the product | |
$info = $product->get_parent_id(); | |
break; | |
case 'children': // Extracts the child product IDs (variations) of the product | |
$children_ids = implode(', ', $product->get_children()); | |
$info = $children_ids !== '' ? $children_ids : 'none'; | |
break; | |
case 'children_names': // Extracts the names of the attributes of child products (variations) of the product | |
$children_names = array(); | |
$children_ids = $product->get_children(); | |
foreach ($children_ids as $child_id) { | |
$variation = wc_get_product($child_id); | |
$attributes = $variation->get_attributes(); | |
foreach ($attributes as $attribute_name => $attribute) { | |
$term_name = get_term_by('slug', $attribute, $attribute_name)->name; | |
$children_names[] = $term_name; | |
} | |
} | |
$info = implode(', ', $children_names); | |
$info = $info !== '' ? $info : 'none'; | |
break; | |
case 'attributes': // Extracts the attributes of the product | |
$attributes = $product->get_attributes(); | |
$attribute_names = array(); | |
foreach ($attributes as $attribute) { | |
$attribute_names[] = $attribute['name']; | |
} | |
$info = implode(', ', $attribute_names); | |
$info = $info !== '' ? $info : 'none'; | |
break; | |
case 'default_attributes': // Extracts the default attributes of the product | |
$default_attributes = $product->get_default_attributes(); | |
$default_values = array(); | |
foreach ($default_attributes as $attribute_name => $attribute_value) { | |
$term_name = get_term_by('slug', $attribute_value, $attribute_name)->name; | |
$default_values[] = $term_name; | |
} | |
$info = implode(', ', $default_values); | |
$info = $info !== '' ? $info : 'none'; | |
break; | |
case 'attribute': // Extracts a specific attribute value of the product | |
$info = $product->get_attribute('attributeid'); | |
break; | |
case 'category_ids': // Extracts the category IDs of the product | |
$category_ids = $product->get_category_ids(); | |
$info = implode(',', $category_ids); | |
break; | |
case 'tag_ids': // Extracts the tag IDs of the product | |
$tag_ids = $product->get_tag_ids(); | |
$info = implode(',', $tag_ids); | |
break; | |
case 'downloads': // Extracts the downloads of the product | |
$downloads = $product->get_downloads(); | |
$info = json_encode($downloads); | |
break; | |
case 'download_expiry': // Extracts the download expiry of the product | |
$info = $product->get_download_expiry(); | |
break; | |
case 'downloadable': // Extracts if the product is downloadable | |
$downloadable = $product->get_downloadable(); | |
$info = $downloadable ? 'yes' : 'no'; | |
break; | |
case 'download_limit': // Extracts the download limit of the product | |
$download_limit = $product->get_download_limit(); | |
$info = ($download_limit !== -1 && !empty($download_limit)) ? $download_limit : 'unlimited'; | |
break; | |
case 'image_id': // Extracts the ID of the main product image | |
$info = $product->get_image_id(); | |
break; | |
case 'image': // Extracts the main product image HTML | |
$info = $product->get_image(); | |
break; | |
case 'gallery_image_ids': // Extracts the IDs of the product gallery images | |
$gallery_ids = $product->get_gallery_image_ids(); | |
$info = implode(',', $gallery_ids); | |
break; | |
case 'gallery_first_image_id': // Extracts the ID of the first gallery image | |
$gallery_ids = $product->get_gallery_image_ids(); | |
if (!empty($gallery_ids)) { | |
$info = $gallery_ids[0]; | |
} else { | |
$info = null; | |
} | |
break; | |
case 'reviews_allowed': // Extracts if reviews are allowed for the product | |
$reviews_allowed = $product->get_reviews_allowed(); | |
$info = $reviews_allowed ? 'yes' : 'no'; | |
break; | |
case 'rating_counts_1': // Extracts the count of reviews with 1-star rating | |
$info = $product->get_rating_count(1); | |
break; | |
case 'rating_counts_2': // Extracts the count of reviews with 2-star rating | |
$info = $product->get_rating_count(2); | |
break; | |
case 'rating_counts_3': // Extracts the count of reviews with 3-star rating | |
$info = $product->get_rating_count(3); | |
break; | |
case 'rating_counts_4': // Extracts the count of reviews with 4-star rating | |
$info = $product->get_rating_count(4); | |
break; | |
case 'rating_counts_5': // Extracts the count of reviews with 5-star rating | |
$info = $product->get_rating_count(5); | |
break; | |
case 'average_rating': | |
$info = $product->get_average_rating(); | |
$info = number_format($info, 1); | |
break; | |
case 'rating_percentage_1': // Calculates the percentage of 1-star ratings | |
$total_ratings = $product->get_review_count(); | |
$rating_count_1 = $product->get_rating_count(1); | |
$percentage_1 = $total_ratings > 0 ? round(($rating_count_1 / $total_ratings) * 100) : 0; | |
$info = $percentage_1; | |
break; | |
case 'rating_percentage_2': // Calculates the percentage of 2-star ratings | |
$total_ratings = $product->get_review_count(); | |
$rating_count_2 = $product->get_rating_count(2); | |
$percentage_2 = $total_ratings > 0 ? round(($rating_count_2 / $total_ratings) * 100) : 0; | |
$info = $percentage_2; | |
break; | |
case 'rating_percentage_3': // Calculates the percentage of 3-star ratings | |
$total_ratings = $product->get_review_count(); | |
$rating_count_3 = $product->get_rating_count(3); | |
$percentage_3 = $total_ratings > 0 ? round(($rating_count_3 / $total_ratings) * 100) : 0; | |
$info = $percentage_3; | |
break; | |
case 'rating_percentage_4': // Calculates the percentage of 4-star ratings | |
$total_ratings = $product->get_review_count(); | |
$rating_count_4 = $product->get_rating_count(4); | |
$percentage_4 = $total_ratings > 0 ? round(($rating_count_4 / $total_ratings) * 100) : 0; | |
$info = $percentage_4; | |
break; | |
case 'rating_percentage_5': // Calculates the percentage of 5-star ratings | |
$total_ratings = $product->get_review_count(); | |
$rating_count_5 = $product->get_rating_count(5); | |
$percentage_5 = $total_ratings > 0 ? round(($rating_count_5 / $total_ratings) * 100) : 0; | |
$info = $percentage_5; | |
break; | |
case 'review_count': // Extracts the total count of reviews for the product | |
$info = $product->get_review_count(); | |
break; | |
case 'find_cart': // Checks if the product is in the cart | |
global $woocommerce; | |
$product_id = $product->get_id(); | |
$product_in_cart = false; | |
foreach ($woocommerce->cart->get_cart() as $cart_item) { | |
$cart_product_id = $cart_item['product_id']; | |
if ($product_id == $cart_product_id) { | |
$product_in_cart = true; | |
break; | |
} | |
} | |
$info = $product_in_cart ? 'yes' : 'no'; | |
break; | |
default: | |
$info = null; | |
break; | |
} | |
return $info; | |
} | |
function css_dyn(...$groups) { | |
$result = ''; | |
foreach ($groups as $group) { | |
$group_args = explode('|', $group); | |
$class = trim($group_args[0]); | |
$woofield = isset($group_args[1]) ? woo_field(trim($group_args[1])) : ''; | |
$unit = isset($group_args[2]) ? trim($group_args[2]) : ''; | |
if (!empty($woofield)) { | |
$result .= "$class: $woofield$unit; "; | |
} else { | |
$result .= "$class; "; | |
} | |
} | |
$result = rtrim($result); | |
return $result; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment