1

WooCommerce で、返品したクライアントが受け取った注文の製品 SKU を変更しようとしています。
現在、現在のコードを使用していますが、機能していないようです。

私は2つの異なる製品を持っています。id の製品5836は通常の製品で、もう 1 つはサブスクリプションです。

これは私のコードです:

function action_woocommerce_thankyou( $order_id ) {  
    // Let's get the order details
    $order = wc_get_order( $order_id );

    // Customer who made the purchase
    $user_id = $order->get_user_id();

    // Now get order count for the customer
    $order_count = wc_get_customer_order_count( $user_id );
    $items = $order->get_items();

    foreach ( $order->get_items() as $item_id => $item) {
        $product_name = $item['name'];
        $product_id = $item['product_id'];
        $product_variation_id = $item['variation_id'];


        if ( $order_count > 1) {

            // Returning customer...
            if ( $product_id == '5836') {
            update_post_meta( $order_id['product_id'],  '_sku', $ANT0001 );}
            else {

            // Returning customer...
            update_post_meta( $order_id['product_id'],  '_sku', $ANT0002 );}


        } else {

            // New customer...

        }

    }
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 ); 
4

1 に答える 1

1

更新(コメントに関連) : 最初に注文の SKU アイテムitem IDに関連して、関連データベース テーブルに関連する SKUの関連データがないため、注文の SKU を更新することはできませんwp_woocommerce_order_itemmeta。関連する SKU データは、製品から直接取得されます。


あなたのコードをテストしましたが、グローバルに動作します。

それがうまくいかないのは

update_post_meta( $order_id['product_id'],  '_sku', $ANT0001 );
// and
update_post_meta( $order_id['product_id'],  '_sku', $ANT0001 );

未定義の変数$ANT0001$ANT0002を使用しているためです。代わりに、これを使用する必要があります。

update_post_meta( $product_id,  '_sku', 'ANT0001' );
// and
update_post_meta( $product_id,  '_sku', 'ANT0001' );

または、2 つの変数$ANT0001とを定義する必要があります。$ANT0002これは、期待どおりに機能します。

通常、SKU 参照番号は製品ごとに一意でなければならないことを忘れないでください

于 2016-10-07T07:31:11.743 に答える