Last active
October 31, 2021 22:17
-
-
Save victormattosvm/b0d7506b5757fc12df9fdbe007884199 to your computer and use it in GitHub Desktop.
Compatibilidade entre o plugin Simulador de frete (Fernando Acosta) e Woocommerce Chained Products
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * If virtual product has chained, it needs shipping. | |
| * | |
| * @param bool $is_virtual Default value. | |
| * @param WC_Product $product The product object. | |
| */ | |
| add_filter('woocommerce_is_virtual', function( $is_virtual, $product ){ | |
| $chained_products = get_post_meta( $product->get_id(), '_chained_product_detail', true ); | |
| $has_chained = ( is_array( $chained_products ) ) ? true : false; | |
| if( is_product() && $has_chained ){ | |
| //If has chained, the product needs shipping | |
| return false; | |
| } | |
| return $is_virtual; | |
| }, 10, 2); | |
| /** | |
| * Add chained products to the package | |
| * | |
| * @param array $package The cart package. | |
| * @param WC_Product The product object. | |
| */ | |
| add_filter('wc_shipping_simulator_package', function( $package, $product ){ | |
| //parâmetro $product será depreciado | |
| //$product = isset( $package['contents'][0]['data'] ) ? $package['contents'][0]['data'] : null; | |
| $chained_products = get_post_meta( $product->get_id(), '_chained_product_detail', true ); | |
| $chained_product_ids = ( is_array( $chained_products ) ) ? array_keys( $chained_products ) : null; | |
| $quantity = $package['contents'][0]['quantity']; | |
| if( $chained_product_ids ){ | |
| $total_price = 0; | |
| $contents = array(); | |
| foreach ( $chained_product_ids as $product_id ) { | |
| $product = wc_get_product( $product_id ); | |
| $chained_item_qty = $chained_products[ $product_id ]['unit'] * $quantity; | |
| // produto invalido | |
| if ( ! $product ) { | |
| continue; | |
| } | |
| $product_id = $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id(); | |
| $variation_id = $product->is_type( 'variation' ) ? $product->get_id() : 0; | |
| $variation = $product->is_type( 'variation' ) ? $product->get_attributes() : array(); | |
| $line_total = ( array() !== $product ) ? $product->get_price() * $quantity : $product->get_price() * $quantity; | |
| $total_price += $line_total; | |
| $contents[$variation_id] = array( | |
| 'product_id' => $product_id, | |
| 'variation_id' => $variation_id, | |
| 'variation' => $variation, | |
| 'line_total' => $line_total, | |
| 'line_tax' => 0, | |
| 'line_subtotal' => $line_total, | |
| 'line_subtotal_tax' => 0, | |
| 'line_tax_data' => array( | |
| 'total' => array(), | |
| 'subtotal' => array(), | |
| ), | |
| 'quantity' => $chained_item_qty, | |
| 'data' => $product | |
| ); | |
| } | |
| $package['contents_cost'] = $total_price; | |
| $package['cart_subtotal'] = $total_price; | |
| $package['contents'] = $contents; | |
| } | |
| return $package; | |
| }, 20, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment