WC_Cart::check_cart_item_sold_individually
Looks through cart items and ensures sold individually products have quantity of 1.
Method of the class: WC_Cart{}
No Hooks.
Returns
true|false|WP_Error.
Usage
$WC_Cart = new WC_Cart(); $WC_Cart->check_cart_item_sold_individually();
Changelog
| Since 10.7.0 | Introduced. |
WC_Cart::check_cart_item_sold_individually() WC Cart::check cart item sold individually code WC 10.7.0
public function check_cart_item_sold_individually() {
$errors = new WP_Error();
foreach ( $this->get_cart() as $cart_item_key => $values ) {
$product = $values['data'];
if ( ! $product || ! $product->exists() ) {
continue;
}
$product_id = $values['variation_id'] ? $values['variation_id'] : $values['product_id'];
$product_to_check = wc_get_product( $product_id );
if ( ! $product_to_check || ! $product_to_check->exists() ) {
continue;
}
if ( $product_to_check->is_sold_individually() && $values['quantity'] > 1 ) {
// Re-fetch and overwrite to reflect product changes made after item was added to cart.
$this->cart_contents[ $cart_item_key ]['data'] = $product_to_check;
$this->set_quantity( $cart_item_key, 1, false );
/* translators: %s: product name */
$errors->add( 'sold-individually', sprintf( __( 'You can only have 1 %s in your cart.', 'woocommerce' ), $product_to_check->get_name() ) );
}
}
return $errors->has_errors() ? $errors : true;
}