当前位置: 首页 > 知识库问答 >
问题:

在WooCommerce单品上动态设置和显示自定义产品变化值

裴和怡
2023-03-14

我有一个商业珠宝网站。在单变量产品页面上,我添加了一些变体,并添加了一个文本框(文本框供客户填写一些将打印在珠宝上的内容)。

因此,我希望页面的工作方式如下:如果客户选择一个特定的产品变体并开始在文本框中键入,则产品的价格应根据字母数变化(注意:仅当选择了一个特定变体时,此功能才起作用),其他每种产品的价格将是固定的。

我确实为文本框价格更新做了部分工作。

我在这里遇到的问题是如何选择特定的产品变体。

基于显示WooCommerce答案中所选变体的产品属性术语,我试图用以下代码尝试解决这个问题:

function vendor_defined_taxonomy() {
    return 'Materijal'; 
}

add_action( 'woocommerce_product_meta_end', 'display_product_vendors', 10 );
function display_product_vendors() {
    $taxonomy = vendor_defined_taxonomy();
    $term_ids = wp_get_post_terms( get_the_ID(), $taxonomy, array('fields' => 
    'ids') );
    if( sizeof($term_ids) > 0 ){ 
        echo '<span class="posted_in vendors"></span>';
    }
}

add_filter( 'woocommerce_available_variation', 'selected_variation_vendor_value', 10, 3 );
function selected_variation_vendor_value( $data, $product, $variation ) {
    $taxonomy = vendor_defined_taxonomy();

    if( isset($data['attributes']['attribute_'.$taxonomy]) )
        $term = get_term_by( 'slug', $data['attributes']['attribute_'.$taxonomy], $taxonomy );

    if( isset($term) && is_a($term, 'WP_Term' ) )
        $data['variation_description'] .= '<input type="hidden" name="vendor- hidden" id="vendor-hidden" value="'.$term->name.'">';

    return $data;
}

add_action('woocommerce_before_add_to_cart_button', 'custom_product_jquery_script');
function custom_product_jquery_script() {
    global $product;

    $taxonomy     = vendor_defined_taxonomy();
    $terms_string = $product->get_attribute($taxonomy);

    if( ! empty($terms_string) ) :
    ?>
    <script type="text/html" target="_blank">javascript">
    jQuery(function($) {
        var form = 'form.variations_form',       
            selected = 'input[name="variation_id"]',
            vendorVal = 'input#vendor-hidden',
            vendorTarget = 'span.vendors',
            vendorHtml = $(vendorTarget).text(), 
            vendorLabel = '';

        // On variation select
        $(form).on( 'blur', 'select', function() {
            if($(selected).val() != ''){
                $(vendorTarget).text("");
                if($(vendorVal).val() == 'Zlato'){
                    //$(vendorTarget).text(vendorLabel+' 
                    '+$(vendorVal).val());
                    $(vendorTarget).text("here is your text");
                }
            } 
        });
    });
    </script>
    <?php
    endif;
}

但我无法在ProductMeta部分下打印“Matrijal”产品属性selected name值。

产品属性名称是“Matrijal”(和slug“mats”)……例如,术语名称是“Zlato”(和slug“Zlato”)。

有什么帮助吗?

共有1个答案

笪智志
2023-03-14

你把事情弄得更复杂了…

这里有一个轻量级和有效的方法来添加特定的选择变化自定义值(从特定的产品属性)到一个附加的html“在单个产品元之后。

您只需要在第一个函数中定义正确的产品属性分类。

守则:

// Add custom variation data to variation form data
add_filter( 'woocommerce_available_variation', 'add_variation_vendor_value', 10, 3 );
function add_variation_vendor_value( $data, $product, $variation ) {
    // Here define the targeted taxonomy used in product variation
    $taxonomy  = 'pa_mats';

    $term_name = $variation->get_attribute($taxonomy);

    if( ! empty($term_name) ) {
        $data['vendor_value'] = $term_name;
    }
    return $data;
}

// Display after product meta an empty span html on variable products 
add_action( 'woocommerce_product_meta_end', 'display_product_vendors', 10 );
function display_product_vendors() {
    global $product;

    if ( $product->get_type() === 'variable' ) {
        echo '<span class="posted_in vendors"></span>';
    }
}

// Fill in our html "span" with specific text value from selected variation
add_action('woocommerce_after_variations_form', 'custom_product_variation_js');
function custom_product_variation_js() {
    ?>
    <script type="text/javascript">
    jQuery(function($) {
        var $form   = $('form.variations_form'),
            $vendor = $('span.vendors'),
            text    = $vendor.text();

        $form.on('show_variation', function(event, data){ // On selected variation
            if ( data.vendor_value ) {
                $vendor.text( data.vendor_value );
            }
        }).on('hide_variation', function(){ // Not on selected variation
            $vendor.text( text );
        });
    });
    </script>
    <?php
}

代码进入函数。活动子主题(或活动主题)的php文件。

在具有已定义产品属性(用于变体)分类法的可变产品上测试并使用最新版本(4.9.2)。

请记住,产品属性分类法总是以“pa_”产品属性slug开头。

 类似资料:
  • 我在WooCommerce单一产品页面上创建了六个自定义属性,其中包含一个自定义函数。它们包括:图标、标签和术语。 基于用每个答案代码的自定义图像替换WooCommerce属性标签,我使用了以下函数(使用自定义HTML标记,与WooCommerce product-attributes.php模板中的标记不同): 该函数工作正常,但存在一个问题:如果特定属性的项不止一个(例如颜色:红色、蓝色、绿色

  • 我成功地在后端的产品变化中的WooCommerce中创建了一个ACF自定义字段。该字段在每个变化中正确显示。 但是在编辑变体中的此字段或其他字段后,我无法再保存整个变体选项卡。加载/保存指示器圆圈继续无限旋转。并且定制字段未显示在前端的单个产品变体中。 我所做的是将以下代码添加到我的: 任何帮助都将不胜感激。

  • 在Woocommerce中,我试图使一个定制的div容器在可变产品的变体缺货时可见,但在缺货时可用。

  • 目前正在使用WordPress 5.1.1和WooCommerce 3.5.7。我的WooCommerce商店有大约500种产品,由简单和多变的产品组成。 每个产品自然都有一个SKU,但每个产品也有一个称为“商品代码”的唯一ID代码。我向特定行业销售特定产品。 我已经在我的functions.php文件中添加了简单和可变产品的自定义字段的代码,目前效果很好。 我的问题是,我试图让“商品代码”出现在

  • 好的,基本上我们在我们的WooCommerce商店中使用ACF创建了一个自定义字段,以便为特定的产品添加一个“发货延迟”通知。

  • 是否有一种方法可以在WooCommerce订单页面的每个产品下显示我的自定义SKU? 当我编辑产品时,自定义sku显示良好,但它不会显示在产品的订单页面中。我需要在订单上显示此信息,以便Zapier可以将其与产品的Visma帐户软件ArticleID匹配。 此尝试基于如何向WooCommerce产品添加(第二个)自定义sku字段的解决方案?