WC_Naguro_Cart

The WooCommerce Naguro Connect WC Naguro Cart class.

Defined (1)

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

/includes/woocommerce/core/class-wc-naguro-cart.php  
  1. class WC_Naguro_Cart { 
  2. public function __construct() { 
  3. add_filter( 'woocommerce_loop_add_to_cart_link', array($this, 'change_add_to_cart_url'), 10, 2); 
  4. add_filter( 'woocommerce_loop_add_to_cart_link', array($this, 'change_add_to_cart_button'), 10, 2); 
  5. add_filter( 'woocommerce_product_add_to_cart_text', array($this, 'add_to_cart_text' ), 10, 2 ); 
  6. add_filter( 'woocommerce_product_single_add_to_cart_text', array($this, 'add_to_cart_text'), 10, 2 ); 
  7. add_filter( 'template_include', array($this, 'designer_template_filter' ), 10, 1 ); 
  8. add_filter( 'woocommerce_is_purchasable', array( $this, 'is_purchasable' ), 10, 2 ); 
  9. add_action( 'woocommerce_simple_add_to_cart', array( $this, 'simple_add_to_cart' ) ); 
  10.  
  11. add_action( 'the_content', array($this, 'output_designer' ) ); 
  12.  
  13. // Meta information handlers 
  14. add_filter( 'woocommerce_get_cart_item_from_session', array( $this, 'get_cart_item_from_session'), 10, 2 ); 
  15. add_filter( 'woocommerce_get_item_data', array( $this, 'get_item_data' ), 10, 2 ); 
  16. add_action( 'woocommerce_add_order_item_meta', array( $this, 'add_order_item_meta' ), 10, 2 ); 
  17.  
  18. public function is_purchasable( $purchasable, $product ) { 
  19. if ( is_single() ) { 
  20. if ( $this->is_naguro_product( $product ) ) { 
  21. $purchasable = false; 
  22.  
  23. return $purchasable; 
  24.  
  25. public function simple_add_to_cart() { 
  26. global $product; 
  27.  
  28. if ( $this->is_naguro_product( $product ) ) { 
  29. echo '<a href="'. $this->append_designer_arg_to_url( $product->get_permalink() ) .'" rel="nofollow" class="button product_type_simple">Design product</a>'; 
  30.  
  31. public function add_order_item_meta( $item_id, $values ) { 
  32. if ( isset( $values['naguro_session'] ) ) { 
  33. wc_add_order_item_meta( $item_id, 'naguro_session', $values['naguro_session']['id'] ); 
  34. wc_add_order_item_meta( $item_id, 'naguro_session_object', $values['naguro_session'] ); 
  35.  
  36. public function get_cart_item_from_session( $cart_item, $values ) { 
  37. if ( isset( $values['naguro_session'] ) ) { 
  38. $cart_item['naguro_session'] = $values['naguro_session']; 
  39.  
  40. return $cart_item; 
  41.  
  42. public function get_item_data( $other_data, $cart_item ) { 
  43. if ( isset( $cart_item['naguro_session'] ) ) { 
  44. $other_data['naguro_session'] = array( 
  45. 'display' => $cart_item['naguro_session']['id'],  
  46. 'value' => $cart_item['naguro_session']['id'],  
  47. 'name' => 'Naguro session',  
  48. ); 
  49.  
  50. return $other_data; 
  51.  
  52. public function output_designer( $content ) { 
  53. if ( isset( $_GET['designer'] ) ) { 
  54. global $post; 
  55.  
  56. if ( isset($post->ID)) { 
  57. $product = wc_get_product($post->ID); 
  58. if ( $this->is_naguro_product($product)) { 
  59. $designer = new WC_Naguro_Designer( $product ); 
  60. $designer->output(); 
  61. } else { 
  62. return $content; 
  63.  
  64. /** 
  65. * @param $product WC_Product 
  66. * @return bool 
  67. */ 
  68. private function is_naguro_product( $product ) { 
  69. return ( 'yes' == get_post_meta( $product->id, 'naguro_product_active', true ) ); 
  70.  
  71. public function change_add_to_cart_url( $button, $product ) { 
  72. if ( $this->is_naguro_product( $product ) ) { 
  73. preg_match( '/<a[^>]*href="([^"]*)"[^>]*>.*<\/a>/', $button, $matches ); 
  74. $button = str_replace( $matches[1], $this->append_designer_arg_to_url( $product->get_permalink() ), $button ); 
  75.  
  76. return $button; 
  77.  
  78. private function append_designer_arg_to_url( $url ) { 
  79. return add_query_arg( 'designer', '', $url ); 
  80.  
  81. /** 
  82. * @param $button string 
  83. * @param $product WC_Product 
  84. * @return string 
  85. */ 
  86. public function change_add_to_cart_button( $button, $product ) { 
  87. if ( $this->is_naguro_product( $product ) ) { 
  88. $button = str_replace( 'add_to_cart_button', '', $button ); 
  89.  
  90. return $button; 
  91.  
  92. /** 
  93. * @param $text string 
  94. * @param $product WC_Product 
  95. * @return string 
  96. */ 
  97. public function add_to_cart_text( $text, $product ) { 
  98. if ( $this->is_naguro_product( $product ) ) { 
  99. return __( 'Design product', 'woocommerce-naguro-connect' ); 
  100.  
  101. return $text; 
  102.  
  103. public function designer_template_filter( $template_file) { 
  104. if ( ! isset( $_GET['designer'] ) ) { 
  105. return $template_file; 
  106.  
  107. if ( strstr( $template_file, 'single-product.php' ) ) { 
  108. if ( file_exists( trailingslashit( get_stylesheet_directory() ) . 'naguro-editor.php' ) ) { 
  109. return trailingslashit( get_stylesheet_directory() ) . 'naguro-editor.php'; 
  110. } else { 
  111. return apply_filters( 'naguro_editor_template_file', trailingslashit( get_stylesheet_directory() ) . 'page.php' ); 
  112.  
  113. return $template_file;