Files

295 lines
7.3 KiB
PHP
Raw Permalink Normal View History

2020-03-20 22:59:47 -04:00
<?php
/**
* DataLoader - WC_Db_Loader
*
* Loads Models for WooCommerce objects defined in custom DB tables.
*
* @package WPGraphQL\WooCommerce\Data\Loader
* @since 0.5.0
*/
namespace WPGraphQL\WooCommerce\Data\Loader;
use GraphQL\Error\UserError;
use WPGraphQL\Data\Loader\AbstractDataLoader;
use WPGraphQL\WooCommerce\Data\Factory;
use WPGraphQL\WooCommerce\Model\Shipping_Method;
use WPGraphQL\WooCommerce\Model\Shipping_Zone;
2020-03-20 22:59:47 -04:00
use WPGraphQL\WooCommerce\Model\Tax_Rate;
/**
* Class WC_Db_Loader
*/
class WC_Db_Loader extends AbstractDataLoader {
/**
* Stores loaded data objects.
2020-03-20 22:59:47 -04:00
*
* @var array
*/
protected $loaded_objects;
/**
* Loader type
2021-01-21 20:55:37 -05:00
*
2020-03-20 22:59:47 -04:00
* @var string
*/
protected $loader_type;
2021-01-21 20:55:37 -05:00
2020-03-20 22:59:47 -04:00
/**
* WC_Db_Loader constructor
2021-01-21 20:55:37 -05:00
*
* @param \WPGraphQL\AppContext $context AppContext instance.
* @param string $loader_type Type of loader be initialized.
2020-03-20 22:59:47 -04:00
*/
public function __construct( $context, $loader_type ) {
$this->loader_type = $loader_type;
2020-03-20 22:59:47 -04:00
parent::__construct( $context );
}
2020-03-24 20:55:13 -04:00
2021-01-21 22:41:43 -05:00
/**
2020-03-20 22:59:47 -04:00
* Given array of keys, loads and returns a map consisting of keys from `keys` array and loaded
* posts as the values
*
* @param array $keys - array of IDs.
*
* @return array
2021-01-21 22:41:43 -05:00
* @throws \Exception Invalid loader type.
2020-03-20 22:59:47 -04:00
*/
public function loadKeys( array $keys ) {
$loader = null;
2020-03-27 13:07:09 -04:00
switch ( $this->loader_type ) {
2020-03-20 22:59:47 -04:00
case 'CART_ITEM':
$loader = [ $this, 'load_cart_item_from_key' ];
2020-03-20 22:59:47 -04:00
break;
case 'DOWNLOADABLE_ITEM':
$loader = [ $this, 'load_downloadable_item_from_id' ];
2020-03-20 22:59:47 -04:00
break;
case 'TAX_CLASS':
$loader = [ $this, 'load_tax_class_from_slug' ];
break;
2020-03-20 22:59:47 -04:00
case 'TAX_RATE':
$loader = [ $this, 'load_tax_rate_from_id' ];
2020-03-20 22:59:47 -04:00
break;
case 'ORDER_ITEM':
$loader = [ $this, 'load_order_item_from_id' ];
break;
case 'SHIPPING_METHOD':
$loader = [ $this, 'load_shipping_method_from_id' ];
break;
case 'SHIPPING_ZONE':
$loader = [ $this, 'load_shipping_zone_from_id' ];
break;
2020-03-20 22:59:47 -04:00
default:
2021-03-17 11:22:48 -04:00
/**
* For adding custom key types to this loader
2021-03-17 15:51:27 -04:00
*
2021-03-17 11:22:48 -04:00
* @param callable|null $loader Callback that gets entry from key.
* @param string $loader_type Used to determine loader being used for this instance of the loader.
*/
2021-03-17 15:51:27 -04:00
$loader = apply_filters( 'woographql_db_loader_func', null, $this->loader_type );
2020-03-20 22:59:47 -04:00
if ( empty( $loader ) ) {
throw new \Exception(
/* translators: %s: Loader Type */
sprintf( __( 'Loader type invalid: %s', 'graphql-for-ecommerce' ), $this->loader_type )
2020-03-20 22:59:47 -04:00
);
}
}//end switch
2020-03-20 22:59:47 -04:00
$loaded_items = [];
2020-03-20 22:59:47 -04:00
/**
2020-03-26 14:52:10 -04:00
* Loop over the keys and return an array of items.
2020-03-20 22:59:47 -04:00
*/
foreach ( $keys as $key ) {
2020-03-24 20:55:13 -04:00
$loaded_items[ $key ] = call_user_func( $loader, $key );
2020-03-20 22:59:47 -04:00
}
return ! empty( $loaded_items ) ? $loaded_items : [];
2020-03-20 22:59:47 -04:00
}
2021-01-21 20:55:37 -05:00
2020-03-20 22:59:47 -04:00
/**
* Returns the cart item connected the provided key.
2021-01-21 20:55:37 -05:00
*
2020-03-20 22:59:47 -04:00
* @param string $key - Cart item key.
*
* @return array
*/
public function load_cart_item_from_key( $key ) {
2020-03-24 20:55:13 -04:00
// Add the cart item's product and product variation to WC-CPT buffer.
2020-03-20 22:59:47 -04:00
return Factory::resolve_cart()->get_cart_item( $key );
}
/**
* Returns the downloadable item connected the provided IDs.
2021-01-21 20:55:37 -05:00
*
2020-03-20 22:59:47 -04:00
* @param int $id - Downloadable item ID.
2021-01-21 20:55:37 -05:00
*
2023-06-13 23:17:02 +03:00
* @return \WC_Customer_Download|null
2020-03-20 22:59:47 -04:00
*/
public function load_downloadable_item_from_id( $id ) {
$node = new \WC_Customer_Download( $id );
return 0 === $node->get_id() ? $node : null;
}
/**
* Returns the tax class connected the provided IDs.
*
* @param int $slug - Tax class slug.
*
* @return array|null
*/
public function load_tax_class_from_slug( $slug ) {
if ( 'standard' === $slug ) {
return [
'slug' => 'standard',
'name' => __( 'Standard rate', 'graphql-for-ecommerce' ),
];
} else {
$tax_class = \WC_Tax::get_tax_class_by( 'slug', $slug );
return is_array( $tax_class ) && ! empty( $tax_class ) ? $tax_class : null;
}
}
2020-03-20 22:59:47 -04:00
/**
* Returns the tax rate connected the provided IDs.
2021-01-21 20:55:37 -05:00
*
2021-01-21 22:41:43 -05:00
* @param int $id - Tax rate IDs.
2021-01-21 20:55:37 -05:00
*
* @return \WPGraphQL\WooCommerce\Model\Tax_Rate|null
2020-03-20 22:59:47 -04:00
*/
public function load_tax_rate_from_id( $id ) {
global $wpdb;
2023-06-13 23:17:02 +03:00
/**
* Get tax rate from WooCommerce.
*
* @var \stdClass&object{
2023-06-13 23:17:02 +03:00
* tax_rate_id: int,
* tax_rate_class: string,
* tax_rate_country: string,
* tax_rate_state: string,
* tax_rate: string,
* tax_rate_name: string,
* tax_rate_priority: int,
* tax_rate_compound: bool,
* tax_rate_shipping: bool,
* tax_rate_order: int,
* tax_rate_city: string,
* tax_rate_postcode: string,
* tax_rate_postcodes: string,
* tax_rate_cities: string
2023-06-13 23:17:02 +03:00
* } $rate
*/
2020-03-20 22:59:47 -04:00
$rate = \WC_Tax::_get_tax_rate( $id, OBJECT );
2023-06-13 23:17:02 +03:00
if ( ! empty( $rate ) && is_object( $rate ) ) {
$rate->tax_rate_city = '';
$rate->tax_rate_postcode = '';
$rate->tax_rate_postcodes = '';
$rate->tax_rate_cities = '';
2020-03-20 22:59:47 -04:00
// Get locales from a tax rate.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
2020-03-20 22:59:47 -04:00
$locales = $wpdb->get_results(
$wpdb->prepare(
"
SELECT location_code, location_type
2020-03-20 22:59:47 -04:00
FROM {$wpdb->prefix}woocommerce_tax_rate_locations
WHERE tax_rate_id = %d
",
2020-03-20 22:59:47 -04:00
$rate->tax_rate_id
)
);
$cities = [];
$postcodes = [];
2020-03-20 22:59:47 -04:00
foreach ( $locales as $locale ) {
if ( 'city' === $locale->location_type ) {
$cities[] = $locale->location_code;
} elseif ( 'postcode' === $locale->location_type ) {
$postcodes[] = $locale->location_code;
} else {
$rate->{'tax_rate_' . $locale->location_type} = $locale->location_code;
2020-03-20 22:59:47 -04:00
}
}
if ( ! empty( $cities ) ) {
$rate->tax_rate_cities = implode( ';', $cities );
$rate->tax_rate_city = end( $cities );
}
if ( ! empty( $postcodes ) ) {
$rate->tax_rate_postcodes = implode( ';', $postcodes );
$rate->tax_rate_postcode = end( $postcodes );
2020-03-20 22:59:47 -04:00
}
return new Tax_Rate( $rate );
} else {
return null;
}//end if
2020-03-20 22:59:47 -04:00
}
/**
* Returns the shipping method Model for the shipping method ID.
*
* @param int $id - Shipping method ID.
*
* @return \WPGraphQL\WooCommerce\Model\Shipping_Method
*
* @throws \GraphQL\Error\UserError Invalid object.
*/
public function load_shipping_method_from_id( $id ) {
$wc_shipping = \WC_Shipping::instance();
$methods = $wc_shipping->get_shipping_methods();
if ( empty( $methods[ $id ] ) ) {
throw new UserError(
/* translators: shipping method ID */
sprintf( __( 'No Shipping Method assigned to ID %s was found ', 'graphql-for-ecommerce' ), $id )
);
}
$method = $methods[ $id ];
return new Shipping_Method( $method );
}
/**
* Returns the shipping zone connected the provided IDs.
*
* @param int $id - Shipping zone IDs.
*
* @return \WPGraphQL\WooCommerce\Model\Shipping_Zone|null
*/
public function load_shipping_zone_from_id( $id ) {
/** @var \WC_Shipping_Zone|false $zone */
$zone = \WC_Shipping_Zones::get_zone( $id );
if ( false === $zone ) {
return null;
}
$zone = new Shipping_Zone( $zone );
return $zone;
}
/**
* Returns the order item connected the provided IDs.
*
* @param int $id - Order item IDs.
*
2023-06-13 23:17:02 +03:00
* @return \WPGraphQL\WooCommerce\Model\Order_Item|null
*/
public function load_order_item_from_id( $id ) {
$item = \WC()->order_factory::get_order_item( $id );
if ( false === $item ) {
return null;
}
2023-06-13 23:17:02 +03:00
$item = new \WPGraphQL\WooCommerce\Model\Order_Item( $item );
return $item;
}
2020-03-20 22:59:47 -04:00
}