Overview

Namespaces

  • None
  • VSP
    • Core
      • Abstracts
      • Interfaces
      • Traits
        • WC_Compatibility
    • Deprecation
    • Helper
    • Modules
      • Addons
      • Logger

Classes

  • VSP\Ajax
  • VSP\Ajaxer
  • VSP\Base
  • VSP\Cache
  • VSP\Core\Abstracts\Addon
  • VSP\Core\Abstracts\Log_Handler
  • VSP\Core\Abstracts\Plugin_Settings
  • VSP\Core\Instance_Handler
  • VSP\Deprecation\Actions
  • VSP\Deprecation\Filters
  • VSP\Error
  • VSP\Framework
  • VSP\Framework_Admin
  • VSP\Framework_Base
  • VSP\Framework_Modules
  • VSP\Helper
  • VSP\Helper\Price_Calculation
  • VSP\Modules\Addons
  • VSP\Modules\Addons\Admin
  • VSP\Modules\Addons\Core
  • VSP\Modules\Logger
  • VSP\Modules\Logger\File_Handler
  • VSP\Modules\Logger\Levels
  • VSP\Modules\Shortcode
  • VSP\Modules\System_Logs
  • VSP\Modules\System_Tools
  • VSP\Modules\WPOnion
  • VSP\Setup
  • VSP\WC_Compatibility
  • VSP_Framework_Loader

Interfaces

  • VSP\Core\Interfaces\Log_Handler
  • VSP\Core\Interfaces\Logger

Traits

  • VSP\Core\Traits\Array_Helper
  • VSP\Core\Traits\Framework
  • VSP\Core\Traits\String_Helper
  • VSP\Core\Traits\URL
  • VSP\Core\Traits\WC_Compatibility\Product
  • VSP\Core\Traits\WC_Compatibility\Version
  • VSP\Core\Traits\WC_Helper
  • VSP\Core\Traits\WP

Functions

  • vsp_add_wc_required_notice
  • vsp_ajax_action
  • vsp_current_screen
  • vsp_date_format
  • vsp_delete_cache
  • vsp_doing_it_wrong
  • vsp_force_load
  • vsp_force_load_vendors
  • vsp_get_cache
  • vsp_get_cache_defaults
  • vsp_get_file_paths
  • vsp_get_logger
  • vsp_get_time_in_seconds
  • vsp_is_admin
  • vsp_is_ajax
  • vsp_is_cron
  • vsp_is_error
  • vsp_is_frontend
  • vsp_is_heartbeat
  • vsp_is_json
  • vsp_is_request
  • vsp_is_screen
  • vsp_json_last_error
  • vsp_list_files
  • vsp_list_log_files
  • vsp_load_core_assets
  • vsp_load_file
  • vsp_log_msg
  • vsp_logger
  • vsp_make_log_list_tree
  • vsp_maybe_load
  • vsp_placeholder_img
  • vsp_print_log_files_ui
  • vsp_register_assets
  • vsp_register_plugin
  • vsp_send_callback_error
  • vsp_send_callback_success
  • vsp_send_json_callback
  • vsp_set_cache
  • vsp_set_time_limit
  • vsp_slashit
  • vsp_time_format
  • vsp_unslashit
  • vsp_url
  • vsp_validate_required_plugin
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 
<?php

namespace VSP\Core\Traits;

defined( 'ABSPATH' ) || exit;

use Exception;
use WC_Payment_Gateway;
use WC_Payment_Gateways;
use WC_Shipping;
use WC_Shipping_Zones;
use WC_Tax;
use WPOnion\Exception\Cache_Not_Found;

/**
 * Trait WC_Helper
 *
 * @package VSP\Core\Traits
 * @author Varun Sridharan <varunsridharan23@gmail.com>
 */
trait WC_Helper {
    /**
     * Fetches Gateways From WooCommerce And Returns It.
     *
     * @param bool   $slug
     * @param string $cache_key Custom Cache key for customized data via filter.
     *
     * @return array|mixed
     */
    public static function wc_payment_methods( $slug = false, $cache_key = 'vsp' ) {
        try {
            return ( $slug ) ? vsp_get_cache( $cache_key . '/wc/payment_methods/slugs' ) : vsp_get_cache( $cache_key . '/wc/payment_methods/all' );
        } catch ( Cache_Not_Found $exception ) {
            $methods  = WC_Payment_Gateways::instance()->payment_gateways;
            $gateways = array();
            $slugs    = array();
            /*  @var WC_Payment_Gateway $method */
            foreach ( $methods as $method ) {
                if ( 'yes' === $method->enabled ) {
                    if ( 'Mollie' === $method->title && $method->has_mollie() ) {
                        $instance = $GLOBALS['wc_mollie'];
                        $gateway  = $instance->get_gateway();
                        $methods  = $gateway->get_methods();
                        foreach ( $methods as $method ) {
                            $method->id           = 'mollie_' . $method->id;
                            $method->description  = $method->description . ' (Mollie)';
                            $gateways[]           = $method;
                            $slugs[ $method->id ] = apply_filters( 'vsp/wc/payment_gateway/label', $method->title, $method, $cache_key );
                        }
                    } else {
                        $gateways[]           = $method;
                        $slugs[ $method->id ] = apply_filters( 'vsp/wc/payment_gateway/label', $method->title, $method, $cache_key );
                    }
                }
            }
            vsp_set_cache( $cache_key . '/wc/paymet_methods/slugs', $slugs );
            vsp_set_cache( $cache_key . '/wc/paymet_methods/all', $gateways );
            return ( $slug ) ? $slugs : $gateways;
        }
    }

    /**
     * @param bool   $slug
     * @param string $cache_key Custom Cache key for customized data via filter.
     *
     * @return mixed
     * @throws \Exception
     */
    public static function wc_shipping_methods( $slug = false, $cache_key = 'vsp' ) {
        try {
            return ( $slug ) ? vsp_get_cache( $cache_key . '/wc/shipping_methods/slugs' ) : vsp_get_cache( $cache_key . '/wc/shipping_methods/all' );
        } catch ( Cache_Not_Found $exception ) {
            $slugs      = array();
            $is_smv     = ( ! empty( WC()->shipping->shipping_methods ) && count( WC()->shipping->shipping_methods ) > 0 );
            $s_instance = WC_Shipping::instance();

            try {
                $shipping_methods = ( $is_smv ) ? $s_instance->shipping_methods : $s_instance->load_shipping_methods();
            } catch ( Exception $exception ) {
                $shipping_methods = array();
            }

            if ( ! empty( $shipping_methods ) ) {
                foreach ( $shipping_methods as $method ) {
                    $slugs[ $method->id ] = apply_filters( 'vsp/wc/shipping_methods/label', $method->method_title, $method );
                }

                vsp_set_cache( $cache_key . '/wc/shipping_methods/slugs', $slugs );
                vsp_set_cache( $cache_key . '/wc/shipping_methods/all', $shipping_methods );
            }
            return ( $slug ) ? $slugs : $shipping_methods;
        }
    }

    /**
     * Fetches All Shipping Methods By Instance ID.
     *
     * @param string $cache_key Custom Cache key for customized data via filter.
     *
     * @return array
     */
    public static function wc_shipping_methods_by_instance( $cache_key = 'vsp' ) {
        try {
            return vsp_get_cache( $cache_key . '/wc/shipping_methods_instance' );
        } catch ( Cache_Not_Found $exception ) {
            /**
             * @var \WC_Shipping_Zone   $zone
             * @var \WC_Shipping_Method $shipping_method
             */
            try {
                $delivery_zones = WC_Shipping_Zones::get_zones();
            } catch ( Exception $exception_inner ) {
                $delivery_zones = array();
            }
            $return = array();
            if ( ! empty( $delivery_zones ) ) {
                foreach ( $delivery_zones as $zone ) {
                    if ( ! empty( $zone['shipping_methods'] ) ) {
                        $store = array();
                        foreach ( $zone['shipping_methods'] as $shipping_method ) {
                            $store[ $shipping_method->get_instance_id() ] = apply_filters( 'vsp/wc/shipping_methods/instance/label', $shipping_method->get_title(), $shipping_method );
                        }
                        $return[ $zone['zone_name'] ] = $store;
                    }
                }
            }
            $exception->set( $return );
            return $return;
        }
    }

    /**
     * Fetches & Returns Product's SKU.
     *
     * @param $product_id
     *
     * @return mixed
     */
    public static function wc_product_sky_by_id( $product_id ) {
        return get_post_meta( $product_id, '_sku', true );
    }

    /**
     * Checks if product exists in cart.
     *
     * @param $product_id
     *
     * @return bool
     */
    public static function wc_has_product_in_cart( $product_id ) {
        if ( method_exists( wc()->cart, 'get_cart' ) ) {
            foreach ( wc()->cart->get_cart() as $key => $val ) {
                $_product = $val['data'];

                if ( $product_id === $_product->get_id() ) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Returns A list of Product ids From WooComerce Cart.
     *
     * @param string      $type ARRAY_A / ARRAY_N.
     * @param bool|string $child If set to true then it also adds variation ids,
     *
     * @return array
     * @since 0.8.7
     */
    public static function wc_get_product_ids_in_cart( $type = ARRAY_A, $child = true ) {
        $return = array();
        if ( method_exists( wc()->cart, 'get_cart' ) ) {
            foreach ( wc()->cart->get_cart() as $key => $val ) {
                /** @var \WC_Product $_product */
                $_product = $val['data'];
                $parent   = $_product->get_parent_id();
                $id       = $_product->get_id();
                $key      = ( 0 === $parent ) ? $id : $parent;

                if ( 'only' === $child && $parent > 0 ) {
                    $return[ $id ] = $id;
                } elseif ( true === $child ) {
                    $return[ $key ] = $id;
                } elseif ( false === $child ) {
                    $return[ $id ] = $id;
                }
            }
            return ( ARRAY_N === $type ) ? array_keys( $return ) : $return;
        }
        return array();
    }

    /**
     * Clears WC Cart.
     *
     * @return bool
     */
    public static function wc_clear_cart() {
        if ( function_exists( 'wc' ) && method_exists( wc()->cart, 'empty_cart' ) ) {
            wc()->cart->empty_cart( true );
            return true;
        }
        return false;
    }

    /**
     * Checks if WooCommerce Cart is empty.
     *
     * @return bool
     * @since 0.8.7
     */
    public static function wc_is_cart_empty() {
        return ( method_exists( wc()->cart, 'get_cart' ) && empty( wc()->cart->get_cart() ) );
    }

    /**
     * Clears Cart if Not Empty.
     *
     * @return bool
     */
    public static function wc_clear_cart_if_notempty() {
        return ( function_exists( 'wc' ) && ! static::wc_is_cart_empty() ) ? static::wc_clear_cart() : false;
    }

    /**
     * Returns A List of enabled tax classes in WooCommerce.
     *
     * @param false  $slug if set to true then it returns only the tax class slugs
     * @param string $cache_key
     *
     * @return array|mixed
     */
    public static function wc_get_tax_classes( $slug = false, $cache_key = 'vsp' ) {
        try {
            return ( $slug ) ? vsp_get_cache( $cache_key . '/wc/tax_classes/slugs' ) : vsp_get_cache( $cache_key . '/wc/tax_classes/all' );
        } catch ( Cache_Not_Found $exception ) {
            $tax         = WC_Tax::get_tax_classes();
            $tax_slugs   = WC_Tax::get_tax_class_slugs();
            $tax_classes = array_combine( $tax_slugs, $tax );
            $tax_slugs[] = 'standard';
            $tax_classes = array_merge( array( 'standard' => esc_html__( 'Standard', 'vsp-framework' ) ), $tax_classes );

            vsp_set_cache( $cache_key . '/wc/tax_classes/slugs', $tax_slugs );
            vsp_set_cache( $cache_key . '/wc/tax_classes/all', $tax_classes );

            return ( $slug ) ? $tax_slugs : $tax_classes;
        }
    }
}

API documentation generated by ApiGen