• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WooCommerce a2z

WooCommerce a2z

WooCommerce

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / CartController::set_cart_item_quantity() – Based on core `set_quantity` method, but validates if an item is sold individually first.

You appear to be a bot. Output may be restricted

Description

Based on core set_quantity method, but validates if an item is sold individually first.

Usage

CartController::set_cart_item_quantity( $item_id, $quantity );

Parameters

$item_id
( string ) required – Cart item id.
$quantity
( integer ) optional default: 1 – Cart quantity.

Returns

void

Source

File name: woocommerce/packages/woocommerce-blocks/src/StoreApi/Utilities/CartController.php
Lines:

1 to 27 of 27
  public function set_cart_item_quantity( $item_id, $quantity = 1 ) {
    $cart_item = $this->get_cart_item( $item_id );

    if ( empty( $cart_item ) ) {
      throw new RouteException( 'woocommerce_rest_cart_invalid_key', __( 'Cart item does not exist.', 'woocommerce' ), 404 );
    }

    $product = $cart_item['data'];

    if ( ! $product instanceof \WC_Product ) {
      throw new RouteException( 'woocommerce_rest_cart_invalid_product', __( 'Cart item is invalid.', 'woocommerce' ), 404 );
    }

    if ( $product->is_sold_individually() && $quantity > 1 ) {
      throw new RouteException(
        'woocommerce_rest_cart_product_sold_individually',
        sprintf(
          /* translators: %s: product name */
          __( 'You cannot add another "%s" to your cart.', 'woocommerce' ),
          $product->get_name()
        ),
        400
      );
    }
    $cart = $this->get_cart_instance();
    $cart->set_quantity( $item_id, $quantity );
  }
 

 View on GitHub View on Trac

Published: 21st August 2020 | Last updated: 22nd February 2021

Primary Sidebar

Information

Function name: CartController::set_cart_item_quantity
Class ref: CartController
Plugin ref: WooCommerce
Version: 5.1.0
Sourcefile: packages/woocommerce-blocks/src/StoreApi/Utilities/CartController.php
File ref: packages/woocommerce-blocks/src/StoreApi/Utilities/CartController.php
Deprecated?: No
API Letters: C,I,Q,S

Footer

WooCommerce a2z
WooCommerce a2z
WooCommerce
WordPress 5.7
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  woocommerce.wp-a2z.org
© Copyright WooCommerce a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites