• Resolved ahnabil

    (@ahnabil)


    I’m using woocommerce with Custom Post Types to enable CPTs to be added to cart as a workaround on a running system

    i’m facing an issue returning the permalink of the CPT as a hyperlink for the product name at the cart page and the orders page

    i’ve tried to filter but i can’t get it to hyperlink the product name as to the product url which is a permalink

    woocommerce 2.6 wp 4.5.3
    thanks in advance

    //item permalink
    add_filter('woocommerce_cart_item_permalink','listing_peramlink',20,2);
    function listing_peramlink($product_permalink,$post){
    if ($post->post->post_type == 'listing_type')
    $product_permalink = get_permalink( get_post_meta($post->id));
    return $product_permalink;
    }

    https://wordpress.org/plugins/woocommerce/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Contributor Claudio Sanches

    (@claudiosanches)

    i’m facing an issue returning the permalink of the CPT as a hyperlink for the product name at the cart page and the orders page

    i’ve tried to filter but i can’t get it to hyperlink the product name as to the product url which is a permalink

    Not sure what you are doing… But if you are a CPT that have the same links for the products, this not should work with the WP rewrite rules…

    Plugin Contributor Claudio Sanches

    (@claudiosanches)

    Maybe should be better you only change the permalink structure of the products in this case.

    Thread Starter ahnabil

    (@ahnabil)

    it doesn’t have the same link for the product it has its own system with its own permalink
    i tried renaming the permalink structure for the CPT to be product, but it didn’t work
    i’m using the permalink structure of http://localhost/sample-post/
    and a custom slug for the CPT

    Plugin Contributor Claudio Sanches

    (@claudiosanches)

    If you rename your CTP to product, for sure you’ll have problems.

    Thread Starter ahnabil

    (@ahnabil)

    Yeah i tried, any suggestions?

    Plugin Contributor Claudio Sanches

    (@claudiosanches)

    No idea what are you doing and why create a post type to link to products with the same permalink structure.

    Thread Starter ahnabil

    (@ahnabil)

    Its the same structure but different name of the cpt, woocommerce have “product” as the main cpt, ive a running system that have another name for the cpt.
    What iam trying to do is when at the cart page i want to link the “cpt product name” to its permalink.
    Right now i cant click on the product name

    Plugin Contributor Claudio Sanches

    (@claudiosanches)

    Ok, but if you need to change the link in the cart need to use the woocommerce_cart_item_permalink filter as are you doing, but this filter don’t return any “post”.

    function my_wc_custom_listing_permalink( $permalink, $cart_item ) {
        // $cart_item returns an array if the cart item data.
    
        return $permalink;
    }
    
    add_filter( 'woocommerce_cart_item_permalink', 'my_wc_custom_listing_permalink', 20, 2 );
    

    Here an example of that array: https://pastebin.com/0cnrRKPr

    Thread Starter ahnabil

    (@ahnabil)

    i was looking for a post when nothing was returning it 😀

    Thanks Claudio i was able to get it right

    Best regards

    Thread Starter ahnabil

    (@ahnabil)

    i spoke too early
    all products at the cart are returning the same link what i’m doing wrong??

    add_filter('woocommerce_cart_item_permalink','listing_peramlink_cart',20,2);
    function listing_peramlink_cart($product_permalink,$cart_item){
    global $woocommerce;
    foreach ($woocommerce->cart->get_cart() as $cart_item => $value) {
    $product_permalink = get_permalink($value['product_id'] );
    	}
    return $product_permalink;
    }
    Plugin Contributor Claudio Sanches

    (@claudiosanches)

    what i’m doing wrong??

    All?

    See my example of code and see yours code… Where you can find in my code any $woocommerce->cart->get_cart() ?
    Why are you looping into all cart products to display a single URL?
    That’s why all products display the wrong URL, since always will be for the last product on your loop.

    Thread Starter ahnabil

    (@ahnabil)

    i though i must loop through all the items in cart, i got it right now, thank you again 😀

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Woocommerce Custom post type permalink’ is closed to new replies.