从 WooCommerce 前端的下拉列表中删除特定的变化 [英] Remove particular variation from dropdown in WooCommerce frontend

查看:17
本文介绍了从 WooCommerce 前端的下拉列表中删除特定的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

现在我想从下拉列表中删除/隐藏特定项目,因此我尝试使用下面的代码/钩子,我相信这可能有助于我实现.

add_filter('woocommerce_dropdown_variation_attribute_options_args', 'hide_variations_for_mindesk_users');函数 hide_variations_for_mindesk_users( $args ){打印_r($args);返回 $args;}

现在我的问题是,如何从下拉列表中删除或隐藏特定的变体产品?我需要从变体 ID 中删除还是从某个地方删除?


例如:

在这里,我想从下拉列表中删除/隐藏第二个变体,其变体 ID #4171 具有Monthly-Professional".这也适用于单个属性.

谁能指出我正确的方向来实现这一目标?

解决方案

add-to-cart/variable.php 模板文件,我们找到 foreach ( $attributes as $attribute_name => $options ).然而,目的是隐藏1个属性,所以让我们看看这些是从哪里传递给模板文件的.

includes/wc-template-functionsphp,我们可以看到调用了模板文件,并传入了一个带有一些选项的数组.这些选项之一是 available_variations' =>$get_variations ?$product->get_available_variations()

然后在 includes/class-wc-product-variable.php 依次$variation_ids = $this->get_children(); 包含.>

get_children() 函数可以在 includes/class-wc-product-variable.php,其中包含 apply_filters('woocommerce_get_children', $this->children, $this, false );

该过滤器钩子可用于删除一个或多个 childID(变体)


所以你得到:

function filter_woocommerce_get_children( $children, $product, $false ) {//不是后端if ( is_admin() ) 返回 $children;//变量 ID$variation_id = 4171;//按值删除:在数组中搜索给定的值,如果成功则返回第一个对应的键if ( ( $key = array_search( $variation_id, $children ) ) !== false ) {未设置( $children[$key] );}返回 $children;}add_filter('woocommerce_get_children', 'filter_woocommerce_get_children', 10, 3);


如果要将其应用于多个变体 ID,请使用:

function filter_woocommerce_get_children( $children, $product, $false ) {//不是后端if ( is_admin() ) 返回 $children;//变体ID,可以输入多个ID,用逗号隔开$variation_ids = array( 4171, 36, 38 );//遍历变体 IDforeach ( $variation_ids 作为 $variation_id ) {//按值删除:在数组中搜索给定值,如果成功则返回第一个对应的键if ( ( $key = array_search( $variation_id, $children ) ) !== false ) {未设置( $children[$key] );}}返回 $children;}add_filter('woocommerce_get_children', 'filter_woocommerce_get_children', 10, 3);


在此答案中使用:PHP 数组按值(非键)删除

I am using WooCommerce and WooCommerce Subscriptions and its working as per my expectations.

Now I am creating a variable subscription product having multiple attributes like this.

Now I want to remove/hide particular item from dropdown hence I am trying to use below code / hook which I believe might help me to achieve.

add_filter('woocommerce_dropdown_variation_attribute_options_args', 'hide_variations_for_mindesk_users');

function hide_variations_for_mindesk_users( $args ){        
    
   print_r($args);

    return $args;    
}

Now my question is, how can I remove or hide particular variation product from dropdown? Do I need to remove from variation id or from somewhere?


For example:

Here I want to remove/hide 2nd variation from dropdown which has variation id #4171 having "Monthly- Professional". This should work with single attribute as well.

Can anyone point me in the right direction to achieve this?

解决方案

In add-to-cart/variable.php template file, we find foreach ( $attributes as $attribute_name => $options ). However, the intention is to hide 1 attribute, so let's see where these are passed to the template file.

In includes/wc-template-functions.php, we can see that the template file is called and an array is passed with some options. One of these options is available_variations' => $get_variations ? $product->get_available_variations()

The get_available_variations() function is then found in includes/class-wc-product-variable.php which in turn $variation_ids = $this->get_children(); contains.

The get_children() function can then be found in includes/class-wc-product-variable.php, which contains apply_filters( 'woocommerce_get_children', $this->children, $this, false );

And that filter hook can be used to remove one or more childIDs (variantions)


So you get:

function filter_woocommerce_get_children( $children, $product, $false ) {
    // NOT backend
    if ( is_admin() ) return $children;

    // Variation ID
    $variation_id = 4171;
    
    // Delete by value: Searches the array for a given value and returns the first corresponding key if successful
    if ( ( $key = array_search( $variation_id, $children ) ) !== false ) {
        unset( $children[$key] );
    }

    return $children;
}
add_filter( 'woocommerce_get_children', 'filter_woocommerce_get_children', 10, 3 );


If you want to apply it for multiple variantion IDs, use:

function filter_woocommerce_get_children( $children, $product, $false ) {
    // NOT backend
    if ( is_admin() ) return $children;

    // Variation IDs, multiple IDs can be entered, separated by a comma
    $variation_ids = array( 4171, 36, 38 );
    
    // Loop through variation IDs
    foreach ( $variation_ids as $variation_id ) {
        // Delete by value: Searches the array for a given value and returns the first corresponding key if successful
        if ( ( $key = array_search( $variation_id, $children ) ) !== false ) {
            unset( $children[$key] );
        }
    }

    return $children;
}
add_filter( 'woocommerce_get_children', 'filter_woocommerce_get_children', 10, 3 );


Used in this answer: PHP array delete by value (not key)

这篇关于从 WooCommerce 前端的下拉列表中删除特定的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆