WooCommerce 挂钩 - woocommerce_after_cart_item_name

WooCommerce Hook - woocommerce_after_cart_item_name

我正在使用一个函数向产品添加自定义元数据。我在 Product Loop woocommerce_after_shop_loop_item 和 Product Single Page woocommerce_product_meta_end.

上对 SHOW 使用了以下挂钩

因此,在使用挂钩申请在 CART PAGE product/item 上获得相同结果时 woocommerce_after_cart_item_name 无效,没有结果。

如果挂钩 woocommerce_after_cart_item_name 与前面提到的其他挂钩一起使用,为什么它不起作用?

这是我正在使用的代码。我只是更改挂钩以使其显示在 Product Loop 和 Product Single Page 中,还需要它显示在购物车产品中。我只是想知道为什么它不适用于购物车项目挂钩..

public function woocommerce_after_cart_item_name() 
{
    global $product;

    if ( !PluginOptions::value('shop_season') && !PluginOptions::value('shop_car_type') )
        return;

    $product_id = $product->get_id();

    $season = get_post_meta( $product_id, 'season', true );
    $car_type = get_post_meta( $product_id, 'car_type', true );

    $tips_seasons   = $this->ui_tips_season();
    $tips_car_types = $this->ui_tips_car_types();

    ?>
    <div class="tyre-details tyre-tips">
        <?php if ( PluginOptions::value('shop_season') && $season ): ?>
            <?php echo $tips_seasons[ strtolower($season) ]; ?>
        <?php endif ?>
        <?php if ( PluginOptions::value('shop_car_type') && $car_type ): ?>
            <?php echo $tips_car_types[ strtolower($car_type) ]; ?>
        <?php endif ?>
    </div>
    <?php
}

来自插件。我刚刚从 woocommerce 支持那里得到了这段代码,但我不知道如何完成它。他说在下面的代码中用我的 meta_keys 替换,我将在下面 post 。你能帮我完成这个吗?或者告诉我哪里需要更换。我的 meta_keys 是 $season 和 $car-type 但我不知道如何申请 woocommerce 提供的代码。

   // Display custom cart item meta data (in cart and checkout)
add_filter( 'woocommerce_get_item_data',     'display_cart_item_custom_meta_data', 10, 2 );
function display_cart_item_custom_meta_data( $item_data, $cart_item ) {
    $meta_key = 'PR CODE';
    if ( isset($cart_item['add_size']) && isset($cart_item['add_size']        [$meta_key]) ) {
    $item_data[] = array(
        'key'       => $meta_key,
        'value'     => $cart_item['add_size'][$meta_key],
    );
}
return $item_data;
}

// Save cart item custom meta as order item meta data and display it      everywhere on orders and email notifications.
add_action( 'woocommerce_checkout_create_order_line_item',     'save_cart_item_custom_meta_as_order_item_meta', 10, 4 );
function save_cart_item_custom_meta_as_order_item_meta( $item, $cart_item_key, $values, $order ) {
$meta_key = 'PR CODE';
if ( isset($values['add_size']) && isset($values['add_size'][$meta_key]) ) {
    $item->update_meta_data( $meta_key, $values['add_size'][$meta_key] );
}
}

我找到了 ClassName(class FeatureTyreTips)并将其添加到您提供的代码中。我在 functions.php 中输入了这个,它在加载购物车页面时显示致命错误。我也尝试将它放在 cart.php 中并得到相同的结果...并且我尝试在该代码最初来自的同一个插件文件中进行尝试。所有 3 个位置都不起作用...唯一的区别是在加载购物车页面时在插件文件中添加和激活新代码时没有显示致命错误。有任何想法吗?感谢 Vdgeatano

“致命错误”很可能是由于“无法静态调用非静态方法”:PHP - Static methods

我现在已经更正了代码。

考虑到您的代码中缺少的 class 名称是 FeatureTyreTips,在产品名称后添加一些文本的正确代码是:

add_action( 'woocommerce_after_cart_item_name', 'add_custom_text_after_cart_item_name', 10, 2 );
function add_custom_text_after_cart_item_name( $cart_item, $cart_item_key ) {
    
    // create an instance of the "PluginOptions" class
    $PluginOptions = new PluginOptions();
    if ( ! $PluginOptions->value( 'shop_season' ) && ! $PluginOptions->value( 'shop_car_type' ) ) {
        return;
    }

    $product = $cart_item['data'];

    $season = get_post_meta( $product->get_id(), 'season', true );
    $car_type = get_post_meta( $product->get_id(), 'car_type', true );

    // create an instance of the "FeatureTyreTips" class
    $FeatureTyreTips = new FeatureTyreTips();
    $tips_seasons   = $FeatureTyreTips->ui_tips_season();
    $tips_car_types = $FeatureTyreTips->ui_tips_car_types();

    if ( ! $PluginOptions->value('shop_season') || ! $season ) {
        $season = '';
    }

    if ( ! $PluginOptions->value('shop_car_type') || ! $car_type ) {
        $car_type = '';
    }

    $html = '<div class="tyre-details tyre-tips">' . trim( $season . ' ' . $car_type ) . '</div>';
    echo $html;

}

代码已经过测试(在可能的情况下)并且有效。
需要将其添加到主题的 functions.php 文件或插件中。