简体   繁体   English

基于 Woocommerce 产品变体库存数据复选框选项的自定义显示

[英]Custom display based on Woocommerce product variations stock data checkbox option

I am trying to add a custom message to variation products, based on their stock quantity and stock status.我正在尝试根据库存数量和库存状态向变体产品添加自定义消息。

So far i got this:到目前为止,我得到了这个:

function load_variation_settings_fields( $variation_data, $product, $variation ) {

    // Display shipping delay text when stock quantity exist
    if( $variation->get_stock_quantity() > 0 )
        $variation_data['text_field'] = __('<p class="stock in-stock">Stock: <span>På lager</span><br>Delivery: <span>2-12 hverdage</span></p>'); 
    else $variation_data['text_field'] = __('<p class="stock in-stock">Stock: <span>Out of stock</span><br>Delivery: <span>4-6 weeks</span></p>');

    return $variation_data;
}

It works, and displays the message based on the quantity of each variation, but I need this to only work for one type of stock status (a custom one).它可以工作,并根据每个变体的数量显示消息,但我需要它仅适用于一种类型的库存状态(自定义状态)。 I would need to display a different message for a different variation stock status.我需要为不同的变体库存状态显示不同的消息。 This is what I have tried:这是我尝试过的:

function load_variation_settings_fields( $variation_data, $product, $variation ) {

    // Display shipping delay text when stock quantity exist
    if( $variation->get_stock_quantity() > 0 && ($stockstatus == 'customstatus'))
        $variation_data['text_field'] = __('<p class="stock in-stock">Stock: <span>In stock</span><br>Delivery: <span>2-12 days</span></p>'); 
    elseif ( ($stockstatus == 'customstatus') )
    $variation_data['text_field'] = __('<p class="stock in-stock">Stock: <span>Out of stock</span><br>Delivery: <span>4-6 weeks</span></p>');

    return $variation_data;
}

It is displayed with this, in the variation.php file:它显示在variation.php文件中:

<div class="woocommerce-variation-custom-text-field">{{{ data.variation.text_field }}}</div>

Any help or point in the right direction is appreciated.任何帮助或指向正确方向的点都表示赞赏。

EDIT: This is my latest attempt, as my custom stock status is stored in the '_stock_status' meta value.编辑:这是我的最新尝试,因为我的自定义库存状态存储在 '_stock_status' 元值中。 Still doesn't work though.不过还是不行。

    function load_variation_settings_fields( $variation_data, $product, $variation ) {
    $stockstatus = $product->get_attribute( '_stock_status' );
if( ($stockstatus == 'bestillingsvare') ) {
        // Display shipping delay text when stock quantity exist
        if( $variation->get_stock_quantity() > 0 )
            $variation_data['text_field'] = __('<p class="stock in-stock">Stock: <span>På lager</span><br>Delivery: <span>2-12 hverdage</span></p>'); 
        else $variation_data['text_field'] = __('<p class="stock in-stock">Stock: <span>Out of stock</span><br>Delivery: <span>4-6 weeks</span></p>');
  }
        return $variation_data;
    }

As we don't know really how you set your custom status 'bestillingsvare', it's not possible to reproduce the problem.由于我们真的不知道您如何设置自定义状态“bestillingsvare”,因此无法重现该问题。 Next time, it should be necessary to add all related code in your question.下次,应该有necessary在您的问题中添加所有相关代码。

So here is a similar working solution, with an additional custom setting in your product variations that works for real .所以这里有一个类似的工作解决方案,在您的产品变体有一个额外的自定义设置,适用于真实的.

First, you need to add the following line in /single-product/add-to-cart/variation.php template file:首先,您需要在/single-product/add-to-cart/variation.php模板文件中添加以下行:

<div class="woocommerce-variation-delivery">{{{ data.variation.delivery_html }}}</div>

Then the revisited complete code:然后重温完整代码:

// Add variation custom checkbox setting option field
add_action( 'woocommerce_variation_options', 'add_variation_delivery_status_option', 20, 3 );
function add_variation_delivery_status_option ( $loop, $variation_data, $post_object ) {
    $checked = get_post_meta( $post_object->ID, '_delivery_option', true ) ? true : false;
    ?>
    <label class="tips" data-tip="<?php _e( 'Enable', 'woocommerce' ); ?>">
    <?php _e( 'Delivery?', 'woocommerce' ); ?>
        <input type="checkbox" class="checkbox variation_delivery_option" name="delivery_option_<?php
        echo $loop; ?>" <?php checked( $checked, true ); ?> />
    </label>
    <?php
}

// Save variation custom checkbox setting option field value
add_action( 'woocommerce_save_product_variation', 'save_variation_delivery_status_option', 20, 2 );
function save_variation_delivery_status_option( $variation_id, $i ) {
    $value = isset( $_POST['delivery_option_'.$i] ) ? true : false;
    update_post_meta( $variation_id, '_delivery_option', $value );
}

// Display in front end the delivery info
add_filter( 'woocommerce_available_variation', 'display_variation_delivery_status_option', 20, 3 );
function display_variation_delivery_status_option( $variation_data, $product, $variation ) {
    if( get_post_meta( $variation->get_id(), '_delivery_option', true ) ) {
        $stock_qty      = $variation->get_stock_quantity();

        $stock_class    = $stock_qty > 0 ? 'stock in-stock' : 'stock out-of-stock';
        $stock_label    = $stock_qty > 0 ? __('På lager') : __('Out of stock');
        $delivery_delay = $stock_qty > 0 ? __('2-12 hverdage') : __('4-6 weeks');

        // Display a shipping delay text when stock quantity exist
        $variation_data['delivery_html'] = sprintf(
            '<p class="%s">' . __('Stock') . ': <span>%s</span><br>
            ' . __('Delivery') . ': <span>%s</span></p>',
            $stock_class, $stock_label, $delivery_delay
        );
    }
    return $variation_data;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 It should work.它应该工作。

In backend variation settings (enabling the delivery option):在后端变体设置中(启用交付选项):

在此处输入图片说明

In Front-end option activated when "In stock":在“有货”时激活的前端选项:

在此处输入图片说明

In Front-end option activated when "Out of stock":在“缺货”时激活的前端选项中:

在此处输入图片说明

But as you see to avoid duplicated information , you could use existing availability_html to change the displayed data adding your delivery information to it instead.但是正如您所看到的,为了避免重复信息,您可以使用现有的availability_html来更改显示的数据,而是将您的交付信息添加到其中。

In this case you will not override /single-product/add-to-cart/variation.php template as it is not needed any more, and you will replace in the last function在这种情况下,您将不会覆盖 /single-product/add-to-cart/variation.php 模板,因为它不再需要,您将在最后一个函数中替换

$variation_data['delivery_html']

by经过

$variation_data['availability_html']

Making the necessary other changes to the code, to get the desired display.对代码进行必要的其他更改,以获得所需的显示。

You can get code status lie:您可以获得代码状态谎言:

// Compatibility for WC versions from 2.5.x to 3.0+
if ( method_exists( $product, 'get_stock_status' ) ) {
     $stockstatus = $product->get_stock_status(); // For version 3.0+
} else {
     $stockstatus = $product->stock_status; // Older than version 3.0
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 显示用于 woocommerce 缺货产品变体的自定义 div 块 - Display a custom div block for woocommerce out of stock product variations woocommerce-显示库存变化 - woocommerce - display variations in stock 显示 现货 WooCommerce 单品简短描述 - Display In stock available variations in WooCommerce single product short description 检查 WooCommerce 产品(简单或变体)是否有库存并将标签显示为短代码 - Check if WooCommerce product (simple or variations) are in stock and display label as shortcode 在 WooCommerce 产品变体中显示 ACF 自定义字段 - Display ACF custom field in WooCommerce product variations 对于没有托管库存的 WooCommerce 变体显示“有货”通知 - Display “In Stock” notice for WooCommerce variations with no Managed Stock 通过 WooCommerce 中的自定义库存数量减少来灰显“缺货”产品变化 - Greying out “out-of-stock” product variations with custom stock quantity reduction in WooCommerce Woocommerce产品变化:更改“库存”的位置 - Woocommerce product variations: Change position of „in stock“ 基于WooCommerce产品当前库存值的自定义库存字段计算 - Custom stock field calculation based on current stock value of WooCommerce Product 在Woocommerce的前端显示自定义广播和复选框产品设置数据 - Display custom radio and checkbox product settings data in front end in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM