{ return WC()->customer->get_shipping_postcode(); } } return NULL; } /** * get orders list by condition * @param array $conditions * @return int[]|WP_Post[] */ static function getOrdersByConditions($conditions = array()) { $default_conditions = array( 'numberposts' => -1, 'post_type' => self::getOrderPostType(), 'post_status' => array_keys(self::getOrderStatusList()), 'orderby' => 'ID', 'order' => 'DESC' ); if (is_object($conditions)) { $conditions = (array)$conditions; } elseif (!is_array($conditions)) { $conditions = array(); } $final_conditions = array_merge($default_conditions, $conditions); return get_posts($final_conditions); } /** * Get all order status lists * @param bool $key_only * @return array */ static function getOrderPostType($key_only = false) { if (function_exists('wc_get_order_types')) { if ($key_only) { return array_keys(wc_get_order_types()); } return wc_get_order_types(); } return NULL; } /** * get woocommerce order status * @return array */ static function getOrderStatusList() { if (function_exists('wc_get_order_statuses')) { return wc_get_order_statuses(); } return array(); } /** * get item ids of the particular order * @param $order * @return array */ static function getOrderItemsId($order) { $order_items = self::getOrderItems($order); $order_items_id = array(); if (!empty($order_items)) { foreach ($order_items as $item) { $order_items_id[] = self::getItemId($item); } } return array_filter($order_items_id); } /** * get quantities of the particular order * @param $order * @return array */ static function getOrderItemsQty($order) { $order_items = self::getOrderItems($order); $productIds = array(); if (!empty($order_items)) { foreach ($order_items as $item) { $product_id = $item->get_product_id(); $variant_id = $item->get_variation_id(); $quantity = $item->get_quantity(); if($variant_id){ $productId = $variant_id; } else { $productId = $product_id; } if(isset($productIds[$productId])){ $productIds[$productId] = $productIds[$productId]+$quantity; } else { $productIds[$productId] = $quantity; } } } return $productIds; } /** * get all items of the order * @param $order * @return array */ static function getOrderItems($order) { if (is_object($order) && method_exists($order, 'get_items')) { return $order->get_items(); } return array(); } /** * get the order currency * @param $order * @return array */ static function getOrderCurrency($order) { if (is_object($order) && method_exists($order, 'get_currency')) { return $order->get_currency(); } return NULL; } /** * Set order meta * @param $order * @param $key * @param $value * @return array */ static function setOrderMeta($order, $key, $value) { if (is_object($order) && method_exists($order, 'add_meta_data')) { $status = $order->add_meta_data($key, $value, true); if (self::customOrdersTableIsEnabled() && method_exists($order, 'save')) { $order->save(); } elseif (method_exists($order, 'save_meta_data')) { $order->save_meta_data(); } return $status; } return NULL; } /** * Set order item meta * @param $item * @param $key * @param $value * @return array */ static function setOrderItemMeta($item, $key, $value) { if (is_object($item) && method_exists($item, 'add_meta_data')) { return $item->add_meta_data($key, $value, true); } return NULL; } /** * Get order item data * @param $item * @return array */ static function getOrderItemData($item) { if (is_object($item) && method_exists($item, 'get_data')) { return $item->get_data(); } return NULL; } /** * Set order item meta * @param $item * @param $key * @return array */ static function getOrderItemMeta($item, $key) { if (is_object($item) && method_exists($item, 'get_meta')) { return $item->get_meta($key); } return NULL; } /** * get item id from the item object * @param $item * @return null */ static function getItemId($item) { if (is_object($item) && method_exists($item, 'get_product_id') && method_exists($item, 'get_variation_id')) { if ($product_id = $item->get_variation_id()) { return $product_id; } else { return $item->get_product_id(); } } return NULL; } /** * get term slug from category id * @param $id * @return bool */ static function getCategorySlugByID($id) { if (function_exists('get_term_by')) { $term = get_term_by('id', $id, 'product_cat', 'ARRAY_A'); return isset($term['slug']) ? $term['slug'] : NULL; } return NULL; } /** * get term slug from tag id * @param $id * @return bool */ static function getTagSlugByID($id) { $slug = false; if (function_exists('get_term_by')) { $term = get_term_by('id', $id, 'product_tag', 'ARRAY_A'); $slug = $term['slug']; } return $slug; } /** * get custom term slug from custom tag id * @param $id * @param $term_name * @return boo */ static function getTermSlugByID($id, $term_name) { $slug = false; if (function_exists('get_term_by')) { $term = get_term_by('id', $id, $term_name, 'ARRAY_A'); $slug = $term['slug']; } return $slug; } /** * get product id by using sku * @param $sku * @return bool|int */ static function getProductsBySku($sku) { if (empty($sku)) { return false; } if (function_exists('wc_get_product_id_by_sku')) { $id = wc_get_product_id_by_sku($sku); if (!empty($id)) { $product_id = Woocommerce::getProductParentId($id); if (empty($product_id)) { return $id; } else { return $product_id; } } } return false; } /** * get on sale products ids * @return array|bool */ static function getOnSaleProductsIds() { if (function_exists('wc_get_product_ids_on_sale')) { $on_sale_product_ids = wc_get_product_ids_on_sale(); return apply_filters('advanced_woo_discount_rules_get_on_sale_product_ids', $on_sale_product_ids); } return false; } /** * get all available attributes details * @return array */ static function getAllAvailableAttributeDetails() { global $wc_product_attributes; $attributes = array(); $available_attr = array(); if (function_exists('get_terms') && isset($wc_product_attributes) && is_array($wc_product_attributes) && !empty($wc_product_attributes)) { foreach ($wc_product_attributes as $attr_tax => $attr_value) { $terms = get_terms(array( 'taxonomy' => $attr_tax, 'hide_empty' => false, )); $attributes[$attr_tax] = $terms; if (is_array($terms) && !empty($terms)) { foreach ($terms as $term_detail) { if (is_object($term_detail)) { $term_id = isset($term_detail->term_id) ? $term_detail->term_id : ''; if (!empty($term_id)) { $available_attr[$attr_tax]['id'][] = $term_id; $available_attr[$attr_tax]['slug'][$term_id] = $term_detail->slug; } } } } } } return array('terms' => $attributes, 'attr' => $available_attr); } /** * get current cart quantities * @return int|mixed */ static function getCartTotalQuantities() { $cart_items = self::getCart(); $quantity = 0; if (!empty($cart_items)) { foreach ($cart_items as $cart_item) { $quantity += $cart_item['quantity']; } } return apply_filters('advanced_woo_discount_rules_get_cart_total_quantities', $quantity, $cart_items); } /** * define product loop has started * @param bool $echo * @return string|null */ static function productLoopStart($echo = true) { if (function_exists('woocommerce_product_loop_start')) { return woocommerce_product_loop_start($echo); } return NULL; } /** * Set product loop as end * @param bool $echo * @return string|null */ static function productLoopEnd($echo = true) { if (function_exists('woocommerce_product_loop_end')) { return woocommerce_product_loop_end($echo); } return NULL; } /** * set product loop properties * @param $option * @param $value * @return void|null */ static function setLoopProperties($option, $value) { if (function_exists('wc_set_loop_prop')) { return wc_set_loop_prop($option, $value); } return NULL; } /** * Validate coupon * * @param string $coupon_name * @return boolean * */ static function checkCouponAlreadyExistsInWooCommerce($coupon_name) { $coupon_args = array( 'name' => $coupon_name, 'post_type' => 'shop_coupon' ); $posts = get_posts($coupon_args); if (!empty($posts) && count($posts) > 0) { return true; } return false; } /** * Validate coupon * * @param string $coupon_name * @return array * */ static function validateDynamicCoupon($coupon_name) { $result['status'] = false; if (!empty($coupon_name)) { $coupon_exists = self::checkCouponAlreadyExistsInWooCommerce($coupon_name); if ($coupon_exists) { $result['status'] = false; $result['message'] = esc_html__('Coupon already exists in WooCommerce. Please select another name', 'woo-discount-rules'); } else { $result['status'] = true; } } $result['coupon'] = $coupon_name; return $result; } /** * get current product price html * @param $product * @return bool */ static function getPriceHtml($product){ $html = false; if(!empty($product)) if (is_object($product) && method_exists($product, 'get_price_html')) { $html = $product->get_price_html(); } return apply_filters('advanced_woo_discount_rules_get_price_html', $html, $product); } /** * get parent product of current product * @param $product * @return bool|false|WC_Product|null */ static function getParentProduct($product){ if (self::productTypeIs($product, 'variation')) { $parent_id = self::getProductParentId($product); $product = self::getProduct($parent_id); } return $product; } public static function getCheckOutPostData(){ if(self::$checkout_post === null){ $input = new Input(); $postData = $input->post('post_data', null, 'raw'); $postDataArray = array(); if($postData != ''){ parse_str($postData, $postDataArray); } self::$checkout_post = $postDataArray; } return self::$checkout_post; } /** * Get billing email from post data * * @return string * */ public static function getBillingEmailFromPost(){ $user_email = ''; $postData = self::getCheckOutPostData(); if(isset($postData['billing_email']) && !empty($postData['billing_email'])){ $user_email = $postData['billing_email']; } if(empty($user_email)){ if(function_exists('WC')){ $session = WC()->session; if(!empty($session)){ if(is_object($session) && method_exists($session, 'get')){ $customer = $session->get('customer'); if(isset($customer['email']) && !empty($customer['email'])){ $user_email = $customer['email']; } } } } } return $user_email; } /** * remove Html Tags * * @param $html * @return string */ public static function removeHtmlTags($html){ if (function_exists('wp_strip_all_tags')) { $html = wp_strip_all_tags($html); }else{ $html = strip_tags($html);//phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags } return $html; } /** * check is rtl function * @return bool */ public static function isRTLEnable(){ if(function_exists('is_rtl') && is_rtl()){ return true; } return false; } /** * Get the min or max variation regular price. * * @param object $product * @param string $min_or_max Min or max price. * @param boolean $for_display If true, prices will be adapted for display based on the `woocommerce_tax_display_shop` setting (including or excluding taxes). * @return string */ public static function get_variation_regular_price($product, $min_or_max = 'min', $for_display = false){ if(!empty($product)) if(is_object($product) && method_exists($product, 'get_variation_regular_price')){ return $product->get_variation_regular_price($min_or_max, $for_display); } return 0; } /** * Load discount table if any one variant has discount * */ public static function displayTableIfAnyOneVariantHasDiscount(){ return apply_filters('advanced_woo_discount_rules_display_bulk_table_if_any_one_variant_has_discount', false); } /** * get available product variations * @param $product * @return array */ public static function availableProductVariations($product){ $product_id = self::getProductId($product); if(isset(self::$product_variations[$product_id])){ return self::$product_variations[$product_id]; } $available_variations = array(); $is_variable_product = self::productTypeIs($product, 'variable'); if(!empty($product)) if ($is_variable_product && is_object($product) && method_exists($product, 'get_available_variations')){ $available_variations = $product->get_available_variations(); } self::$product_variations[$product_id] = $available_variations; return $available_variations; } /** * WC format price * */ public static function wc_format_decimal($price, $dp = false, $trim_zeros = false ){ if (function_exists('wc_format_decimal')) { $price = wc_format_decimal($price, $dp, $trim_zeros); } return $price; } /** * get the product visibility * @param $product - woocommerce product object * @return bool */ static function variationIsVisible($product) { if(!empty($product)){ if (is_object($product) && method_exists($product, 'variation_is_visible')) { return $product->variation_is_visible(); } } return false; } /** * @param array|WC_Product_Variation $variation Variation object. * @param bool $flat Should this be a flat list or HTML list? (default: false). * @param bool $include_names include attribute names/labels in the list. * @param bool $skip_attributes_in_name Do not list attributes already part of the variation name. * @return string */ static function wc_get_formatted_variation( $variation, $flat = false, $include_names = true, $skip_attributes_in_name = false ) { if (function_exists('wc_get_formatted_variation')) { return wc_get_formatted_variation($variation, $flat, $include_names, $skip_attributes_in_name); } else { return ''; } } /** * Get title of product * @param $product - woocommerce product object * @return string */ static function getTitleOfProduct($product){ if (is_numeric($product)) $product = self::getProduct($product); $product_id = self::getProductId($product); if(self::productTypeIs($product, 'variation')){ $attributes = (array) self::getProductAttributes($product); $modify_count = apply_filters('advanced_woo_discount_rules_variation_title_modify_count', 2); if (count($attributes) > $modify_count) { $variation_parent_id = self::getProductParentId($product); $variation_parent_title = get_the_title($variation_parent_id); $variation_separator = apply_filters('woocommerce_product_variation_title_attributes_separator', ' - ', $product); $variation_attributes = self::wc_get_formatted_variation($product, true, false); $product_title = $variation_parent_title . $variation_separator . $variation_attributes; } else { $product_title = get_the_title($product_id); } } else { $product_title = get_the_title($product_id); } return $product_title; } /** * Check cart needs shipping * * @return bool */ static function isCartNeedsShipping() { if (function_exists('WC')) { if(isset(WC()->cart) && WC()->cart != null){ if (is_object(WC()->cart) && method_exists(WC()->cart, 'needs_shipping')) { return WC()->cart->needs_shipping(); } } } return false; } /** * Check the product is purchasable or not * @param $product * @return bool */ static function checkProductIsPurchasable($product) { if(!apply_filters('wlr_is_purchasable_need_to_check',true)){ return true; } if(is_object($product) && method_exists($product, 'is_purchasable')) { return $product->is_purchasable(); } return false; } /** * Check custom order table feature (HPOS) is enabled or not * * @since 2.6.0 * * @return bool */ static function customOrdersTableIsEnabled() { if (class_exists('Automattic\WooCommerce\Utilities\OrderUtil') && method_exists('Automattic\WooCommerce\Utilities\OrderUtil', 'custom_orders_table_usage_is_enabled')) { return \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled(); } return false; } }