WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru
функция не описана

WC_Cart_Session::populate_cart_from_order() WC 3.5.0

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

Это метод класса: WC_Cart_Session

Хуки из метода
Возвращает

Массив.

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

$WC_Cart_Session = new WC_Cart_Session();
$var = $WC_Cart_Session->populate_cart_from_order( $order_id, $cart );
$order_id(число) (обязательный)
Order ID to try to load.
$cart(массив) (обязательный)
Current cart array.

Код WC Cart Session::populate cart from order: woocommerce/includes/class-wc-cart-session.php WC 3.6.3

<?php
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 ) ) {
				$term                     = get_term_by( 'slug', $meta->value, $meta->key );
				$variations[ $meta->key ] = $term ? $term->name : $meta->value;
			} elseif ( 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;
}