更新Woocommerce订阅订单项元数据

时间:2019-03-04 20:56:38

标签: wordpress woocommerce woocommerce-subscriptions

我正在尝试更新所有订阅,以包括“交货频率”和“每批行李”的元数据。交货频率和每批行李数是产品属性。最初创建订单时,我可以传递这些元数据值,但现在我想返回所有活动的订阅并添加这些值。

function update_subscriptions() {

  $args = array('subscriptions_per_page' => '-1');
  $subscriptions = wcs_get_subscriptions($args);

  foreach ($subscriptions as $subscription):

    $subscription_wc = new WC_Subscription($subscription->get_id());
    $subscription_items = $subscription_wc->get_items();

    foreach($subscription_items as $item):

      $product = $item->get_product();

      // Check to make sure item is a subscription product 
      if( class_exists( 'WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product ) ) {
        $delivery_frequency = $product->get_attribute('pa_delivery-frequency');
        $bags_per_shipment = $product->get_attribute('pa_bags-per-shipment');
        if (!empty($delivery_frequency)) {
          $item->update_meta_data( 'Delivery Frequency', $delivery_frequency );
        }
        if (!empty($bags_per_shipment)) {
          $item->update_meta_data( 'Bags Per Shipment', $bags_per_shipment );
        }
      }

    endforeach;

  endforeach;

}

0 个答案:

没有答案