Files
Geoff TaylorandGitHub 76e8db510e feat: QL Session Handler refactored to handle non-GraphQL requests (#870)
* 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
2024-08-07 12:39:45 -04:00

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." );
}
}
}
}