I want to sell a CPT "bet". So if you buy the product you will be enrolled and eventually get a payout.

My problem is that I need to update the post which was bought and increase the payout value and participants count. Here is what I did so far:

First I added a custom post type as product to woocommerce with a new datastore:

function DS_woocommerce_data_stores ( $stores ) {
    $stores['product'] = 'DS_WCCPT_Product_Data_Store_CPT';
    return $stores;

function DS_woocommerce_product_get_price( $price, $product ) {
    if ($post->post->post_type === 'ds-bet') // change birds with your post type
        $price = get_post_meta($post->id, "price", true);
    return $price;

function DS_Create_Product_Data_Store_CPT(){
    // Load Custom Class for Custom Woocommerce Datastore
    require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-ds-WCCPT.php';

I can now buy my CPT like a normal product => Works fine!

But in order to update the bought post meta I need to get the bought post so I did this on woocommerce_payment_complete-hook:

$order = wc_get_order( $order_id ); 
    // Iterating through each "line" items in the order
    foreach ($order->get_items() as $item_id => $item_data) {
        error_log("2: " .$item_data);
        // Get an instance of corresponding the WC_Product object
        $product = $item_data->get_product();
        error_log("3: " . $product);

Logs this:


Note: I bought post-id 72

since "3" is empty I tink there is a problem with my CPT-product. I still get the name of the item, but if it happens to be two items with the same name it would be bad.

so any suggestions? Thank you!

  • What about the total_sales post meta of the post type product – you should reproduce it, or it should be, iirc, part of a custom product type. Note: this question will be closed as it concerns a third party plugin. It'd be better to ask the WC support, forum. – Nicolai Mar 31 '19 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.