基于Woocommerce产品变化库存数据的动态自定义显示

时间:2018-07-20 10:35:21

标签: php wordpress woocommerce shipping variations

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

到目前为止,我知道了:

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

它可以工作,并根据每种变化的数量显示消息,但是我只需要这种类型才能处理一种库存状态(自定义)。 我需要针对不同的库存状态显示不同的消息。 这是我尝试过的:

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文件中:

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

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

编辑: 这是我的最新尝试,因为我的自定义库存状态存储在“ _stock_status”元值中。仍然不起作用。

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

2 个答案:

答案 0 :(得分:1)

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

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

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

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

然后重新访问完整的代码:

// 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文件中。应该可以。

在后端版本设置中(启用投放选项):

enter image description here

“有货”时激活了前端选项:

enter image description here

“缺货”时激活了前端选项:

enter image description here

  

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

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

$variation_data['delivery_html']

作者

$variation_data['availability_html']

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

答案 1 :(得分:0)

您可以获取代码状态谎言:

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