Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Includes\widgets\WooCommerce\ABCProductAddToCart;
- if (!defined('ABSPATH')) exit; // Exit if accessed directly
- use Includes\Widgets\BaseWidget;
- use Elementor\Controls_Manager;
- use Elementor\Group_Control_Background;
- use Elementor\Group_Control_Typography;
- /**
- * Elementor List Widget.
- * @since 1.0.0
- */
- class Main extends BaseWidget {
- // define protected variables...
- protected $name = 'abcbiz-wc-add-to-cart';
- protected $title = 'ABC Add to Cart';
- protected $icon = 'eicon-product-add-to-cart';
- protected $categories = [
- 'abcbiz-wc-category'
- ];
- protected $keywords = [
- 'abc', 'product', 'add', 'cart'
- ];
- /**
- * Register list widget controls.
- */
- protected function register_controls() {
- //Template
- $this->start_controls_section(
- 'abcbiz_elementor_wc_product_price',
- [
- 'label' => esc_html__( 'Style', 'abcbiz-multi' ),
- 'tab' => Controls_Manager::TAB_CONTENT,
- ]
- );
- //Alignment
- $this->add_responsive_control(
- 'abcbiz_elementor_wc_product_price_align',
- [
- 'label' => esc_html__( 'Alignment', 'abcbiz-multi'),
- 'type' => Controls_Manager::CHOOSE,
- 'default' => 'left',
- 'options' => [
- 'left' => [
- 'title' => esc_html__( 'Left', 'abcbiz-multi' ),
- 'icon' => 'eicon-text-align-left',
- ],
- 'center' => [
- 'title' => esc_html__( 'Center', 'abcbiz-multi' ),
- 'icon' => 'eicon-text-align-center',
- ],
- 'right' => [
- 'title' => esc_html__( 'Right', 'abcbiz-multi' ),
- 'icon' => 'eicon-text-align-right',
- ],
- ],
- 'selectors' => [
- '{{WRAPPER}} .abcbiz-elementor-wc-product-price .price' => 'text-align: {{VALUE}}',
- ],
- ]
- );
- //Price Color regular
- $this->add_control(
- 'abcbiz_elementor_wc_product_price_regular_color',
- [
- 'label' => esc_html__( 'Price Regular Color', 'abcbiz-multi' ),
- 'type' => Controls_Manager::COLOR,
- 'default' => '#999999',
- 'selectors' => [
- '{{WRAPPER}} .abcbiz-elementor-wc-product-price .price del bdi, {{WRAPPER}} .abcbiz-elementor-wc-product-price .price bdi' => 'color: {{VALUE}}',
- ],
- ]
- );
- //Price regular typoghraphy
- $this->add_group_control(
- Group_Control_Typography::get_type(),
- [
- 'name' => 'abcbiz_elementor_wc_product_price_regular_typography',
- 'label' => esc_html__( 'Typography', 'abcbiz-multi' ),
- 'selector' => '{{WRAPPER}} .abcbiz-elementor-wc-product-price .price del bdi, {{WRAPPER}} .abcbiz-elementor-wc-product-price .price bdi',
- ]
- );
- //Price Color sales
- $this->add_control(
- 'abcbiz_elementor_wc_product_price_sales_color',
- [
- 'label' => esc_html__( 'Price Sales Color', 'abcbiz-multi' ),
- 'type' => Controls_Manager::COLOR,
- 'default' => '#333333',
- 'selectors' => [
- '{{WRAPPER}} .abcbiz-elementor-wc-product-price .price ins bdi' => 'color: {{VALUE}}',
- ],
- ]
- );
- //Price sales typoghraphy
- $this->add_group_control(
- Group_Control_Typography::get_type(),
- [
- 'name' => 'abcbiz_elementor_wc_product_price_sales_typography',
- 'label' => esc_html__( 'Typography', 'abcbiz-multi' ),
- 'selector' => '{{WRAPPER}} .abcbiz-elementor-wc-product-price .price ins bdi',
- ]
- );
- $this->end_controls_section();
- }
- public function before_add_to_cart_quantity() {
- ?>
- <div class="abcbiz-wc-qty-button-holder">
- <?php
- }
- /**
- * After Add to Cart Quantity
- *
- * @since 3.6.0
- */
- public function after_add_to_cart_button() {
- ?>
- </div>
- <div id="acbbiz-add-to-cart-message"></div>
- <?php
- }
- public function unescape_html( $safe_text, $text ) {
- return $text;
- }
- private function render_form_button( $product ) {
- if ( ! $product && current_user_can( 'manage_options' ) ) {
- echo esc_html__( 'Please set a valid product', 'abcbiz-multi' );
- return;
- }
- $text_callback = function() {
- ob_start();
- echo esc_html__('Add to cart', 'abcbiz-multi');
- return ob_get_clean();
- };
- add_filter( 'woocommerce_product_single_add_to_cart_text', $text_callback );
- add_filter( 'esc_html', [ $this, 'unescape_html' ], 10, 2 );
- ob_start();
- woocommerce_template_single_add_to_cart();
- $form = ob_get_clean();
- // Add the data-product_id attribute
- $product_id = $product->get_id();
- $replacement = 'single_add_to_cart_button elementor-button abcbiz_add_to_cart button alt" data-product_id="' . esc_attr($product_id) . '"';
- $form = str_replace( 'single_add_to_cart_button', $replacement, $form );
- echo $form; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
- }
- public function get_product( $product_id = false ) {
- if ( 'product_variation' === get_post_type() ) {
- return $this->get_product_variation( $product_id );
- }
- $product = wc_get_product( $product_id );
- if ( ! $product ) {
- $product = wc_get_product();
- }
- return $product;
- }
- public function get_product_variation( $product_id = false ) {
- return wc_get_product( get_the_ID() );
- }
- /**
- * Render the widget output on the frontend.
- */
- protected function render()
- {
- $settings = $this->get_settings_for_display();
- $product_id = get_queried_object_id();
- global $product;
- $product = $this->get_product( $product_id );
- add_action( 'woocommerce_before_add_to_cart_quantity', [ $this, 'before_add_to_cart_quantity' ], 95 );
- add_action( 'woocommerce_after_add_to_cart_button', [ $this, 'after_add_to_cart_button' ], 5 );
- $this->render_form_button( $product );
- //include 'renderview.php';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement