Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1.  
  2. // compare button
  3. if( class_exists( 'YITH_Woocompare_Frontend' ) ) {
  4. class ComBtn extends YITH_Woocompare_Frontend{
  5.  
  6. public function enqueue_scripts() {
  7.  
  8. // scripts
  9. wp_enqueue_script( 'yith-woocompare-main', YITH_WOOCOMPARE_ASSETS_URL . '/js/woocompare.js', array('jquery'), $this->version, true );
  10. wp_localize_script( 'yith-woocompare-main', 'yith_woocompare', array(
  11. 'ajaxurl' => WC_AJAX::get_endpoint( "%%endpoint%%" ),
  12. 'actionadd' => $this->action_add,
  13. 'actionremove' => $this->action_remove,
  14. 'actionview' => $this->action_view,
  15. 'added_label' => '',
  16. 'table_title' => __( 'Product Comparison', 'yith-woocommerce-compare' ),
  17. 'auto_open' => get_option( 'yith_woocompare_auto_open', 'yes' ),
  18. 'loader' => YITH_WOOCOMPARE_ASSETS_URL . '/images/loader.gif',
  19. 'button_text' => get_option('yith_woocompare_button_text')
  20. ));
  21.  
  22. // colorbox
  23. wp_enqueue_style( 'jquery-colorbox', YITH_WOOCOMPARE_ASSETS_URL . '/css/colorbox.css' );
  24. wp_enqueue_script( 'jquery-colorbox', YITH_WOOCOMPARE_ASSETS_URL . '/js/jquery.colorbox-min.js', array('jquery'), '1.4.21', true );
  25.  
  26. // widget
  27. if ( is_active_widget( false, false, 'yith-woocompare-widget', true ) && ! is_admin() ) {
  28. wp_enqueue_style( 'yith-woocompare-widget', YITH_WOOCOMPARE_ASSETS_URL . '/css/widget.css' );
  29. }
  30. }
  31.  
  32.  
  33. /**
  34. * Add the link to compare
  35. */
  36. public function add_compare_link( $product_id = false, $args = array() ) {
  37. extract( $args );
  38.  
  39. if ( ! $product_id ) {
  40. global $product;
  41. $product_id = isset( $product->id ) ? $product->id : 0;
  42. }
  43.  
  44. // return if product doesn't exist
  45. if ( empty( $product_id ) || apply_filters( 'yith_woocompare_remove_compare_link_by_cat', false, $product_id ) )
  46. return;
  47.  
  48. $is_button = ! isset( $button_or_link ) || ! $button_or_link ? get_option( 'yith_woocompare_is_button' ) : $button_or_link;
  49.  
  50. if ( ! isset( $button_text ) || $button_text == 'default' ) {
  51. $button_text = get_option( 'yith_woocompare_button_text', __( 'Compare', 'yith-woocommerce-compare' ) );
  52. yit_wpml_register_string( 'Plugins', 'plugin_yit_compare_button_text', $button_text );
  53. $button_text = yit_wpml_string_translate( 'Plugins', 'plugin_yit_compare_button_text', $button_text );
  54. $button_text = '<i class="fa fa-refresh"></i>';
  55. }
  56.  
  57. printf( '<a href="%s" class="%s" data-product_id="%d" rel="nofollow">%s</a>', $this->add_product_url( $product_id ), 'compare scom' . ( $is_button == 'button' ? ' button' : '' ), $product_id, $button_text );
  58. }
  59.  
  60. /**
  61. * Show the html for the shortcode
  62. */
  63. public function compare_button_sc( $atts, $content = null ) {
  64. $atts = shortcode_atts(array(
  65. 'product' => false,
  66. 'type' => 'default',
  67. 'container' => 'yes'
  68. ), $atts);
  69.  
  70. $product_id = 0;
  71.  
  72. /**
  73. * Retrieve the product ID in these steps:
  74. * - If "product" attribute is not set, get the product ID of current product loop
  75. * - If "product" contains ID, post slug or post title
  76. */
  77. if ( ! $atts['product'] ) {
  78. global $product;
  79. $product_id = isset( $product->id ) ? $product->id : 0;
  80. } else {
  81. global $wpdb;
  82. $product = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d OR post_name = %s OR post_title = %s LIMIT 1", $atts['product'], $atts['product'], $atts['product'] ) );
  83. if ( ! empty( $product ) ) {
  84. $product_id = $product->ID;
  85. }
  86. }
  87.  
  88. // if product ID is 0, maybe the product doesn't exists or is wrong.. in this case, doesn't show the button
  89. if ( empty( $product_id ) ) return;
  90.  
  91. ob_start();
  92. if ( $atts['container'] == 'yes' ) echo '<div class="woocommerce product compare-button">';
  93. $this->add_compare_link( $product_id, array(
  94. 'button_or_link' => ( $atts['type'] == 'default' ? false : $atts['type'] ),
  95. 'button_text' => empty( $content ) ? 'default' : $content
  96. ) );
  97. if ( $atts['container'] == 'yes' ) echo '</div>';
  98. return ob_get_clean();
  99. }
  100.  
  101. }
  102. new ComBtn();
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement