Search code examples
phpwordpresswoocommerceelementor

Add Elementor Display Sub Condition of Product Type


im looking for a way to add custom conditions to elementor's display condition, especially by product type. thats my code so far:

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

use ElementorPro\Modules\ThemeBuilder\Conditions\Condition_Base;

class Product_Type_Condition extends Condition_Base {

    public function get_name() {
        return 'product_type_condition';
    }

    public function get_label() {
        return __( 'Product Type', 'text-domain' );
    }

    public function get_type() {
        return 'product';
    }

    public function check( $args ) {
        ...
    }
}

class Product_Type_Sub_Condition extends Condition_Base {

    private $product_type_key;
    private $product_type_label;

    public function __construct( $product_type_key, $product_type_label ) {
        $this->product_type_key = $product_type_key;
        $this->product_type_label = $product_type_label;
    }

    public function get_name() {
        return 'product_type_sub_condition_' . $this->product_type_key;
    }

    public function get_label() {
        return $this->product_type_label;
    }

    public function check( $args ) {
        ...
    }
}

add_action( 'elementor/theme/register_conditions', 'register_custom_product_type_condition' );

function register_custom_product_type_condition( $conditions_manager ) {
    $conditions_manager->register( new Product_Type_Condition() );
}

the result im getting close, but all the sub conditions list display in the same main condition list.


Solution

  • ok, i think i've got it. Was a bit tough but it's i coded my way around. What i was trying is to condition the display of single product template by product type, with custom type i created.

    product type conditionning

    the class code:

    <?php
    
    if (!defined('ABSPATH')) {
        exit; // Exit if accessed directly
    }
    use ElementorPro\Modules\ThemeBuilder\Conditions\Condition_Base;
    class Product_Type_Condition extends Condition_Base
    {
        /**
         * Get the type of condition.
         *
         * @return string
         */
        public static function get_type()
        {
            return 'product';
        }
        /**
         * Get the priority of this condition.
         *
         * @return int
         */
        /**
         * Get the unique name for this condition.
         *
         * @return string
         */
        public function get_name()
        {
            return 'product_type_condition';
        }
        /**
         * Get the label that appears in Elementor's conditions interface.
         *
         * @return string
         */
        public function get_label()
        {
            return __('Product Type', 'text-domain');
        }
        /**
         * Get the label for "All" product types option.
         *
         * @return string
         */
        public function get_all_label()
        {
            return __('All', 'text-domain');
        }
        protected function _register_controls()
        {
            $product_types = wc_get_product_types(); // Get all registered product types
            $product_types = array_merge(['all' => __('All', 'text-domain')], $product_types);
            $this->add_control(
                'product_types',
                args: [
                    'section' => 'settings',
                    'label' => __('Page Template'),
                    'type' => \Elementor\Controls_Manager::SELECT2,
                    'options' => $product_types,
                    'default' => 'all',
                ]
            );
        }
        /**
         * Check if the current product matches the selected product type condition.
         *
         * @param array $args
         * @return bool
         */
        public function check($args)
        {
            if (!is_product()) {
                return false;
            }
            $selected_type = $args['id'];
            if ($selected_type === 'all') {
                return true;
            }
            $product = wc_get_product();
            $product_type = $product->get_type();
            return $product_type === $selected_type;
        }
    }
    

    the hook code:

        <?php
    
    function register_product_type_sub_conditions($conditions_manager)
    {
        require_once PATH_TO_CONDITION_CLASS;
        $conditions_manager->get_condition('product')->register_sub_condition(new \Product_Type_Condition());
    }
    add_action('elementor/theme/register_conditions', 'register_product_type_sub_conditions', 100);
    

    hope you had something so specific it helped =).

    Can see the code in repo: https://github.com/GUShap/Elementor-Woo-Template-Sub-Condition-Product-Type