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 {
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();
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;
}
}
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;
}
}
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 ) {
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;
}
}
public static function wc_product_sky_by_id( $product_id ) {
return get_post_meta( $product_id, '_sku', true );
}
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;
}
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 ) {
$_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();
}
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;
}
public static function wc_is_cart_empty() {
return ( method_exists( wc()->cart, 'get_cart' ) && empty( wc()->cart->get_cart() ) );
}
public static function wc_clear_cart_if_notempty() {
return ( function_exists( 'wc' ) && ! static::wc_is_cart_empty() ) ? static::wc_clear_cart() : false;
}
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;
}
}
}