php - 如何更改 woocommerce 片段以使用变量名称而不是产品表列表的变量段

标签 php wordpress woocommerce

我偶然发现了一个非常酷的 woocommerce 片段,我想通过将其输入到我的 function.php 页面来将其合并到我的网站中。我现在已经完成了,但问题是 woocommerce 现在从变量产品中提取变量 slug 并将其显示在网站的前端。

有没有办法可以更改下面的代码以使用为变量指定的实际名称而不是 slug?

下面是代码

/*product variations list table*/
function find_valid_variations() {
global $product;

$variations = $product->get_available_variations();
$attributes = $product->get_attributes();
$new_variants = array();

// Loop through all variations
foreach( $variations as $variation ) {

    // Peruse the attributes.

    // 1. If both are explicitly set, this is a valid variation
    // 2. If one is not set, that means any, and we must 'create' the rest.

    $valid = true; // so far
    foreach( $attributes as $slug => $args ) {
        if( array_key_exists("attribute_$slug", $variation['attributes']) && !empty($variation['attributes']["attribute_$slug"]) ) {
            // Exists

        } else {
            // Not exists, create
            $valid = false; // it contains 'anys'
            // loop through all options for the 'ANY' attribute, and add each
            foreach( explode( '|', $attributes[$slug]['value']) as $attribute ) {
                $attribute = trim( $attribute );
                $new_variant = $variation;
                $new_variant['attributes']["attribute_$slug"] = $attribute;
                $new_variants[] = $new_variant;
            }

        }
    }

    // This contains ALL set attributes, and is itself a 'valid' variation.
    if( $valid )
        $new_variants[] = $variation;

}

return $new_variants;}

感谢你们分享您的知识。

最佳答案

正如我们所得到的,产品变体slug,我们可以得到它的名称,如下,

引用链接:https://gist.github.com/renegadesk/3967956

请检查第 80 至 91 行的代码。

在这里,我们可以直接提取变体名称。 (第86行)通过,

$woocommerce->attribute_label

关于php - 如何更改 woocommerce 片段以使用变量名称而不是产品表列表的变量段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24802174/

相关文章:

Mac OSX Mavericks 升级后 PHP 和 Mongo 损坏

php - OOP PHP 集成 mysqli_query

javascript - AJAX 内的 AJAX 无法正常工作

wordpress - 如何从 Discord 的预览中删除作者标签?

wordpress - Woocommerce 单一产品税收状况胜过 WooTax 和 Tax Cloud

php - Laravel 中的日期格式查询

javascript - 页面加载时内容消失

php - 为什么 WordPress 中有多个表,为什么它只读取错误/最后一个表?

css - 内容和页脚之间的梯度差距

javascript - 如何在单个产品页面 woocommerce 上显示高级自定义字段 Google map