Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function commodities($items) {
- $xml_sub= " <commodities>\r\n";
- foreach ($items as $product) {
- do_action( 'woocommerce/cart_loop/start', $product );
- //Check if the product is virtual. If so, then skip it.
- $is_virtual = get_post_meta( $product['product_id'], '_virtual', true );
- if ( $is_virtual == 'yes' ) {
- do_action( 'woocommerce/cart_loop/end', $product );
- continue;
- }
- $only_virtual = 'no';
- $product_id = $product['product_id'];
- $product_instance = wc_get_product($product_id);
- // If it's a product variation, get the product_data from the variation field instead.
- $variable_product = new WC_Product_Variation( $product['variation_id'] );
- if ( preg_match( '/^{"id":0,".*/', $variable_product ) ) {
- $product_data = new WC_Product( $product['product_id'] );
- }
- else {
- $product_data = $variable_product;
- }
- $description = get_post_meta( $product_data->get_id(), 'customs_description', true);
- if (empty($description)) {
- $description = $product_data->get_attribute('customs_description');
- }
- if (empty($description)) {
- $terms1 = wc_get_product_terms( $product_id, 'product_brand', array('orderby' => 'term_id', 'order' => 'ASC', 'fields' => 'names') );
- $terms2 = wc_get_product_terms( $product_id, 'pa_brands', array('orderby' => 'term_id', 'order' => 'ASC', 'fields' => 'names') );
- $terms3 = wc_get_product_terms( $product_id, 'pa_colors', array('orderby' => 'term_id', 'order' => 'ASC', 'fields' => 'names') );
- $terms4 = wc_get_product_terms( $product_id, 'product_cat', array('orderby' => 'term_id', 'order' => 'ASC', 'fields' => 'names') );
- $terms5 = wc_get_product_terms( $product_id, 'product_tag', array('orderby' => 'term_id', 'order' => 'ASC', 'fields' => 'names') );
- $terms = array_merge($terms1, $terms2, $terms3, $terms4, $terms5);
- $description = implode(", ", $terms);
- }
- if (empty($description)) {
- $regular_product_description = $product_instance->get_description();
- $short_product_description = $product_instance->get_short_description();
- $description = ($regular_product_description == '') ? $short_product_description : $regular_product_description;
- $description = ($description == '') ? $product['name'] : $description;
- }
- $description = preg_replace('/[^A-ZÅÄÖa-zåäö0-9\-\,\ ]/', '', $description );
- $hscode = get_post_meta( $product_data->get_id(), 'hscode', true);
- if (empty($hscode)) {
- $hscode = get_post_meta( $product_data->get_id(), 'taric', true);
- }
- if (empty($hscode)) {
- $hscode = $product_data->get_attribute('hscode');
- }
- if (empty($hscode)) {
- $hscode = $product_data->get_attribute('taric');
- }
- $country = get_post_meta( $product_data->get_id(), 'country', true);
- if (empty($country)) {
- $country = get_post_meta( $product_data->get_id(), 'Country', true);
- }
- if (empty($country)) {
- $country = $product_data->get_attribute('country');
- }
- if (empty($country)) {
- $country = $product_data->get_attribute('Country');
- }
- $fraktjakt_shipping_method_settings = get_option( 'woocommerce_fraktjakt_shipping_method_settings' );
- $fraktjakt_vikt=wc_get_weight( $product_data->get_weight(), 'kg', get_option( 'woocommerce_weight_unit' ) );
- $dimension_unit = get_option( 'woocommerce_dimension_unit' );
- $xml_sub.= ' <commodity>' . "\r\n";
- $xml_sub.= ' <name>'. str_replace(array("\r","\n"), "", strip_tags($product_data->get_name()) ) .'</name>' . "\r\n";
- $xml_sub.= ' <quantity>'. $product['quantity'] .'</quantity>' . "\r\n";
- $xml_sub.= ' <taric>'. $hscode .'</taric>' . "\r\n";
- $xml_sub.= ' <country_of_manufacture>'. $country .'</country_of_manufacture>' . "\r\n";
- $xml_sub.= ' <quantity_units>EA</quantity_units>' . "\r\n";
- $xml_sub.= ' <description>'. substr(strip_tags($description),0,80) .'</description>' . "\r\n";
- $xml_sub.= ' <article_number>'. $product_data->get_sku() .'</article_number>' . "\r\n";
- $xml_sub.= ' <unit_price>'. wc_get_price_to_display($product_data) .'</unit_price>' . "\r\n";
- $xml_sub.= ' <currency>' . (empty($fraktjakt_shipping_method_settings['currency_conversion']) ? 'SEK' : ( $fraktjakt_shipping_method_settings['currency_conversion'] == 'SEK' ? "SEK" : ( $fraktjakt_shipping_method_settings['currency_conversion'] == 'EUR' ? "EUR" : get_woocommerce_currency() ) )) . '</currency>' . "\r\n";
- $xml_sub.= ' <weight>'. ($fraktjakt_vikt * $product['quantity']) .'</weight>' . "\r\n";
- $length = $product_data->get_length() ? wc_get_dimension( $product_data->get_length(), 'cm', $dimension_unit ) : '';
- $xml_sub.= ' <length>'.$length.'</length>'."\r\n";
- $width = $product_data->get_width() ? wc_get_dimension( $product_data->get_width(), 'cm', $dimension_unit ) : '';
- $xml_sub.= ' <width>'.$width.'</width>'."\r\n";
- $height = $product_data->get_height() ? wc_get_dimension( $product_data->get_height(), 'cm', $dimension_unit ) : '';
- $xml_sub.= ' <height>'.$height.'</height>'."\r\n";
- $xml_sub.= ' </commodity>' . "\r\n";
- do_action( 'woocommerce/cart_loop/end', $product );
- }
- if ($xml_sub==" <commodities>\r\n") {
- return "";
- }
- $xml_sub.= ' </commodities>' . "\r\n";
- return $xml_sub;
- }
Advertisement
Comments
-
- edit the file fraktjakt-woocommerce-shipping.php
Add Comment
Please, Sign In to add comment
Advertisement