繁体   English   中英

WooCommerce - 在购物车、结帐和“浮动购物车”插件上编辑产品名称

[英]WooCommerce - Edit Product Name on Cart, Checkout and “Floating Cart” Plugin

我有两个代码可以更改 woocommerce 变量产品上的产品名称。 第一个在任何地方都可以正常工作,但在我的“ 浮动购物车插件”中却不行(它使用默认产品名称)。

    add_action( 'woocommerce_before_calculate_totals', 'custom_cart_items_prices', 10, 1 );
function custom_cart_items_prices( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    foreach ( $cart->get_cart() as $cart_item ) {

        $product = $cart_item['data'];
        $modell = $product->get_attribute('pa_modell');
        $reparatur = $product->get_attribute('pa_reparatur');

        $original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;

        $new_name = '<class="new-product-name">' . $original_name . __( " ", "woocommerce") . $modell . __( " - ", "woocommerce") . $reparatur;

        if( method_exists( $product, 'set_name' ) )
            $product->set_name( $new_name );
        else
            $product->post->post_title = $new_name;
    }
}

第二个正在开发我的“ 浮动购物车插件”,但如果我也使用它,我在购物车和结帐页面表上有 2 次相同的名称。

    function show_model_in_cart_items( $item_name, $cart_item, $cart_item_key ) {

    $product = $cart_item['data'];
    $modell = $product->get_attribute('pa_modell');
    $reparatur = $product->get_attribute('pa_reparatur');

    {
        $item_name = '<class="product-model">' . $item_name . __( " ", "woocommerce") . $modell . __( " - ", "woocommerce") . $reparatur;
    } 

    return $item_name;
}
add_filter( 'woocommerce_cart_item_name', 'show_model_in_cart_items', 99, 3 );

该代码的某些部分是在互联网上找到的,其他部分来自我自己。 有谁知道如何组合它,以便它在购物车中没有相同名称的任何地方工作 2 次?

我找到了解决方案。 有时它是如此简单……就像一个简单的 if 和 else。

add_filter( 'woocommerce_cart_item_name', 'show_model_in_cart_items', 99, 3 );
function show_model_in_cart_items( $item_name, $cart_item, $cart_item_key ) {

       if ( is_checkout() ) {
       return $item_name;
       }
       else {
    $product = $cart_item['data'];
    $modell = $product->get_attribute('pa_modell');
    $reparatur = $product->get_attribute('pa_reparatur');

    {
        $item_name = '<class="reparatur-beschreibung">' . $item_name . __( " ", "woocommerce") . $modell . __( " - ", "woocommerce") . $reparatur;
    } 
        return $item_name;
}}

现在浮动购物车插件可以在任何地方使用它,除了我有其他代码来更正产品标题的结帐。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM