WC_Cart_Session::populate_cart_from_order()privateWC 3.5.0

Get a cart from an order, if user has permission.

Метод класса: WC_Cart_Session{}

Возвращает

Массив.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->populate_cart_from_order( $order_id, $cart );
$order_id(int) (обязательный)
Order ID to try to load.
$cart(массив) (обязательный)
Current cart array.

Список изменений

С версии 3.5.0 Введена.

Код WC_Cart_Session::populate_cart_from_order() WC 8.7.0

private function populate_cart_from_order( $order_id, $cart ) {
	$order = wc_get_order( $order_id );

	if ( ! $order->get_id() || ! $order->has_status( apply_filters( 'woocommerce_valid_order_statuses_for_order_again', array( 'completed' ) ) ) || ! current_user_can( 'order_again', $order->get_id() ) ) {
		return;
	}

	if ( apply_filters( 'woocommerce_empty_cart_when_order_again', true ) ) {
		$cart = array();
	}

	$inital_cart_size = count( $cart );
	$order_items      = $order->get_items();

	foreach ( $order_items as $item ) {
		$product_id     = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $item->get_product_id() );
		$quantity       = $item->get_quantity();
		$variation_id   = (int) $item->get_variation_id();
		$variations     = array();
		$cart_item_data = apply_filters( 'woocommerce_order_again_cart_item_data', array(), $item, $order );
		$product        = $item->get_product();

		if ( ! $product ) {
			continue;
		}

		// Prevent reordering variable products if no selected variation.
		if ( ! $variation_id && $product->is_type( 'variable' ) ) {
			continue;
		}

		// Prevent reordering items specifically out of stock.
		if ( ! $product->is_in_stock() ) {
			continue;
		}

		foreach ( $item->get_meta_data() as $meta ) {
			if ( taxonomy_is_product_attribute( $meta->key ) || meta_is_product_attribute( $meta->key, $meta->value, $product_id ) ) {
				$variations[ $meta->key ] = $meta->value;
			}
		}

		if ( ! apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data ) ) {
			continue;
		}

		// Add to cart directly.
		$cart_id          = WC()->cart->generate_cart_id( $product_id, $variation_id, $variations, $cart_item_data );
		$product_data     = wc_get_product( $variation_id ? $variation_id : $product_id );
		$cart[ $cart_id ] = apply_filters(
			'woocommerce_add_order_again_cart_item',
			array_merge(
				$cart_item_data,
				array(
					'key'          => $cart_id,
					'product_id'   => $product_id,
					'variation_id' => $variation_id,
					'variation'    => $variations,
					'quantity'     => $quantity,
					'data'         => $product_data,
					'data_hash'    => wc_get_cart_item_data_hash( $product_data ),
				)
			),
			$cart_id
		);
	}

	do_action_ref_array( 'woocommerce_ordered_again', array( $order->get_id(), $order_items, &$cart ) );

	$num_items_in_cart           = count( $cart );
	$num_items_in_original_order = count( $order_items );
	$num_items_added             = $num_items_in_cart - $inital_cart_size;

	if ( $num_items_in_original_order > $num_items_added ) {
		wc_add_notice(
			sprintf(
				/* translators: %d item count */
				_n(
					'%d item from your previous order is currently unavailable and could not be added to your cart.',
					'%d items from your previous order are currently unavailable and could not be added to your cart.',
					$num_items_in_original_order - $num_items_added,
					'woocommerce'
				),
				$num_items_in_original_order - $num_items_added
			),
			'error'
		);
	}

	if ( 0 < $num_items_added ) {
		wc_add_notice( __( 'The cart has been filled with the items from your previous order.', 'woocommerce' ) );
	}

	return $cart;
}