Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Add: Cross-sells for the Store API Cart response. (#6635)
Browse files Browse the repository at this point in the history
* Added Cross-sells object and schema for the API Cart response.

* Cross-sells are now WC_Product objects that pass through the visibility filter.

* Removed redundant check.

* Updated function doc comment.

* Cleaned up imports.

* Cross-sells item schema (extension of ProductSchema) was removed, and replaced by ProductSchema itself. Cross-sells are direct product representation, and extending this goes out of scope for the task at hand.

* Unit testing for cart response containing cross-sell products.
  • Loading branch information
wavvves authored Jul 13, 2022
1 parent 2f74259 commit 1bce6a1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/StoreApi/SchemaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function __construct( ExtendSchema $extend ) {
Schemas\V1\BillingAddressSchema::IDENTIFIER => Schemas\V1\BillingAddressSchema::class,
Schemas\V1\ShippingAddressSchema::IDENTIFIER => Schemas\V1\ShippingAddressSchema::class,
Schemas\V1\CartShippingRateSchema::IDENTIFIER => Schemas\V1\CartShippingRateSchema::class,
Schemas\V1\CartShippingRateSchema::IDENTIFIER => Schemas\V1\CartShippingRateSchema::class,
Schemas\V1\CartCouponSchema::IDENTIFIER => Schemas\V1\CartCouponSchema::class,
Schemas\V1\CartFeeSchema::IDENTIFIER => Schemas\V1\CartFeeSchema::class,
Schemas\V1\CartItemSchema::IDENTIFIER => Schemas\V1\CartItemSchema::class,
Expand Down
22 changes: 22 additions & 0 deletions src/StoreApi/Schemas/V1/CartSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class CartSchema extends AbstractSchema {
*/
public $coupon_schema;

/**
* Product item schema instance representing cross-sell items.
*
* @var ProductSchema
*/
public $cross_sells_item_schema;

/**
* Fee schema instance.
*
Expand Down Expand Up @@ -84,6 +91,7 @@ class CartSchema extends AbstractSchema {
public function __construct( ExtendSchema $extend, SchemaController $controller ) {
parent::__construct( $extend, $controller );
$this->item_schema = $this->controller->get( CartItemSchema::IDENTIFIER );
$this->cross_sells_item_schema = $this->controller->get( ProductSchema::IDENTIFIER );
$this->coupon_schema = $this->controller->get( CartCouponSchema::IDENTIFIER );
$this->fee_schema = $this->controller->get( CartFeeSchema::IDENTIFIER );
$this->shipping_rate_schema = $this->controller->get( CartShippingRateSchema::IDENTIFIER );
Expand Down Expand Up @@ -155,6 +163,16 @@ public function get_properties() {
'context' => [ 'view', 'edit' ],
'readonly' => true,
],
'cross_sells' => [
'description' => __( 'List of cross-sells items related to cart items.', 'woo-gutenberg-products-block' ),
'type' => 'array',
'context' => [ 'view', 'edit' ],
'readonly' => true,
'items' => [
'type' => 'object',
'properties' => $this->force_schema_readonly( $this->cross_sells_item_schema->get_properties() ),
],
],
'needs_payment' => [
'description' => __( 'True if the cart needs payment. False for carts with only free products and no shipping costs.', 'woo-gutenberg-products-block' ),
'type' => 'boolean',
Expand Down Expand Up @@ -323,6 +341,9 @@ public function get_item_response( $cart ) {
// Get shipping packages to return in the response from the cart.
$shipping_packages = $has_calculated_shipping ? $controller->get_shipping_packages() : [];

// Get visible cross sells products.
$cross_sells = array_filter( array_map( 'wc_get_product', $cart->get_cross_sells() ), 'wc_products_array_filter_visible' );

return [
'coupons' => $this->get_item_responses_from_schema( $this->coupon_schema, $cart->get_applied_coupons() ),
'shipping_rates' => $this->get_item_responses_from_schema( $this->shipping_rate_schema, $shipping_packages ),
Expand All @@ -331,6 +352,7 @@ public function get_item_response( $cart ) {
'items' => $this->get_item_responses_from_schema( $this->item_schema, $cart->get_cart() ),
'items_count' => $cart->get_cart_contents_count(),
'items_weight' => wc_get_weight( $cart->get_cart_contents_weight(), 'g' ),
'cross_sells' => $this->get_item_responses_from_schema( $this->cross_sells_item_schema, $cross_sells ),
'needs_payment' => $cart->needs_payment(),
'needs_shipping' => $cart->needs_shipping(),
'has_calculated_shipping' => $has_calculated_shipping,
Expand Down
23 changes: 20 additions & 3 deletions tests/php/StoreApi/Routes/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace Automattic\WooCommerce\Blocks\Tests\StoreApi\Routes;

use Automattic\WooCommerce\Blocks\Tests\StoreApi\Routes\ControllerTestCase;
use Automattic\WooCommerce\Blocks\Tests\Helpers\FixtureData;
use Automattic\WooCommerce\Blocks\Tests\Helpers\ValidateSchema;

Expand Down Expand Up @@ -40,8 +39,21 @@ public function setUp() {
'weight' => 10,
)
),

$fixtures->get_simple_product(
array(
'name' => 'Test Product 3',
'stock_status' => 'instock',
'regular_price' => 10,
'weight' => 10,
)
),
);

// Add product #3 as a cross-sell for product #1.
$this->products[0]->set_cross_sell_ids( array( $this->products[2]->get_id() ) );
$this->products[0]->save();

$this->coupon = $fixtures->get_coupon(
array(
'code' => 'test_coupon',
Expand All @@ -53,7 +65,7 @@ public function setUp() {
wc_empty_cart();
$this->keys = array();
$this->keys[] = wc()->cart->add_to_cart( $this->products[0]->get_id(), 2 );
$this->keys[] = wc()->cart->add_to_cart( $this->products[1]->get_id(), 1 );
$this->keys[] = wc()->cart->add_to_cart( $this->products[1]->get_id() );
wc()->cart->apply_coupon( $this->coupon->get_code() );

// Draft order.
Expand All @@ -78,6 +90,11 @@ public function test_get_item() {
'items' => function( $value ) {
return count( $value ) === 2;
},
'cross_sells' => array(
array(
'id' => $this->products[2]->get_id(),
),
),
'totals' => array(
'currency_code' => 'USD',
'currency_minor_unit' => 2,
Expand Down Expand Up @@ -475,6 +492,7 @@ public function test_prepare_item() {
$this->assertArrayHasKey( 'needs_shipping', $data );
$this->assertArrayHasKey( 'items_weight', $data );
$this->assertArrayHasKey( 'totals', $data );
$this->assertArrayHasKey( 'cross_sells', $data );
}

/**
Expand All @@ -483,7 +501,6 @@ public function test_prepare_item() {
public function test_get_item_schema() {
$routes = new \Automattic\WooCommerce\StoreApi\RoutesController( new \Automattic\WooCommerce\StoreApi\SchemaController( $this->mock_extend ) );
$controller = $routes->get( 'cart', 'v1' );
$schema = $controller->get_item_schema();
$cart = wc()->cart;
$response = $controller->prepare_item_for_response( $cart, new \WP_REST_Request() );
$schema = $controller->get_item_schema();
Expand Down

0 comments on commit 1bce6a1

Please sign in to comment.