Getting Woocommerce SKU in cart and wishlist

2019-07-23 22:13发布

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.

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.

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(); ?>

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

Thanks.

1条回答
你好瞎i
2楼-- · 2019-07-23 23:04

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 ;
    }
}
查看更多
登录 后发表回答