我正在寻找一种将自定义条件添加到 elementor 的显示条件的方法,尤其是按产品类型。 到目前为止,这就是我的代码:
<?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() );
}
结果已经很接近了,但是所有子条件列表都显示在同一个主条件列表中。
好的,我想我已经明白了。有点困难,但这是我编码的方式。 我尝试的是按产品类型和我创建的自定义类型来调节单个产品模板的显示。
班级代码:
<?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;
}
}
挂钩代码:
<?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);
希望你有一些具体的东西可以帮助你 =)。
可以在repo中看到代码:https://github.com/GUShap/Elementor-Woo-Template-Sub-Condition-Product-Type