Created
November 6, 2025 04:31
-
-
Save TanvirHasan19/dcc35e28c7ac0bb74d3cfb7b495d449e to your computer and use it in GitHub Desktop.
WC Vendors Google Merchant Center Integration
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 | |
| /** | |
| * WC Vendors Google Merchant Center Integration | |
| * | |
| * This code provides the missing functionality to retrieve vendor shipping data | |
| * from a product object for Google Merchant Center feed generation. | |
| */ | |
| /** | |
| * Get vendor ID from a WooCommerce product object | |
| * | |
| * @param WC_Product $product The WooCommerce product object | |
| * @return int|false Vendor ID or false if not found | |
| */ | |
| function wcv_get_vendor_id_from_product( $product ) { | |
| if ( ! is_a( $product, 'WC_Product' ) ) { | |
| return false; | |
| } | |
| $product_id = $product->get_id(); | |
| // Use WC Vendors built-in method to get vendor from product | |
| $vendor_id = WCV_Vendors::get_vendor_from_product( $product_id ); | |
| // Verify the vendor ID is valid | |
| if ( ! WCV_Vendors::is_vendor( $vendor_id ) ) { | |
| return false; | |
| } | |
| return $vendor_id; | |
| } | |
| /** | |
| * Get vendor shipping data for Google Merchant Center | |
| * | |
| * @param WC_Product $product The WooCommerce product object | |
| * @return array Vendor shipping data | |
| */ | |
| function wcv_get_vendor_shipping_for_gmc( $product ) { | |
| $vendor_id = wcv_get_vendor_id_from_product( $product ); | |
| if ( ! $vendor_id ) { | |
| return array(); | |
| } | |
| // Get vendor's shipping data | |
| $vendor_shipping = (array) get_user_meta( $vendor_id, '_wcv_shipping', true ); | |
| // Get global shipping settings as fallback | |
| $global_shipping_settings = get_option( 'woocommerce_wcv_pro_vendor_shipping_settings', wcv_get_default_vendor_shipping() ); | |
| // Merge vendor settings with global defaults | |
| $vendor_shipping = wp_parse_args( $vendor_shipping, $global_shipping_settings ); | |
| // Get additional vendor shipping information | |
| $shipping_data = array( | |
| 'vendor_id' => $vendor_id, | |
| 'shipping_type' => get_user_meta( $vendor_id, '_wcv_shipping_type', true ), | |
| 'store_country' => get_user_meta( $vendor_id, '_wcv_store_country', true ), | |
| 'store_state' => get_user_meta( $vendor_id, '_wcv_store_state', true ), | |
| 'vendor_shipping' => $vendor_shipping, | |
| 'global_settings' => $global_shipping_settings | |
| ); | |
| return $shipping_data; | |
| } | |
| /** | |
| * Get vendor shipping rates for specific destination | |
| * | |
| * @param WC_Product $product The WooCommerce product object | |
| * @param string $destination_country Destination country code | |
| * @param string $destination_state Destination state code (optional) | |
| * @return array Shipping rates | |
| */ | |
| function wcv_get_vendor_shipping_rates_for_destination( $product, $destination_country, $destination_state = '' ) { | |
| $shipping_data = wcv_get_vendor_shipping_for_gmc( $product ); | |
| if ( empty( $shipping_data ) ) { | |
| return array(); | |
| } | |
| $vendor_shipping = $shipping_data['vendor_shipping']; | |
| $shipping_type = $shipping_data['shipping_type']; | |
| $rates = array(); | |
| // Handle different shipping types | |
| if ( 'flat' === $shipping_type || empty( $shipping_type ) ) { | |
| // Flat rate shipping | |
| if ( ! empty( $vendor_shipping['shipping_flat_rate'] ) ) { | |
| $flat_rates = $vendor_shipping['shipping_flat_rate']; | |
| // Determine if destination is national or international | |
| $store_country = $shipping_data['store_country']; | |
| $is_national = ( strtolower( $destination_country ) === strtolower( $store_country ) ); | |
| if ( $is_national ) { | |
| $rates['national'] = array( | |
| 'rate' => $flat_rates['national'] ?? '', | |
| 'free_shipping_order' => $flat_rates['national_free_shipping_order'] ?? '', | |
| 'min_charge' => $flat_rates['national_min_charge'] ?? '', | |
| 'max_charge' => $flat_rates['national_max_charge'] ?? '', | |
| 'free' => $flat_rates['national_free'] ?? false | |
| ); | |
| } else { | |
| $rates['international'] = array( | |
| 'rate' => $flat_rates['international'] ?? '', | |
| 'free_shipping_order' => $flat_rates['international_free_shipping_order'] ?? '', | |
| 'min_charge' => $flat_rates['international_min_charge'] ?? '', | |
| 'max_charge' => $flat_rates['international_max_charge'] ?? '', | |
| 'free' => $flat_rates['international_free'] ?? false | |
| ); | |
| } | |
| } | |
| } elseif ( 'country' === $shipping_type ) { | |
| // Country table rates | |
| if ( ! empty( $vendor_shipping['shipping_table_rates'] ) ) { | |
| $table_rates = $vendor_shipping['shipping_table_rates']; | |
| foreach ( $table_rates as $rate ) { | |
| if ( strtolower( $rate['country'] ) === strtolower( $destination_country ) ) { | |
| if ( empty( $destination_state ) || strtolower( $rate['state'] ) === strtolower( $destination_state ) ) { | |
| $rates[] = array( | |
| 'country' => $rate['country'], | |
| 'state' => $rate['state'], | |
| 'rate' => $rate['fee'], | |
| 'postcode' => $rate['postcode'] ?? '', | |
| 'region' => $rate['region'] ?? '' | |
| ); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| return $rates; | |
| } | |
| /** | |
| * Generate Google Merchant Center shipping information | |
| * | |
| * @param WC_Product $product The WooCommerce product object | |
| * @return array GMC formatted shipping data | |
| */ | |
| function wcv_generate_gmc_shipping_data( $product ) { | |
| $shipping_data = wcv_get_vendor_shipping_for_gmc( $product ); | |
| if ( empty( $shipping_data ) ) { | |
| return array(); | |
| } | |
| $vendor_id = $shipping_data['vendor_id']; | |
| $vendor_shipping = $shipping_data['vendor_shipping']; | |
| // Get vendor store information | |
| $store_country = $shipping_data['store_country'] ?: WC()->countries->get_base_country(); | |
| $store_state = $shipping_data['store_state']; | |
| // Build GMC shipping data | |
| $gmc_shipping = array( | |
| 'vendor_id' => $vendor_id, | |
| 'origin_country' => $store_country, | |
| 'origin_state' => $store_state, | |
| 'shipping_type' => $shipping_data['shipping_type'], | |
| 'shipping_rates' => array(), | |
| 'shipping_policy' => $vendor_shipping['shipping_policy'] ?? '', | |
| 'return_policy' => $vendor_shipping['return_policy'] ?? '' | |
| ); | |
| // Add shipping rates based on type | |
| if ( 'flat' === $shipping_data['shipping_type'] || empty( $shipping_data['shipping_type'] ) ) { | |
| $flat_rates = $vendor_shipping['shipping_flat_rate'] ?? array(); | |
| // National rates | |
| if ( ! empty( $flat_rates['national'] ) ) { | |
| $gmc_shipping['shipping_rates']['national'] = array( | |
| 'country' => $store_country, | |
| 'rate' => $flat_rates['national'], | |
| 'free_shipping_threshold' => $flat_rates['national_free_shipping_order'] ?? '', | |
| 'min_charge' => $flat_rates['national_min_charge'] ?? '', | |
| 'max_charge' => $flat_rates['national_max_charge'] ?? '' | |
| ); | |
| } | |
| // International rates | |
| if ( ! empty( $flat_rates['international'] ) ) { | |
| $gmc_shipping['shipping_rates']['international'] = array( | |
| 'rate' => $flat_rates['international'], | |
| 'free_shipping_threshold' => $flat_rates['international_free_shipping_order'] ?? '', | |
| 'min_charge' => $flat_rates['international_min_charge'] ?? '', | |
| 'max_charge' => $flat_rates['international_max_charge'] ?? '' | |
| ); | |
| } | |
| } elseif ( 'country' === $shipping_data['shipping_type'] ) { | |
| // Country table rates | |
| $table_rates = $vendor_shipping['shipping_table_rates'] ?? array(); | |
| $gmc_shipping['shipping_rates']['table_rates'] = $table_rates; | |
| } | |
| return $gmc_shipping; | |
| } | |
| // Example usage for YITH Google Feed Plugin: | |
| /* | |
| // In your feed generation code: | |
| $product = wc_get_product( $product_id ); | |
| $vendor_shipping = wcv_generate_gmc_shipping_data( $product ); | |
| if ( ! empty( $vendor_shipping ) ) { | |
| // Use the shipping data in your feed | |
| $shipping_info = array( | |
| 'origin_country' => $vendor_shipping['origin_country'], | |
| 'origin_state' => $vendor_shipping['origin_state'], | |
| 'shipping_rates' => $vendor_shipping['shipping_rates'] | |
| ); | |
| // Add to your feed array | |
| $feed_data['shipping'] = $shipping_info; | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment