mirror of
https://github.com/wp-graphql/wp-graphql-woocommerce.git
synced 2026-08-14 12:53:44 +02:00
* chore: bump min versions to actual and update deps
* chore: fix multiple empty lines at EOF
* chore: preincrement when standalone (phpcs)
* fix: use `printf` instead of `echo sprintf` [phpcs]
* chore: avoid lonely `if()` in `else{}` [phpcs]
* chore: avoid reserved keywords as param names [phpcs]
* chore: remove unused callback params [phpcs]
* chore: remove more unused callback params [phpcs]
* chore: apply lints to tests
* chore: update ruleset for PHPCS 3.0
* texts: restore $last_request_headers
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 ...$products Product to be added to the cart.
|
|
*
|
|
* @return array
|
|
*/
|
|
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." );
|
|
}
|
|
}
|
|
}
|
|
}
|