AEMexportersimple

The Woocommerce CSV Import AEM exporter simple class.

Defined (1)

The class is defined in the following location(s).

/export/products/simple.php  
  1. class simple implements productsInterface 
  2.  
  3. public $id; 
  4.  
  5. public $content = array (); 
  6.  
  7. public $fields = array (); 
  8.  
  9. public $writer; 
  10.  
  11. public function __construct ($id = NULL, $fields, writerInterface $writer) 
  12. $this->id = $id; 
  13. $this->fields = $fields; 
  14. $this->writer = $writer; 
  15.  
  16. $this->buildContent( $this->fields ); 
  17. $this->setProductType(); 
  18. $this->fillProduct(); 
  19.  
  20.  
  21. public function setProductType () 
  22. $this->content[ 'product_type' ] = 'simple'; 
  23.  
  24. public function buildContent ($fields) 
  25. foreach ($fields as $field) { 
  26. $this->content[ $field ] = ''; 
  27.  
  28. public function fillProduct () 
  29. $this->body(); 
  30. $this->meta(); 
  31. $this->categories(); 
  32. $this->tags(); 
  33. $this->shippingClass(); 
  34. $this->images(); 
  35. $this->attributes(); 
  36. $this->defaultAttributes(); 
  37. $this->setPostparentSku(); 
  38.  
  39.  
  40.  
  41.  
  42. public function body () 
  43. global $wpdb; 
  44. $body = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $this->id ), ARRAY_A ); 
  45.  
  46. foreach ($body as $key => $value) { 
  47. if (array_key_exists( $key, $this->content )) { 
  48. $this->content[ $key ] = $value; 
  49. unset ($body); 
  50.  
  51.  
  52. public function meta () 
  53. global $wpdb; 
  54.  
  55. $allmeta = $wpdb->get_results( "select meta_key, meta_value from $wpdb->postmeta where post_id= $this->id" ); 
  56.  
  57. foreach ($allmeta as $meta) { 
  58. //if key in field lists add it to row 
  59. if (array_key_exists( $meta->meta_key, $this->content )) { 
  60. $this->content[ $meta->meta_key ] = $meta->meta_value; 
  61.  
  62. public function categories () 
  63.  
  64. $cats = array (); 
  65.  
  66. $categories = wp_get_object_terms( $this->id, 'product_cat' ); 
  67. foreach ($categories as $category) { 
  68. $cats[] = $this->getTermName( $category->term_id ); 
  69.  
  70. $this->content[ 'categories' ] = implode( '|', $cats ); 
  71.  
  72.  
  73. public function getTermName ($term_id, $taxonomy = 'product_cat') 
  74. $term = get_term( $term_id, $taxonomy ); 
  75.  
  76. if (is_wp_error( $term )) { 
  77. return FALSE; 
  78.  
  79. if ($term->parent) { 
  80. return $this->getTermName( $term->parent ) . '->' . $term->name; 
  81.  
  82. return $term->name; 
  83.  
  84. public function shippingClass () 
  85. $shippingClass = get_the_terms( $this->id, 'product_shipping_class' ); 
  86. if ($shippingClass && !is_wp_error( $shippingClass )) { 
  87. $this->content[ '_shipping_class' ] = current( $shippingClass )->slug; 
  88.  
  89.  
  90. public function tags () 
  91. $tags = wp_get_object_terms( $this->id, 'product_tag', array ( 'fields' => 'names' ) ); 
  92.  
  93. if (is_wp_error( $tags )) { 
  94. return FALSE; 
  95.  
  96. $this->content[ 'tags' ] = implode( '|', $tags ); 
  97.  
  98. return true; 
  99.  
  100. public function images () 
  101.  
  102. //featured_image 
  103. $featured_image = get_post_meta( $this->id, '_thumbnail_id', TRUE ); 
  104. $this->content[ 'featured_image_name' ] = basename( wp_get_attachment_url( $featured_image ) ); 
  105. $this->content[ 'featured_image' ] = wp_get_attachment_url( $featured_image ); 
  106.  
  107. //product_gallery 
  108. $product_gallery = array (); 
  109. $allimages = get_post_meta( $this->id, '_product_image_gallery', TRUE ); 
  110. $images = explode( ', ', $allimages ); 
  111. foreach ($images as $image) { 
  112. $product_gallery[] = wp_get_attachment_url( $image ); 
  113. $product_gallery_name[] = basename( wp_get_attachment_url( $image ) ); 
  114.  
  115. $this->content[ 'product_gallery' ] = implode( '|', $product_gallery ); 
  116. $this->content[ 'product_gallery_name' ] = implode( '|', $product_gallery_name ); 
  117.  
  118.  
  119. public function attributes () 
  120. $atts = array (); 
  121. $vars = array (); 
  122.  
  123. $attributes = get_post_meta( $this->id, '_product_attributes', TRUE ); 
  124.  
  125. if (!$attributes) { 
  126. return; 
  127.  
  128. foreach ($attributes as $attribute) { 
  129.  
  130. if ($attribute[ 'is_taxonomy' ] == 0) { 
  131. continue; 
  132.  
  133. $att = ltrim( $attribute[ 'name' ], 'pa_' ) . '->' . $attribute[ 'is_visible' ]; 
  134.  
  135. if ($attribute[ 'is_variation' ] == 1) { 
  136. $vars[] = $att; 
  137. } else { 
  138. $atts[] = $att; 
  139.  
  140. $this->fillAttributesValues( $attribute[ 'name' ] ); 
  141.  
  142.  
  143.  
  144. if ($atts) { 
  145. $this->content[ 'attributes' ] = implode( '|', $atts ); 
  146.  
  147. if ($vars) { 
  148. $this->content[ 'variations' ] = implode( '|', $vars ); 
  149.  
  150. public function fillAttributesValues ($taxonomy) 
  151. $cats = array (); 
  152. $categories = wp_get_object_terms( $this->id, $taxonomy ); 
  153. foreach ($categories as $category) { 
  154. $cats[] = $category->name; 
  155.  
  156. $this->content[ $taxonomy ] = implode( '|', $cats ); 
  157.  
  158.  
  159.  
  160. public function getContent () 
  161. return $this->content; 
  162.  
  163.  
  164. public function save () 
  165. $this->writer->write( $this->getContent() ); 
  166.  
  167.  
  168. public function defaultAttributes () 
  169. $atts = array (); 
  170. $attributes = get_post_meta( $this->id, '_default_attributes', TRUE ); 
  171.  
  172. if (!$attributes) { 
  173. return false; 
  174.  
  175. foreach ($attributes as $key => $value) { 
  176. $atts[] = substr( $key, 3 ) . '->' . $value; 
  177.  
  178. $this->content[ '_default_attributes' ] = implode( $atts, '|' ); 
  179.  
  180. return true; 
  181.  
  182. private function setPostparentSku() 
  183. $parent_id = $this->content[ 'post_parent' ]; 
  184. if (empty($parent_id)) { 
  185. return; 
  186.  
  187. $parent_sku = get_post_meta($parent_id, '_sku', true); 
  188. if ($parent_sku) { 
  189. $this->content[ 'post_parent' ] = $parent_sku; 
  190.  
  191. return; 
  192.  
  193. $this->content[ 'post_parent' ] = ''; 
  194.  
  195. return; 
  196.  
  197.  
  198.