2020-10-22 15:59:11 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Factory class for the WooCommerce's Cart data objects.
|
|
|
|
|
*
|
2021-02-26 09:29:19 -05:00
|
|
|
* @since v0.8.0
|
2020-10-22 15:59:11 -04:00
|
|
|
* @package Tests\WPGraphQL\WooCommerce\Factory
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Tests\WPGraphQL\WooCommerce\Factory;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-10-23 12:09:53 -04:00
|
|
|
* Cart factory class for testing.
|
2020-10-22 15:59:11 -04:00
|
|
|
*/
|
2020-10-23 12:09:53 -04:00
|
|
|
class CartFactory {
|
|
|
|
|
/**
|
|
|
|
|
* Add products to the cart.
|
|
|
|
|
*
|
2024-08-07 12:39:45 -04:00
|
|
|
* @param array<array<string, mixed>> ...$products Product to be added to the cart.
|
2020-10-23 12:09:53 -04:00
|
|
|
*
|
2024-08-07 12:39:45 -04:00
|
|
|
* @return array<string>
|
2020-10-23 12:09:53 -04:00
|
|
|
*/
|
|
|
|
|
public function add( ...$products ) {
|
2022-08-26 14:53:36 -04:00
|
|
|
$keys = [];
|
2020-10-22 15:59:11 -04:00
|
|
|
|
2022-02-04 15:00:44 -05:00
|
|
|
foreach ( $products as $product ) {
|
2020-10-23 12:09:53 -04:00
|
|
|
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,
|
2022-08-26 14:53:36 -04:00
|
|
|
! empty( $product['variation'] ) ? $product['variation'] : [],
|
|
|
|
|
! empty( $product['cart_item_data'] ) ? $product['cart_item_data'] : []
|
2020-10-23 12:09:53 -04:00
|
|
|
);
|
|
|
|
|
} 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." );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-22 15:59:11 -04:00
|
|
|
}
|