dtdd25012 2017-04-20 17:29
浏览 145
已采纳

WooCommerce订阅钩子:获取订单项目

WooCommerce 3.0 broke my app and I cannot figure out how to fix it now.

I have an action for when a subscription is added/changed running here:

Inside the function I was getting the order details and finding the line item for a variable subscription to update my custom DB with the option as well as getting custom order meta that I added via woocommerce_form_field:

This no longer works and everything appears protected? How can I update this to work with 3.0?

add_action( 'woocommerce_subscription_status_changed', 'update_subscription', 10, 3 );
function update_subscription( $id, $old_status, $new_status ) {

    $sitelink_db            = new SSLA_DB_Sitelink();
    $order                  = new WC_Order( $id );
    $items                  = $order->get_items();
    $subscription_type  = '';
    $user_id                = $order->get_user_id();
    $sitelink_domain        = get_post_meta( $order->id, 'ssla_sitelink_url', true );

    foreach ($items as $item) {

        if( "SiteLink Subscription" === $item['name'] ) {
            $subscription_type = $item['brand'];
        }

    }

    $customer_data = array(
        'user_id'               => $user_id,
        'subscription_type'     => $subscription_type,
        'domain_referrer'       => $sitelink_domain,
        'active_subscription'   => $new_status,
        'date_modified'         => date( 'Y-m-d H:i:s' ),
        );

    $sitelink_db->add( $customer_data );

}

Basically I need to get that variation name of the subscription to store in my DB, as well as that custom meta field I made. Which does not work anymore either

  • 写回答

1条回答 默认 最新

  • douhuanglou1445 2017-04-20 19:51
    关注

    Here's my best guess. It's impossible to test since I don't have the same setup as you.

    Few notes:

    1. The $subscription object is passed to the woocommerce_subscription_status_changed hook so let's use it.
    2. $order->id should be replaced by $order->get_id() in WC3.0, but we're going to use the the $subscription object (the subscription order class extends the order class so it's similar).
    3. getters must be used on the WC_Order_Item_Product object that is returned when looping through get_items() so $item['name'] becomes $item->get_name()

    Here's the full code block:

    add_action( 'woocommerce_subscription_status_changed', 'update_subscription', 10, 4 );
    function update_subscription( $subscription_id, $old_status, $new_status, $subscription ) {
    
        $match_this_id = 99; // Change this to the product ID of your special subscription
    
        $sitelink_db            = new SSLA_DB_Sitelink();
    
        $items                  = $subscription->get_items();
        $subscription_type  = '';
        $user_id                = $subscription->get_user_id();
        $sitelink_domain        = $subscription->get_meta( 'ssla_sitelink_url' );
    
        foreach ($items as $item) {
    
            if( $match_this_id === $item->get_product_id() ) {
                $product = $item->get_product();
                if( $product->is_type( 'variation' ) ){
                    $subscription_type = $product->get_attribute( 'brand' );
                }
            }
    
        }
    
        $customer_data = array(
            'user_id'               => $user_id,
            'subscription_type'     => $subscription_type,
            'domain_referrer'       => $sitelink_domain,
            'active_subscription'   => $new_status,
            'date_modified'         => date( 'Y-m-d H:i:s' ),
            );
    
        $sitelink_db->add( $customer_data );
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化