【问题标题】:WooCommerce Hook - woocommerce_after_cart_item_nameWooCommerce 挂钩 - woocommerce_after_cart_item_name
【发布时间】:2021-04-28 03:39:57
【问题描述】:

我正在使用一个函数来向产品添加自定义元数据。我在产品循环woocommerce_after_shop_loop_item 和产品单页woocommerce_product_meta_end 上使用了以下钩子。

因此,当申请通过使用钩子在 CART PAGE 产品/项目上获得相同的结果时 woocommerce_after_cart_item_name 它不起作用,没有结果。

如果钩子 woocommerce_after_cart_item_name 与前面提到的其他钩子一起工作,为什么它不能工作?

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

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 替换。你能帮我完成这个吗?或者告诉我需要更换的地方。我的 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(FeatureTyreTips 类)并将其添加到您提供的代码中。我在functions.php中输入了这个,加载购物车页面时出现致命错误。我还尝试将它放在 cart.php 中,结果相同......我尝试在此代码最初来自的同一个插件文件中。所有 3 个位置都不起作用......唯一的区别是它在加载购物车页面时在插件文件中添加和激活新代码时没有显示致命错误。有任何想法吗?格拉齐·维吉塔诺

【问题讨论】:

  • 能否发布完整的代码?
  • 嗨 Vdgaetano,我添加了我正在使用的代码供您查看。
  • 这是一种方法吗?是插件吗? woocommerce_after_cart_item_name 钩子声明在哪里?
  • 这是插件提供的功能之一。我在上面添加了 woocommerce 支持提供的新代码来完成这项工作。
  • 您也应该通过添加致命错误行来修改您的问题。

标签: woocommerce cart hook-woocommerce


【解决方案1】:

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

我现在已经更正了代码。

考虑到您的代码中缺少的类名是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 文件或插件中。

【讨论】:

    猜你喜欢
    • 2020-08-17
    • 1970-01-01
    • 2021-04-21
    • 2016-02-22
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    相关资源
    最近更新 更多