mirror of
https://github.com/wp-graphql/wp-graphql-woocommerce.git
synced 2026-08-14 12:53:44 +02:00
* feat: QL Session Handler functionality expanded to support cookies on non-GraphQL requests * chore: Linter and PHPStan compliance met * devops: QLSessionHandlerTest patched for suite testing * chore: Linter and PHPStan compliance met * fix: More cart session save triggered implemented * fix: More cart session save triggered implemented * chore: Linter compliance met * chore: Linter compliance met * feat: forgetSession mutation added * feat: forgetSession mutation added
57 lines
1.3 KiB
PHP
57 lines
1.3 KiB
PHP
<?php
|
|
/**
|
|
* Factory class for the WooCommerce's Cart data objects.
|
|
*
|
|
* @since v0.8.0
|
|
* @package Tests\WPGraphQL\WooCommerce\Factory
|
|
*/
|
|
|
|
namespace Tests\WPGraphQL\WooCommerce\Factory;
|
|
|
|
/**
|
|
* Cart factory class for testing.
|
|
*/
|
|
class CartFactory {
|
|
/**
|
|
* Add products to the cart.
|
|
*
|
|
* @param array<array<string, mixed>> ...$products Product to be added to the cart.
|
|
*
|
|
* @return array<string>
|
|
*/
|
|
public function add( ...$products ) {
|
|
$keys = [];
|
|
|
|
foreach ( $products as $product ) {
|
|
if ( gettype( $product ) === 'array' ) {
|
|
if ( empty( $product['product_id'] ) ) {
|
|
codecept_debug( $product );
|
|
codecept_debug( 'IS AN INVALID CART ITEM' );
|
|
continue;
|
|
}
|
|
|
|
$keys[] = WC()->cart->add_to_cart(
|
|
$product['product_id'],
|
|
! empty( $product['quantity'] ) ? $product['quantity'] : 1,
|
|
! empty( $product['variation_id'] ) ? $product['variation_id'] : 0,
|
|
! empty( $product['variation'] ) ? $product['variation'] : [],
|
|
! empty( $product['cart_item_data'] ) ? $product['cart_item_data'] : []
|
|
);
|
|
} else {
|
|
WC()->cart->add_to_cart( $product, 1 );
|
|
}
|
|
}
|
|
|
|
return $keys;
|
|
}
|
|
|
|
public function remove( ...$keys ) {
|
|
foreach ( $keys as $key ) {
|
|
$success = \WC()->cart->remove_cart_item( $key );
|
|
if ( false === $success ) {
|
|
codecept_debug( "FAILED TO REMOVE ITEM {$key} FROM CART." );
|
|
}
|
|
}
|
|
}
|
|
}
|