更改了购物车库存状态中基于 woocommerce_cart_shipping_method_full_label 的产品

问题描述 投票:0回答:1

我们有一个在线商店,我想根据购物车中产品的库存状态修改运输方式下方的标签(挂钩:woocommerce_cart_shipping_method_full_label)。

基本上,如果有 1 个产品的库存状态为“待订购”,则运输方式 1 的标签 =“48/72h”,运输方式 2 的标签 =“48/72h”。

如果有 1 个产品为“限量库存”,则运输方式标签 1 =“24/48h”,运输方式标签 2 =“48/72h”。

否则(即所有产品均为“有货”)标签 =““下午 4 点前发货”。

我最接近的尝试是这样的:(但它会检查每个产品,也就是说,它会添加与购物车中的产品数量一样多的标签 - 这是错误的)

add_filter( 'woocommerce_cart_item_name', 'custom_text_based_status_name', 10, 3 );
function custom_text_based_status_name( $item_name, $cart_item, $cart_item_key ) {
if( ! ( is_checkout() ) )
return $item_name;
$shipping_class_1 = 'onbackorder';
$shipping_class   = $cart_item['data']->get_stock_status();
if( $shipping_class === $shipping_class_1 ) {
add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_shipping_methods_label', 10, 2 ); 
function custom_shipping_methods_label( $label, $method ) { 
switch ( $method->id ) {
case 'fish_n_ships:62': //Portugal Continental - Transportadora
$txt = __('Expedição estimada: 24 a 48h úteis''); // <= Additional text
break;
case 'fish_n_ships:63': //Portugal Continental - Gratuito
$txt =  __('Expedição estimada: 72 a 96h úteis'); // <= Additional text
break;
default: //nos restantes casos
$txt =  __('Confirmaremos assim que possível'); // <= Additional text
}
return $label . '<br /><small style="color:#777777">' . $txt . '</small>';
}
}    
return $item_name; 
}`

我也尝试过,但显示严重错误:

add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_shipping_method_label', 10, 3 ); 
function custom_shipping_method_label( $label, $method, $cart_item ) { 
switch ([$method->id, $cart_item['data']->get_stock_status]) {
case ['fish_n_ships:62', 'instock']: //Portugal Continental - Transportadora
$txt = __('Expedição estimada: 24 a 48h úteis'); // <= Additional text
break;
case ['fish_n_ships:62', 'stock_limitado']: //Portugal Continental - Transportadora
$txt = __('Expedição estimada: 24 a 48h úteis'); // <= Additional text
break;
default: //nos restantes casos
$txt =  __('Confirmaremos assim que possível'); // <= Additional text
}
return $label . '<br /><small style="color:#777777">' . $txt . '</small>';
}
php wordpress woocommerce cart shipping-method
1个回答
0
投票

您的代码尝试中存在错误和遗漏的内容。

要恢复,您有 2 种运输方式:

  • 运输方式 1:“fish_n_ships:62”(葡萄牙大陆航空 - 付费),
  • 运送方式 2:“fish_n_ships:63”(葡萄牙大陆航空 - 免费)。

您的请求(与购物车商品相关):

  • 如果至少有 1 件商品的库存状态为“onbackorder”,则在两种运输方式的运输标签上添加“Expedição estimada: 48 a 72h úteis”,
  • 如果至少有 1 件商品的库存状态为“limited_stock”,则添加:
    • 运输方式 1 的运输标签上注明“Expedição estimada: 24 a 48h úteis”,
    • 运输方式 2 的运输标签上注明“Expedição estimada: 48 a 72h úteis”,
  • 如果所有商品的库存状态均为“库存”,则在两种运输方式的运输标签上添加“Expedição às 16h”。

尝试以下操作:

// Utility function that get the stock status (prioritized) from cart items
function cart_items_stock_status(){
    // Define all stock status
    $data = array('instock' => 0, 'stock_limitado' => 0, 'onbackorder' => 0 );
    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $item ) {
        $data[$item['data']->get_stock_status()] += $item['quantity'];
    }

    if ( $data['onbackorder'] > 0 ) {
        return 'onbackorder';
    } elseif ( $data['stock_limitado'] > 0 ) {
        return 'stock_limitado';
    } else {
        return 'instock';
    }
}

// Add the delivery estimated time to the shipping label
add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_shipping_method_label', 10, 3 ); 
function custom_shipping_method_label( $label, $method ) { 
    $stock_status = cart_items_stock_status();
    
    if ( $stock_status === 'onbackorder' ) {
        $text = __('Expedição estimada: 48 a 72h úteis');
    } 
    elseif ( $stock_status === 'stock_limitado' ) {
        if ( $method->id === 'fish_n_ships:62' ) {
            $text = __('Expedição estimada: 24 a 48h úteis');
        } 
        elseif ( $method->id === 'fish_n_ships:63' ) {
            $text = __('Expedição estimada: 48 a 72h úteis');
        }
    } 
    else {
        $text = __('Expedição às 16h'); // In stock
    }

    if ( $method->id === 'fish_n_ships:62' || $method->id === 'fish_n_ships:63' ) {
        $label .= '<br /><small style="color:#777777">' . $text . '</small>';
    }
    return $label;
}

代码位于子主题的functions.php 文件中(或插件中)。应该可以。

© www.soinside.com 2019 - 2024. All rights reserved.