Advertisement
Guest User

Untitled

a guest
Mar 16th, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.26 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Product Purchase Button
  4.  *
  5.  * Display the "Add to cart" button for the current product
  6.  */
  7. if( ! defined( 'ABSPATH' ) ) {  exit;  }    // Exit if accessed directly
  8.  
  9.  
  10. if( ! class_exists( 'woocommerce' ) )
  11. {
  12.     add_shortcode( 'av_product_button', 'avia_please_install_woo' );
  13.     return;
  14. }
  15.  
  16. if( ! class_exists( 'avia_sc_produc_button' ) )
  17. {
  18.     class avia_sc_produc_button extends aviaShortcodeTemplate
  19.     {
  20.         /**
  21.          * Create the config array for the shortcode button
  22.          */
  23.         function shortcode_insert_button()
  24.         {
  25.             $this->config['self_closing']   = 'yes';
  26.  
  27.             $this->config['name']           = __( 'Product Purchase Button', 'avia_framework' );
  28.             $this->config['tab']            = __( 'Plugin Additions', 'avia_framework' );
  29.             $this->config['icon']           = AviaBuilder::$path['imagesURL'] . 'sc-button.png';
  30.             $this->config['order']          = 20;
  31.             $this->config['target']         = 'avia-target-insert';
  32.             $this->config['shortcode']      = 'av_product_button';
  33.             $this->config['tooltip']        = __( 'Display the "Add to cart" button for the current product', 'avia_framework' );
  34.             $this->config['drag-level']     = 3;
  35.             $this->config['tinyMCE']        = array( 'disable' => 'true' );
  36.             $this->config['posttype']       = array( 'product', __( 'This element can only be used on single product pages', 'avia_framework' ) );
  37.         }
  38.  
  39.  
  40.         /**
  41.          * Editor Element - this function defines the visual appearance of an element on the AviaBuilder Canvas
  42.          * Most common usage is to define some markup in the $params['innerHtml'] which is then inserted into the drag and drop container
  43.          * Less often used: $params['data'] to add data attributes, $params['class'] to modify the className
  44.          *
  45.          *
  46.          * @param array $params this array holds the default values for $content and $args.
  47.          * @return $params the return array usually holds an innerHtml key that holds item specific markup.
  48.          */
  49.         function editor_element( $params )
  50.         {
  51.             $params = parent::editor_element( $params );
  52.  
  53.             $params['innerHtml'] .= "<div class='avia-flex-element'>";
  54.             $params['innerHtml'] .=         __( 'Display the &quot;Add to cart&quot; button including prices and variations but no product description.', 'avia_framework' );
  55.             $params['innerHtml'] .= '</div>';
  56.  
  57.             return $params;
  58.         }
  59.  
  60.         /**
  61.          * Create custom stylings
  62.          *
  63.          * @since 4.8.9
  64.          * @param array $args
  65.          * @return array
  66.          */
  67.         protected function get_element_styles( array $args )
  68.         {
  69.             $result = parent::get_element_styles( $args );
  70.  
  71.             extract( $result );
  72.  
  73.  
  74.  
  75.             $classes = array(
  76.                         'av-woo-purchase-button',
  77.                         $element_id
  78.                     );
  79.  
  80.             $element_styling->add_classes( 'container', $classes );
  81.             $element_styling->add_classes_from_array( 'container', $meta, 'el_class' );
  82.  
  83.             $selectors = array(
  84.                         'container'     => ".av-woo-purchase-button.{$element_id}"
  85.                     );
  86.  
  87.             /**
  88.              * Fix for plugins (not a clean solution but easier to maintain). Could also be placed in shortcode.css.
  89.              */
  90.             if( class_exists( 'Woocommerce_German_Market' ) )
  91.             {
  92.                 /**
  93.                  * German Market also outputs the price
  94.                  */
  95.                 $selectors['price'] = "#top .av-woo-purchase-button.{$element_id} > div > p.price";
  96.  
  97.                 $element_styling->add_styles( 'price', array( 'display' => 'none' ) );
  98.             }
  99.             else if( class_exists( 'WooCommerce_Germanized' ) )
  100.             {
  101.                 /**
  102.                  * Hides variation price with js
  103.                  */
  104.                 $selectors['price'] = "#top form.variations_form.cart .woocommerce-variation-price > .price";
  105.  
  106.                 $element_styling->add_styles( 'price', array( 'display' => 'block !important;' ) );
  107.             }
  108.  
  109.  
  110.             $element_styling->add_selectors( $selectors );
  111.  
  112.  
  113.             $result['default'] = $default;
  114.             $result['atts'] = $atts;
  115.             $result['content'] = $content;
  116.             $result['meta'] = $meta;
  117.  
  118.             return $result;
  119.         }
  120.  
  121.         /**
  122.          * Frontend Shortcode Handler
  123.          *
  124.          * @param array $atts array of attributes
  125.          * @param string $content text within enclosing form of shortcode element
  126.          * @param string $shortcodename the shortcode found, when == callback name
  127.          * @return string $output returns the modified html string
  128.          */
  129.         function shortcode_handler( $atts, $content = '', $shortcodename = '', $meta = '' )
  130.         {
  131.             // fix for seo plugins which execute the do_shortcode() function before everything is loaded
  132.             global $product;
  133.            
  134.             if( ! function_exists( 'WC' ) || ! WC() instanceof WooCommerce || ! is_object( WC()->query ) || ! $product instanceof WC_Product )
  135.             {
  136.                 return '';
  137.             }
  138.  
  139.             $result = $this->get_element_styles( compact( array( 'atts', 'content', 'shortcodename', 'meta' ) ) );
  140.  
  141.             extract( $result );
  142.  
  143.             /**
  144.              * @since WC 3.0
  145.              */
  146.             $wc_structured_data = isset( WC()->structured_data ) ? WC()->structured_data : null;
  147.  
  148.             /**
  149.              *  Remove default WC actions
  150.              */
  151.             remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
  152.             remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
  153.             remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
  154.             remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
  155.             remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
  156.  
  157.             // produces an endless loop because $wc_structured_data uses 'content' filter and do_shortcode !!
  158.             if( ! is_null( $wc_structured_data ) )
  159.             {
  160.                 remove_action( 'woocommerce_single_product_summary', array( $wc_structured_data, 'generate_product_data' ), 60 );
  161.             }
  162.  
  163.             // $product = wc_get_product();
  164.  
  165.             $style_tag = $element_styling->get_style_tag( $element_id );
  166.             $container_class = $element_styling->get_class_string( 'container' );
  167.  
  168.             $output  = '';
  169.             $output .= $style_tag;
  170.             $output .= "<div class='{$container_class}'>";
  171.            
  172.             if ( $product->is_type( 'variable' ) ) {
  173.                 wp_enqueue_script( 'wc-add-to-cart-variation' );
  174.  
  175.                 add_action( 'woocommerce_after_variations_form', 'woocommerce_template_single_meta', 10 );
  176.  
  177.                 ob_start();
  178.  
  179.                 $output .= woocommerce_variable_add_to_cart();     
  180.             } else {
  181.                 // fix a problem with SEO plugin
  182.                 if( function_exists( 'wc_clear_notices' ) )
  183.                 {
  184.                     wc_clear_notices();
  185.                 }
  186.  
  187.                 ob_start();
  188.  
  189.                 $output .=      '<p class="price">' . $product->get_price_html() . '</p>';
  190.                 /**
  191.                  * hooked by: add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
  192.                  */
  193.                 do_action( 'woocommerce_single_product_summary' );
  194.             }
  195.  
  196.             $output .=      ob_get_clean();
  197.             $output .= '</div>';
  198.  
  199.             add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
  200.             add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
  201.             add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
  202.             add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
  203.             add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
  204.             add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
  205.  
  206.             if( ! is_null( $wc_structured_data ) )
  207.             {
  208.                 add_action( 'woocommerce_single_product_summary', array( $wc_structured_data, 'generate_product_data' ), 60 );
  209.             }
  210.  
  211.             return $output;
  212.         }
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement