在购物车和愿望清单中获取 Woocommerce SKU [英] Getting Woocommerce SKU in cart and wishlist

查看:53
本文介绍了在购物车和愿望清单中获取 Woocommerce SKU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看遍了,还没有弄清楚这一点,但我已经接近了.我购买了 Woocommerce 心愿单,我正在尝试获得 SKU 以及价格、数量等.

I've looked all over and haven't figured this out, but am close. I purchased the Woocommerce wishlist and I am trying to have the SKU as well as price, quantity, etc.

我发现这段代码要添加到functions.php文件中:

I found this code to add to the functions.php file:

add_filter( 'woocommerce_in_cart_product_title', 'add_sku_in_cart', 20, 3);

function add_sku_in_cart( $title, $values, $cart_item_key ) {
$sku = $values['data']->get_sku();
return $sku ? $title . sprintf(" (SKU: %s)", $sku) : $title;
}

但是我不知道如何使用购物车或心愿单文件中的功能.我正在购物车中练习,并假设愿望清单是相同的.

However I don't know how to use the function in the cart or wishlist file. I'm practicing in the Cart and assume that the Wishlist would be the same.

我尝试了 YITH Wishlist 插件,并成功地使用此代码将 SKU 加入了该愿望清单(感谢 skrilled):

I tried YITH Wishlist plugin and was successful in getting the SKU to that wishlist using this code (thanks to skrilled):

<?php echo $product_obj->get_sku(); ?>

但是,使用 Woocommerce Wishlist 插件时,相同的代码似乎不起作用.

However, that same code does not seem to work using the Woocommerce Wishlist plugin.

谢谢.

推荐答案

对于 woocommerce 愿望清单插件:

For woocommerce wishlist plugin:

    add_filter("woocommerce_in_cartproduct_obj_title", "wdm_test", 10 , 2);

function wdm_test($product_title, $product){
    if(is_a($product, "WC_Product_Variation")){
        $product_test    = new WC_Product($product->variation_id);
       return $product_title . " " . $product_test->get_sku();
    }
    elseif( is_a($product, "WC_Product") ){
        $product_test    = new WC_Product($product->id);
       return $product_title . " " . $product_test->get_sku();
    }
    else{
     return $product_title ;
    }
}

这篇关于在购物车和愿望清单中获取 Woocommerce SKU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆