0

I want to know, on the wc report page (or somewhere else), the page from which a product is added to the cart.

I'm working at an online food market which has a recipe blog, on each recipe page there is a section featuring the products used on the recipe.

We're developing an affiliate program in which each recipe author will get paid for each product a customer adds from one of his/her recipe; for such reason it's imperative for us to know the page from which each product is added to the cart.

Any idea about how to get this done? I appreciate the help, and I'll give further details if necessary. Thanks!

2 Answers 2

0

Please try below code :

In this display only page/post URL and page/post ID. You need to store page ID/URL and can get from database.

NOTE : This is code working only in details page.

function custom_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
    $page_url = wp_get_referer();
    $page_id = url_to_postid( $page_url );
}
add_filter( 'woocommerce_add_cart_item_data', 'custom_to_cart_item', 10, 3 );
2
0

I was able to get it done, here is the code I used:

// To create a new product meta data with the page from which the product is added:

function custom_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
$page_url = wp_get_referer();
$page_id = url_to_postid( $page_url );
$cart_item_data['page-of-addition'] =  $page_id;

return $cart_item_data;}

add_filter( 'woocommerce_add_cart_item_data', 'custom_to_cart_item', 10, 3 );

In the case you want to display such value un the order page:

function add_addition_page_to_order_items( $item, $cart_item_key, $values, $order ) {
if ( empty( $values['page-of-addition'] ) ) {
    return;
}

$item->add_meta_data( __( 'Page of addition', 'wpi18n' ), $values['page-of-addition'] );
}
add_action( 'woocommerce_checkout_create_order_line_item', 'add_addition_page_to_order_items', 10, 4 );

Thanks a lot @Milan Hirpara. I also complemented the code with some of what's shared on iconic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.