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

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

问题描述:

我正尝试根据其库存数量和库存状态向各种产品添加自定义消息.

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;
}

它与此一起显示在variant.php文件中:

It is displayed with this, in the variation.php file:

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

感谢任何帮助或指出正确的方向.

Any help or point in the right direction is appreciated.

这是我的最新尝试,因为我的自定义库存状态存储在"_stock_status"元值中.仍然无法正常工作.

This is my latest attempt, as my custom stock status is stored in the '_stock_status' meta value. 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;
    }

由于我们真的不知道如何将自定义状态设置为"bestillingsvare",因此无法重现该问题.下次,应该将necessary添加到您的问题中的所有相关代码.

As we don't know really how you set your custom status 'bestillingsvare', it's not possible to reproduce the problem. Next time, it should be necessary to add all related code in your question.

因此,这是一个类似的工作解决方案,在产品变体中的其他自定义设置适用于实际情况.

So here is a similar working solution, with an additional custom setting in your product variations that works for real.

首先,您需要在/single-product/add-to-cart/variation.php模板文件中添加以下行:

First, you need to add the following line in /single-product/add-to-cart/variation.php template file:

<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;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.应该可以.

Code goes in function.php file of your active child theme (or active theme). 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":

但是,如您所见,为避免信息重复,您可以使用现有的availability_html更改显示的数据,而在其中添加交货信息.

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.

在这种情况下,您将不会覆盖/single-product/add-to-cart/variation.php模板,因为不再需要它,而您将在最后一个函数中替换

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

$variation_data['delivery_html']

作者

$variation_data['availability_html']

对代码进行必要的其他更改,以得到所需的显示.

Making the necessary other changes to the code, to get the desired display.