简体   繁体   English

在购物车和愿望清单中获取Woocommerce SKU

[英]Getting Woocommerce SKU in cart and wishlist

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. 我购买了Woocommerce愿望清单,并试图获取SKU以及价格,数量等信息。

I found this code to add to the functions.php file: 我发现此代码添加到functions.php文件中:

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. 我在购物车中练习,并假设心愿单是相同的。

I tried YITH Wishlist plugin and was successful in getting the SKU to that wishlist using this code (thanks to skrilled ): 我尝试了YITH Wishlist插件,并使用以下代码成功将SKU转到了该愿望清单(感谢skrilled ):

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

However, that same code does not seem to work using the Woocommerce Wishlist plugin. 但是,使用Woocommerce愿望清单插件似乎无法使用相同的代码。

Thanks. 谢谢。

For woocommerce wishlist plugin: 对于woocommerce愿望清单插件:

    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 ;
    }
}

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

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