add_action("wp_footer", function () {
if (defined("OXYGEN_IFRAME")) { ?>
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_original':
if ($product->is_type('variable')) {
$prices = $product->get_variation_prices();
if (empty($prices['price'])) {
$info = null;
break;
}
$min_price = min($prices['price']);
$max_price = max($prices['price']);
$formatted_min_price = number_format($min_price, get_option('woocommerce_price_num_decimals'), get_option('woocommerce_price_decimal_sep'), get_option('woocommerce_price_thousand_sep'));
$formatted_max_price = number_format($max_price, get_option('woocommerce_price_num_decimals'), get_option('woocommerce_price_decimal_sep'), get_option('woocommerce_price_thousand_sep'));
$currency_symbol = get_woocommerce_currency_symbol();
$currency_pos = get_option('woocommerce_currency_pos');
if ($currency_pos === 'left') {
$info = $currency_symbol . $formatted_min_price . ' - ' . $currency_symbol . $formatted_max_price;
} elseif ($currency_pos === 'right') {
$info = $formatted_min_price . $currency_symbol . ' - ' . $formatted_max_price . $currency_symbol;
} elseif ($currency_pos === 'left_space') {
$info = $currency_symbol . ' ' . $formatted_min_price . ' - ' . $currency_symbol . ' ' . $formatted_max_price;
} elseif ($currency_pos === 'right_space') {
$info = $formatted_min_price . ' ' . $currency_symbol . ' - ' . $formatted_max_price . ' ' . $currency_symbol;
} else {
$info = $formatted_min_price . ' - ' . $formatted_max_price;
}
} elseif (is_numeric($product->get_price())) {
$price = $product->get_price();
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$tax_display_mode = get_option('woocommerce_tax_display_shop');
if ($tax_display_mode === 'incl') {
$price_with_tax = wc_get_price_including_tax($product);
$formatted_price = number_format($price_with_tax, get_option('woocommerce_price_num_decimals'), get_option('woocommerce_price_decimal_sep'), get_option('woocommerce_price_thousand_sep'));
} else {
$price_excl_tax = wc_get_price_excluding_tax($product);
$formatted_price = number_format($price_excl_tax, get_option('woocommerce_price_num_decimals'), get_option('woocommerce_price_decimal_sep'), get_option('woocommerce_price_thousand_sep'));
}
$currency_symbol = get_woocommerce_currency_symbol();
$currency_pos = get_option('woocommerce_currency_pos');
if ($product->is_on_sale() && $sale_price) {
$regular_formatted = number_format($regular_price, get_option('woocommerce_price_num_decimals'), get_option('woocommerce_price_decimal_sep'), get_option('woocommerce_price_thousand_sep'));
$sale_formatted = number_format($sale_price, get_option('woocommerce_price_num_decimals'), get_option('woocommerce_price_decimal_sep'), get_option('woocommerce_price_thousand_sep'));
if ($currency_pos === 'left') {
$info = '' . $currency_symbol . $regular_formatted . ' ' . $currency_symbol . $sale_formatted . '';
} elseif ($currency_pos === 'right') {
$info = '' . $regular_formatted . $currency_symbol . ' ' . $sale_formatted . $currency_symbol . '';
} elseif ($currency_pos === 'left_space') {
$info = '' . $currency_symbol . ' ' . $regular_formatted . ' ' . $currency_symbol . ' ' . $sale_formatted . '';
} elseif ($currency_pos === 'right_space') {
$info = '' . $regular_formatted . ' ' . $currency_symbol . ' ' . $sale_formatted . ' ' . $currency_symbol . '';
} else {
$info = '' . $regular_formatted . ' ' . $sale_formatted . '';
}
} else {
if ($currency_pos === 'left') {
$info = $currency_symbol . $formatted_price;
} elseif ($currency_pos === 'right') {
$info = $formatted_price . $currency_symbol;
} elseif ($currency_pos === 'left_space') {
$info = $currency_symbol . ' ' . $formatted_price;
} elseif ($currency_pos === 'right_space') {
$info = $formatted_price . ' ' . $currency_symbol;
} else {
$info = $formatted_price;
}
}
} else {
$info = null;
}
break;
case "price": // Extract the price
$price = $product->get_price();
if (is_numeric($price)) {
$tax_display_mode = get_option(
"woocommerce_tax_display_shop"
);
if ($tax_display_mode === "incl") {
$price_with_tax = wc_get_price_including_tax($product);
$formatted_price = number_format(
$price_with_tax,
get_option("woocommerce_price_num_decimals"),
get_option("woocommerce_price_decimal_sep"),
get_option("woocommerce_price_thousand_sep")
);
} else {
$price_excl_tax = wc_get_price_excluding_tax($product);
$formatted_price = number_format(
$price_excl_tax,
get_option("woocommerce_price_num_decimals"),
get_option("woocommerce_price_decimal_sep"),
get_option("woocommerce_price_thousand_sep")
);
}
$currency_symbol = get_woocommerce_currency_symbol();
$currency_pos = get_option("woocommerce_currency_pos");
if ($currency_pos === "left") {
$info = $currency_symbol . $formatted_price;
} elseif ($currency_pos === "right") {
$info = $formatted_price . $currency_symbol;
} elseif ($currency_pos === "left_space") {
$info = $currency_symbol . " " . $formatted_price;
} elseif ($currency_pos === "right_space") {
$info = $formatted_price . " " . $currency_symbol;
} else {
$info = $formatted_price;
}
} else {
$info = null;
}
break;
case "regular_price": // Extract the regular price
$regular_price = $product->get_regular_price();
if (is_numeric($regular_price)) {
$tax_display_mode = get_option(
"woocommerce_tax_display_shop"
);
if ($tax_display_mode === "incl") {
$regular_price_with_tax = wc_get_price_including_tax(
$product,
["price" => $regular_price]
);
$formatted_price = number_format(
$regular_price_with_tax,
get_option("woocommerce_price_num_decimals"),
get_option("woocommerce_price_decimal_sep"),
get_option("woocommerce_price_thousand_sep")
);
} else {
$regular_price_excl_tax = wc_get_price_excluding_tax(
$product,
["price" => $regular_price]
);
$formatted_price = number_format(
$regular_price_excl_tax,
get_option("woocommerce_price_num_decimals"),
get_option("woocommerce_price_decimal_sep"),
get_option("woocommerce_price_thousand_sep")
);
}
$currency_symbol = get_woocommerce_currency_symbol();
$currency_pos = get_option("woocommerce_currency_pos");
if ($currency_pos === "left") {
$info = $currency_symbol . $formatted_price;
} elseif ($currency_pos === "right") {
$info = $formatted_price . $currency_symbol;
} elseif ($currency_pos === "left_space") {
$info = $currency_symbol . " " . $formatted_price;
} elseif ($currency_pos === "right_space") {
$info = $formatted_price . " " . $currency_symbol;
} else {
$info = $formatted_price;
}
} else {
$info = null;
}
break;
case "sale_price": // Extract the sale price
$sale_price = $product->get_sale_price();
if (is_numeric($sale_price)) {
$tax_display_mode = get_option(
"woocommerce_tax_display_shop"
);
if ($tax_display_mode === "incl") {
$sale_price_with_tax = wc_get_price_including_tax(
$product,
["price" => $sale_price]
);
$formatted_price = number_format(
$sale_price_with_tax,
get_option("woocommerce_price_num_decimals"),
get_option("woocommerce_price_decimal_sep"),
get_option("woocommerce_price_thousand_sep")
);
} else {
$sale_price_excl_tax = wc_get_price_excluding_tax(
$product,
["price" => $sale_price]
);
$formatted_price = number_format(
$sale_price_excl_tax,
get_option("woocommerce_price_num_decimals"),
get_option("woocommerce_price_decimal_sep"),
get_option("woocommerce_price_thousand_sep")
);
}
$currency_symbol = get_woocommerce_currency_symbol();
$currency_pos = get_option("woocommerce_currency_pos");
if ($currency_pos === "left") {
$info = $currency_symbol . $formatted_price;
} elseif ($currency_pos === "right") {
$info = $formatted_price . $currency_symbol;
} elseif ($currency_pos === "left_space") {
$info = $currency_symbol . " " . $formatted_price;
} elseif ($currency_pos === "right_space") {
$info = $formatted_price . " " . $currency_symbol;
} else {
$info = $formatted_price;
}
} else {
$info = null;
}
break;
case "tax_suffix": // Extract the tax suffix
$suffix = get_option("woocommerce_price_display_suffix");
if ($suffix) {
$price_including_tax = wc_get_price_including_tax($product);
$price_excluding_tax = wc_get_price_excluding_tax($product);
$suffix = str_replace(
"{price_including_tax}",
get_woocommerce_currency_symbol() .
number_format($price_including_tax, 2),
$suffix
);
$suffix = str_replace(
"{price_excluding_tax}",
get_woocommerce_currency_symbol() .
number_format($price_excluding_tax, 2),
$suffix
);
}
$info = $suffix ? $suffix : "";
break;
case "tax_included": // Check if tax is included
$tax_included =
get_option("woocommerce_tax_display_shop") === "incl"
? "yes"
: "no";
$info = $tax_included;
break;
case "price_tax_included": // Extract the price including tax
$price = $product->get_price();
if (is_numeric($price)) {
$price_including_tax = wc_get_price_including_tax(
$product,
["price" => $price]
);
$formatted_price = number_format(
$price_including_tax,
get_option("woocommerce_price_num_decimals"),
get_option("woocommerce_price_decimal_sep"),
get_option("woocommerce_price_thousand_sep")
);
$currency_symbol = get_woocommerce_currency_symbol();
$currency_pos = get_option("woocommerce_currency_pos");
if ($currency_pos === "left") {
$info = $currency_symbol . $formatted_price;
} elseif ($currency_pos === "right") {
$info = $formatted_price . $currency_symbol;
} elseif ($currency_pos === "left_space") {
$info = $currency_symbol . " " . $formatted_price;
} elseif ($currency_pos === "right_space") {
$info = $formatted_price . " " . $currency_symbol;
} else {
$info = $formatted_price;
}
} else {
$info = null;
}
break;
case "price_tax_excluding": // Extract the price excluding tax
$price = $product->get_price();
if (is_numeric($price)) {
$price_excluding_tax = wc_get_price_excluding_tax(
$product,
["price" => $price]
);
$formatted_price = number_format(
$price_excluding_tax,
get_option("woocommerce_price_num_decimals"),
get_option("woocommerce_price_decimal_sep"),
get_option("woocommerce_price_thousand_sep")
);
$currency_symbol = get_woocommerce_currency_symbol();
$currency_pos = get_option("woocommerce_currency_pos");
if ($currency_pos === "left") {
$info = $currency_symbol . $formatted_price;
} elseif ($currency_pos === "right") {
$info = $formatted_price . $currency_symbol;
} elseif ($currency_pos === "left_space") {
$info = $currency_symbol . " " . $formatted_price;
} elseif ($currency_pos === "right_space") {
$info = $formatted_price . " " . $currency_symbol;
} else {
$info = $formatted_price;
}
} else {
$info = null;
}
break;
case "find_variable_price": // Check if the current product is a variable product
$prices = [];
if ($product->is_type("variable")) {
$variations = $product->get_available_variations();
foreach ($variations as $variation) {
$prices[] = $variation["display_price"];
}
} elseif ($product->is_type("grouped")) {
$children = $product->get_children();
foreach ($children as $child_id) {
$child_product = wc_get_product($child_id);
if ($child_product) {
$prices[] = $child_product->get_price();
}
}
}
$info = count(array_unique($prices)) > 1 ? "yes" : "no";
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": //Extract sale start date
$date_on_sale_from = $product->get_date_on_sale_from();
if (!empty($date_on_sale_from)) {
$formatted_date = date_i18n(
$date_format,
strtotime($date_on_sale_from)
);
$info = $formatted_date;
} else {
$info = null;
}
break;
case "date_on_sale_to": //Extract sale expiration date
$info = $product->get_date_on_sale_to();
if (!empty($info)) {
$formatted_date = date_i18n($date_format, strtotime($info));
$info = $formatted_date;
} else {
$info = null;
}
break;
case "date_on_sale_to_year": // Extraer año de expiración de venta
$date_on_sale_to = null;
if ($product->is_type("variable")) {
$children_ids = $product->get_children();
if (!empty($children_ids)) {
$first_child_id = reset($children_ids);
$first_child = wc_get_product($first_child_id);
$date_on_sale_to = $first_child->get_date_on_sale_to();
}
} else {
$date_on_sale_to = $product->get_date_on_sale_to();
}
if (!empty($date_on_sale_to)) {
$info = date_i18n("Y", strtotime($date_on_sale_to));
} else {
$info = null;
}
break;
case "date_on_sale_to_month": // Extraer mes de expiración de venta
$date_on_sale_to = null;
if ($product->is_type("variable")) {
$children_ids = $product->get_children();
if (!empty($children_ids)) {
$first_child_id = reset($children_ids);
$first_child = wc_get_product($first_child_id);
$date_on_sale_to = $first_child->get_date_on_sale_to();
}
} else {
$date_on_sale_to = $product->get_date_on_sale_to();
}
if (!empty($date_on_sale_to)) {
$info = date_i18n("m", strtotime($date_on_sale_to));
} else {
$info = null;
}
break;
case "date_on_sale_to_day": // Extraer día de expiración de venta
$date_on_sale_to = null;
if ($product->is_type("variable")) {
$children_ids = $product->get_children();
if (!empty($children_ids)) {
$first_child_id = reset($children_ids);
$first_child = wc_get_product($first_child_id);
$date_on_sale_to = $first_child->get_date_on_sale_to();
}
} else {
$date_on_sale_to = $product->get_date_on_sale_to();
}
if (!empty($date_on_sale_to)) {
$info = date_i18n("d", strtotime($date_on_sale_to));
} else {
$info = null;
}
break;
case "discount_percentage": // Calculate the discount percentage
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
if (
is_numeric($regular_price) &&
is_numeric($sale_price) &&
$regular_price > 0
) {
$discount_percentage = round(
(($regular_price - $sale_price) / $regular_price) * 100
);
$info = "-" . $discount_percentage . "%";
} else {
$info = null;
}
break;
case "discount_amount": // Calculate the discount amount in money
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
if (
is_numeric($regular_price) &&
is_numeric($sale_price) &&
$regular_price > 0
) {
$tax_display_mode = get_option(
"woocommerce_tax_display_shop"
);
if ($tax_display_mode === "incl") {
$regular_price_including_tax = wc_get_price_including_tax(
$product,
["price" => $regular_price]
);
$sale_price_including_tax = wc_get_price_including_tax(
$product,
["price" => $sale_price]
);
$discount_amount =
$regular_price_including_tax -
$sale_price_including_tax;
} else {
$regular_price_excluding_tax = wc_get_price_excluding_tax(
$product,
["price" => $regular_price]
);
$sale_price_excluding_tax = wc_get_price_excluding_tax(
$product,
["price" => $sale_price]
);
$discount_amount =
$regular_price_excluding_tax -
$sale_price_excluding_tax;
}
$formatted_discount = number_format(
$discount_amount,
get_option("woocommerce_price_num_decimals"),
get_option("woocommerce_price_decimal_sep"),
get_option("woocommerce_price_thousand_sep")
);
$currency_symbol = get_woocommerce_currency_symbol();
$currency_pos = get_option("woocommerce_currency_pos");
if ($currency_pos === "left") {
$info = $currency_symbol . $formatted_discount;
} elseif ($currency_pos === "right") {
$info = $formatted_discount . $currency_symbol;
} elseif ($currency_pos === "left_space") {
$info = $currency_symbol . " " . $formatted_discount;
} elseif ($currency_pos === "right_space") {
$info = $formatted_discount . " " . $currency_symbol;
} else {
$info = $formatted_discount;
}
} else {
$info = null;
}
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":
if ($product->is_type('variable')) {
$variations = $product->get_children();
$total_stock = 0;
foreach ($variations as $variation_id) {
$variation = wc_get_product($variation_id);
$stock = $variation->get_stock_quantity();
if ($stock !== null) {
$total_stock += $stock;
}
}
$info = $total_stock;
} else {
$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 attribute names of the child products (variations) of the product
$children_names = [];
$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 = $attribute;
if (!empty($term_name)) {
$children_names[] = $term_name;
} else {
$children_names[] = "No Name Found";
}
}
}
$info = implode(", ", $children_names);
$info = $info !== "" ? $info : "none";
break;
case "attributes": // Extracts the attributes of the product
$attributes = $product->get_attributes();
$attribute_names = [];
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 = [];
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()
? $product->get_image_id()
: get_option("woocommerce_placeholder_image", 0);
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 "has_ratings": // Checks if the product has ratings
$rating_count = $product->get_rating_count();
$info = $rating_count > 0 ? "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
$product_id = $product->get_id();
$product_in_cart = false;
$cart = WC()->cart;
if ($cart && !$cart->is_empty()) {
foreach ($cart->get_cart() as $cart_item) {
if (
$cart_item["product_id"] == $product_id ||
$cart_item["variation_id"] == $product_id
) {
$product_in_cart = true;
break;
}
}
}
$info = $product_in_cart ? "yes" : "no";
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 "cart_product_image_id": // Extracts the id of the main image or uses the default WooCommerce placeholder if none exists
if ($product->get_image_id()) {
$image_id = $product->get_image_id();
} elseif (
$product->is_type("variation") &&
$product->get_parent_id()
) {
$parent_product = wc_get_product($product->get_parent_id());
$image_id =
$parent_product && $parent_product->get_image_id()
? $parent_product->get_image_id()
: attachment_url_to_postid(
wc_placeholder_img_src()
);
} else {
$image_id = attachment_url_to_postid(
wc_placeholder_img_src()
);
}
$info = $image_id;
break;
case "cart_selected_attribute": // Extracts the variable product attribute name for example: Color: Blue
$attribute_values = [];
if ($product) {
if ($product->is_type("simple")) {
$info = "None";
break;
}
$attributes = $product->get_attributes();
if (!empty($attributes)) {
foreach (
$attributes
as $attribute_name => $attribute_data
) {
$label = wc_attribute_label(
$attribute_name,
$product
);
$value = $product->get_attribute($attribute_name);
if (!empty($value)) {
if (taxonomy_exists($attribute_name)) {
$term = get_term_by(
"slug",
$value,
$attribute_name
);
$formatted_value = $term
? $term->name
: $value;
} else {
$formatted_value = $value;
}
$attribute_values[] =
$label . ": " . $formatted_value;
}
}
}
}
$info = !empty($attribute_values)
? implode(", ", $attribute_values)
: "none";
break;
case "cart_product_price": // Extracts the price of the product, variable or simple
$price = $product->get_price();
if (is_numeric($price)) {
$tax_display_mode = get_option(
"woocommerce_tax_display_shop"
);
if ($tax_display_mode === "incl") {
$price_with_tax = wc_get_price_including_tax($product);
$formatted_price = number_format(
$price_with_tax,
get_option("woocommerce_price_num_decimals"),
get_option("woocommerce_price_decimal_sep"),
get_option("woocommerce_price_thousand_sep")
);
} else {
$price_excl_tax = wc_get_price_excluding_tax($product);
$formatted_price = number_format(
$price_excl_tax,
get_option("woocommerce_price_num_decimals"),
get_option("woocommerce_price_decimal_sep"),
get_option("woocommerce_price_thousand_sep")
);
}
$currency_symbol = get_woocommerce_currency_symbol();
$currency_pos = get_option("woocommerce_currency_pos");
if ($currency_pos === "left") {
$info = $currency_symbol . $formatted_price;
} elseif ($currency_pos === "right") {
$info = $formatted_price . $currency_symbol;
} elseif ($currency_pos === "left_space") {
$info = $currency_symbol . " " . $formatted_price;
} elseif ($currency_pos === "right_space") {
$info = $formatted_price . " " . $currency_symbol;
} else {
$info = $formatted_price;
}
} else {
$info = null;
}
break;
case "cart_quantity": // Extract the quantity of each product in the cart
$product_id = $product->get_id();
$cart = WC()->cart->get_cart();
$quantity = 0;
if ($product->is_type("variation")) {
foreach ($cart as $cart_item) {
if ($cart_item["variation_id"] == $product_id) {
$quantity = $cart_item["quantity"];
break;
}
}
} else {
foreach ($cart as $cart_item) {
if ($cart_item["product_id"] == $product_id) {
$quantity = $cart_item["quantity"];
break;
}
}
}
$info = $quantity;
break;
case "cart_product_title": // Extrae el título del producto (no de las variaciones)
if (!$product->is_type("variation")) {
$title = $product->get_name();
} else {
$parent_product = wc_get_product($product->get_parent_id());
$title = $parent_product ? $parent_product->get_name() : "";
}
$info = $title;
break;
case "cart_link": // Extract custom link from cart button
$product_id = $product->get_id();
$cart_link = "?add-to-cart=" . $product_id;
$info = $cart_link;
break;
case "cart_item_key": // Extracts the cart item key for the product in the cart
$product_id = $product->get_id();
$cart = WC()->cart;
$item_key = "";
if ($cart && !$cart->is_empty()) {
foreach (
$cart->get_cart()
as $cart_item_key => $cart_item
) {
if (
$cart_item["product_id"] == $product_id ||
$cart_item["variation_id"] == $product_id
) {
$item_key = $cart_item_key;
break;
}
}
}
$info = !empty($item_key) ? $item_key : "none";
break;
case "remove_cart_url": // Extracts the remove URL for the product in the cart
$product_id = $product->get_id();
$cart = WC()->cart;
$remove_url = "";
if ($cart && !$cart->is_empty()) {
foreach (
$cart->get_cart()
as $cart_item_key => $cart_item
) {
if (
$cart_item["product_id"] == $product_id ||
$cart_item["variation_id"] == $product_id
) {
$remove_url = wc_get_cart_remove_url(
$cart_item_key
);
break;
}
}
}
$info = !empty($remove_url) ? $remove_url : "none";
break;
// [Yes it depends on a product]
case strpos($property, "acf_") === 0:
if (class_exists("acf")) {
$field_name = str_replace("acf_", "", $property);
if (function_exists("get_sub_field")) {
$find_acf_value = get_sub_field($field_name);
$info = !empty($find_acf_value)
? $find_acf_value
: "null";
}
} else {
$info = "null";
}
break;
default:
$product_id = $product->get_id();
$find_field_value = get_post_meta($product_id, $property, true);
$info = !empty($find_field_value) ? $find_field_value : "null";
break;
}
return wp_kses_post($info);
}
// Cases that do not depend on a product
switch ($property) {
/*User*/
case "user_order_find":
$user = wp_get_current_user();
$info =
$user && $user->exists()
? wc_get_customer_order_count($user->ID)
: 0;
break;
case "user_email": // Extracts the current user's email
$user = wp_get_current_user();
$info =
$user && $user->exists()
? $user->user_email
: "No user logged in";
break;
case "user_address_billing": // Extracts the current user's full billing address
$user = wp_get_current_user();
if ($user && $user->exists()) {
$billing_first_name = get_user_meta(
$user->ID,
"billing_first_name",
true
);
$billing_last_name = get_user_meta(
$user->ID,
"billing_last_name",
true
);
$billing_address_1 = get_user_meta(
$user->ID,
"billing_address_1",
true
);
$billing_address_2 = get_user_meta(
$user->ID,
"billing_address_2",
true
);
$billing_city = get_user_meta($user->ID, "billing_city", true);
$billing_state = get_user_meta(
$user->ID,
"billing_state",
true
);
$billing_postcode = get_user_meta(
$user->ID,
"billing_postcode",
true
);
$billing_country = get_user_meta(
$user->ID,
"billing_country",
true
);
$billing_phone = get_user_meta(
$user->ID,
"billing_phone",
true
);
$billing_email = $user->user_email;
$state_full =
WC()->countries->get_states($billing_country)[
$billing_state
] ?? $billing_state;
$country_full =
WC()->countries->countries[$billing_country] ??
$billing_country;
$info = "{$billing_first_name} {$billing_last_name}
";
$info .= "{$billing_address_1}
";
$info .= $billing_address_2 ? "{$billing_address_2}
" : "";
$info .= "{$billing_city}
";
$info .= "{$state_full}
";
$info .= "{$billing_postcode}
";
$info .= "{$country_full}
";
$info .= "{$billing_phone}
";
$info .= "{$billing_email}
";
} else {
$info = "No user logged in";
}
break;
/*End User*/
case "cart_product_ids": //Extracts all product IDs from the cart
if (!class_exists("WooCommerce")) {
$info = "none";
break;
}
$cart = WC()->cart;
if (!$cart || $cart->is_empty()) {
$info = "none";
} else {
$product_ids = [];
foreach ($cart->get_cart() as $cart_item) {
$product_id = !empty($cart_item["variation_id"])
? $cart_item["variation_id"]
: $cart_item["product_id"];
if (!in_array($product_id, $product_ids)) {
$product_ids[] = $product_id;
}
}
$info = !empty($product_ids)
? implode(",", $product_ids)
: "none";
}
break;
case "cart_total_items": //Extracts the total number of items in the cart
$cart = WC()->cart;
$info = $cart ? $cart->get_cart_contents_count() : "0";
break;
case "cart_total_unformatted":
$cart = WC()->cart;
if ($cart && !$cart->is_empty()) {
$info = floatval($cart->get_cart_contents_total());
} else {
$info = 0;
}
break;
case "cart_total_before_shipping": // Extracts the total price of all items in the cart before shipping
$cart = WC()->cart;
if ($cart && !$cart->is_empty()) {
$subtotal_price = $cart->get_subtotal();
$formatted_subtotal_price = number_format(
floatval($subtotal_price),
get_option("woocommerce_price_num_decimals"),
get_option("woocommerce_price_decimal_sep"),
get_option("woocommerce_price_thousand_sep")
);
$currency_symbol = get_woocommerce_currency_symbol();
$currency_pos = get_option("woocommerce_currency_pos");
if ($currency_pos === "left") {
$info = $currency_symbol . $formatted_subtotal_price;
} elseif ($currency_pos === "right") {
$info = $formatted_subtotal_price . $currency_symbol;
} elseif ($currency_pos === "left_space") {
$info = $currency_symbol . " " . $formatted_subtotal_price;
} elseif ($currency_pos === "right_space") {
$info = $formatted_subtotal_price . " " . $currency_symbol;
} else {
$info = $formatted_subtotal_price;
}
} else {
$currency_symbol = get_woocommerce_currency_symbol();
$info = $currency_symbol . "0.00";
}
break;
case "cart_price_shipping": // Extracts the shipping cost of the cart
$cart = WC()->cart;
if ($cart && !$cart->is_empty()) {
$shipping_total = $cart->get_shipping_total();
$formatted_shipping_total = number_format(
floatval($shipping_total),
get_option("woocommerce_price_num_decimals"),
get_option("woocommerce_price_decimal_sep"),
get_option("woocommerce_price_thousand_sep")
);
$currency_symbol = get_woocommerce_currency_symbol();
$currency_pos = get_option("woocommerce_currency_pos");
if ($currency_pos === "left") {
$info = $currency_symbol . $formatted_shipping_total;
} elseif ($currency_pos === "right") {
$info = $formatted_shipping_total . $currency_symbol;
} elseif ($currency_pos === "left_space") {
$info = $currency_symbol . " " . $formatted_shipping_total;
} elseif ($currency_pos === "right_space") {
$info = $formatted_shipping_total . " " . $currency_symbol;
} else {
$info = $formatted_shipping_total;
}
} else {
$currency_symbol = get_woocommerce_currency_symbol();
$info = $currency_symbol . "0.00";
}
break;
case "cart_total_price": // Extracts the total price of all items in the cart
$cart = WC()->cart;
if ($cart && !$cart->is_empty()) {
$total_price = $cart->get_total("");
$formatted_total_price = number_format(
floatval($total_price),
get_option("woocommerce_price_num_decimals"),
get_option("woocommerce_price_decimal_sep"),
get_option("woocommerce_price_thousand_sep")
);
$currency_symbol = get_woocommerce_currency_symbol();
$currency_pos = get_option("woocommerce_currency_pos");
if ($currency_pos === "left") {
$info = $currency_symbol . $formatted_total_price;
} elseif ($currency_pos === "right") {
$info = $formatted_total_price . $currency_symbol;
} elseif ($currency_pos === "left_space") {
$info = $currency_symbol . " " . $formatted_total_price;
} elseif ($currency_pos === "right_space") {
$info = $formatted_total_price . " " . $currency_symbol;
} else {
$info = $formatted_total_price;
}
} else {
$currency_symbol = get_woocommerce_currency_symbol();
$info = $currency_symbol . "0.00";
}
break;
// [No it depends on a product]
default:
$info = "Invalid property";
break;
}
return wp_kses_post($info);
}
// woo_css
function woo_css(...$groups)
{
if (!class_exists("WooCommerce")) {
return "Woocommerce is not active";
}
$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 wp_kses_post($result);
}