Friday, May 3, 2024
HomeWordPress Developmentphp - Woocommerce Convert current order to the cart

php – Woocommerce Convert current order to the cart


How you can switch an current and modified order with all its particulars and its objects to the cart.

I wish to set the costs straight equal to the modified order in addition to the totals with out referring to a reduction.

I used this code however one thing remains to be lacking

                foreach ( $order->get_items() as $merchandise ) {
                    $product_id   = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $item->get_product_id() );
                    $amount     = $item->get_quantity();
                    $variation_id = (int) $item->get_variation_id();
                    $variations   = array();

                    foreach ( $item->get_meta_data() as $meta ) {
                        if ( taxonomy_is_product_attribute( $meta->key ) ) {
                            $time period                                    = get_term_by( 'slug', $meta->worth, $meta->key );
                            $variations[ 'attribute_' . $meta->key ] = $time period ? $term->worth : $meta->worth;
                        } elseif ( meta_is_product_attribute( $meta->key, $meta->worth, $product_id ) ) {
                            $variations[ 'attribute_' . $meta->key ] = $meta->worth;
                        }
                    }

                    $cart_item_data = apply_filters( 'woocommerce_order_again_cart_item_data', array(), $merchandise, $order );

                    // Add to cart validation.
                    remove_filter( 'woocommerce_add_to_cart_validation', array( $this, 'cart_validation' ), 10 );

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

                    $meta_data = $item->get_meta_data();

                    if ( ! empty( $meta_data ) ) {

                        foreach ( $meta_data as $meta ) {
                            $cart_item_data['meta'][] = $meta->get_data();
                        }
                    }

                    $cart_item_data['meta']['total'] = $item->get_total();
                    $cart_item_data['meta']['subtotal'] = $item->get_subtotal();

                    WC()->cart->add_to_cart( $product_id, $amount, $variation_id, $variations, $cart_item_data );
                }

                WC()->cart->calculate_totals();

with name the hook add_action('woocommerce_before_calculate_totals')

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments