Advertisement
nshelper

WPGlobaCart - Fraktjakt Shipping Method for WooCommerce

May 15th, 2024 (edited)
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.46 KB | None | 0 0
  1. function commodities($items) {
  2.  
  3.     $xml_sub= "  <commodities>\r\n";
  4.     foreach ($items as $product) {
  5.        
  6.         do_action( 'woocommerce/cart_loop/start', $product );
  7.    
  8.         //Check if the product is  virtual. If so, then skip it.
  9.         $is_virtual = get_post_meta( $product['product_id'], '_virtual', true );
  10.             if ( $is_virtual == 'yes' ) {
  11.                 do_action( 'woocommerce/cart_loop/end', $product );
  12.                 continue;
  13.             }
  14.       $only_virtual = 'no';  
  15.       $product_id = $product['product_id'];
  16.             $product_instance = wc_get_product($product_id);
  17.                
  18.             // If it's a product variation, get the product_data from the variation field instead.
  19.             $variable_product = new WC_Product_Variation( $product['variation_id'] );
  20.             if ( preg_match( '/^{"id":0,".*/', $variable_product ) ) {
  21.             $product_data = new WC_Product( $product['product_id'] );
  22.             }
  23.             else {
  24.                 $product_data = $variable_product;
  25.             }
  26.  
  27.             $description = get_post_meta( $product_data->get_id(), 'customs_description', true);
  28.            
  29.             if (empty($description)) {
  30.                 $description = $product_data->get_attribute('customs_description');
  31.             }
  32.  
  33.             if (empty($description)) {
  34.                 $terms1 = wc_get_product_terms( $product_id, 'product_brand', array('orderby' => 'term_id', 'order' => 'ASC', 'fields' => 'names') );
  35.                 $terms2 = wc_get_product_terms( $product_id, 'pa_brands', array('orderby' => 'term_id', 'order' => 'ASC', 'fields' => 'names')  );
  36.                 $terms3 = wc_get_product_terms( $product_id, 'pa_colors', array('orderby' => 'term_id', 'order' => 'ASC', 'fields' => 'names')  );
  37.                 $terms4 = wc_get_product_terms( $product_id, 'product_cat', array('orderby' => 'term_id', 'order' => 'ASC', 'fields' => 'names')  );
  38.                 $terms5 = wc_get_product_terms( $product_id, 'product_tag', array('orderby' => 'term_id', 'order' => 'ASC', 'fields' => 'names')  );
  39.                 $terms = array_merge($terms1, $terms2, $terms3, $terms4, $terms5);
  40.                 $description = implode(", ", $terms);
  41.             }
  42.                        
  43.         if (empty($description)) {
  44.             $regular_product_description = $product_instance->get_description();
  45.                 $short_product_description = $product_instance->get_short_description();
  46.                 $description = ($regular_product_description == '') ? $short_product_description : $regular_product_description;
  47.                 $description = ($description == '') ? $product['name'] : $description;
  48.             }                                      
  49.  
  50.       $description = preg_replace('/[^A-ZÅÄÖa-zåäö0-9\-\,\ ]/', '', $description );
  51.  
  52.             $hscode = get_post_meta( $product_data->get_id(), 'hscode', true);
  53.             if (empty($hscode)) {
  54.                 $hscode = get_post_meta( $product_data->get_id(), 'taric', true);
  55.             }
  56.             if (empty($hscode)) {
  57.                 $hscode = $product_data->get_attribute('hscode');
  58.                 }
  59.             if (empty($hscode)) {
  60.                 $hscode = $product_data->get_attribute('taric');
  61.                 }
  62.                
  63.             $country = get_post_meta( $product_data->get_id(), 'country', true);
  64.             if (empty($country)) {
  65.                 $country = get_post_meta( $product_data->get_id(), 'Country', true);
  66.                 }
  67.             if (empty($country)) {
  68.                 $country = $product_data->get_attribute('country');
  69.                 }
  70.             if (empty($country)) {
  71.                 $country = $product_data->get_attribute('Country');
  72.                 }
  73.  
  74.         $fraktjakt_shipping_method_settings = get_option( 'woocommerce_fraktjakt_shipping_method_settings' );
  75.  
  76.             $fraktjakt_vikt=wc_get_weight( $product_data->get_weight(), 'kg', get_option( 'woocommerce_weight_unit' ) );
  77.         $dimension_unit = get_option( 'woocommerce_dimension_unit' );
  78.  
  79.  
  80.       $xml_sub.= '    <commodity>' . "\r\n";
  81.       $xml_sub.= '      <name>'. str_replace(array("\r","\n"), "", strip_tags($product_data->get_name()) ) .'</name>' . "\r\n";
  82.       $xml_sub.= '      <quantity>'. $product['quantity'] .'</quantity>' . "\r\n";
  83.       $xml_sub.= '      <taric>'. $hscode .'</taric>' . "\r\n";
  84.       $xml_sub.= '      <country_of_manufacture>'. $country .'</country_of_manufacture>' . "\r\n";
  85.       $xml_sub.= '      <quantity_units>EA</quantity_units>' . "\r\n";
  86.       $xml_sub.= '      <description>'. substr(strip_tags($description),0,80) .'</description>' . "\r\n";
  87.       $xml_sub.= '      <article_number>'. $product_data->get_sku() .'</article_number>' . "\r\n";
  88.       $xml_sub.= '      <unit_price>'.  wc_get_price_to_display($product_data) .'</unit_price>' . "\r\n";
  89.           $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";
  90.  
  91.       $xml_sub.= '      <weight>'. ($fraktjakt_vikt * $product['quantity']) .'</weight>' . "\r\n";
  92.       $length = $product_data->get_length() ? wc_get_dimension( $product_data->get_length(), 'cm', $dimension_unit ) : '';
  93.             $xml_sub.= '      <length>'.$length.'</length>'."\r\n";
  94.       $width = $product_data->get_width() ? wc_get_dimension( $product_data->get_width(), 'cm', $dimension_unit ) : '';
  95.             $xml_sub.= '      <width>'.$width.'</width>'."\r\n";
  96.       $height = $product_data->get_height() ? wc_get_dimension( $product_data->get_height(), 'cm', $dimension_unit ) : '';
  97.             $xml_sub.= '      <height>'.$height.'</height>'."\r\n";
  98.  
  99.       $xml_sub.= '    </commodity>' . "\r\n";
  100.      
  101.       do_action( 'woocommerce/cart_loop/end', $product );          
  102.     }
  103.         if ($xml_sub=="  <commodities>\r\n") {
  104.             return "";
  105.             }
  106.     $xml_sub.= '  </commodities>' . "\r\n";
  107.         return $xml_sub;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement