【问题标题】:woocommerce change variation-id to product attribute namewoocommerce 将变体 ID 更改为产品属性名称
【发布时间】:2020-07-20 00:33:21
【问题描述】:

我想通过使用产品属性的名称而不是 id 变体来重构代码。

从此代码:

function action_woocommerce_thankyou( $order_id ) {
    // Get $order object
    $order = wc_get_order( $order_id );

    // Get items
    $items = $order->get_items();

    // Set variable
    $found = false;

    // Set variable
    $output = '';

    // Loop
    foreach ( $items as $item ) {
        // Add whatever variation id you want below here.
        if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9647 ) {
            $output = 'Thank you for buy VARIABLE A-9647';
            $found = true;
            break;
        }

        if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9648 ) {
            $output = 'Thank you for buy VARIABLE B-9648';
            $found = true;
            break;
        }
    }

    // Get payment method
    $payment_method = $order->get_payment_method();

    // Payment method = basc & found = true
    if ( $payment_method == 'bacs' && $found ) {
        $output .= ' YOUR PAYMENT IS BACS';
    }

    // Print result
    echo $output;
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );

我想通过使用产品属性的名称而不是 id 变体来重构代码

如何更改此代码

来自

variation_id

product name attribute

更确切地说是以下几行:

if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9647 )

if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9648 )

提前致谢!

【问题讨论】:

    标签: php wordpress woocommerce hook hook-woocommerce


    【解决方案1】:

    假设你是这个意思?

    function action_woocommerce_thankyou( $order_id ) {
        // Get $order object
        $order = wc_get_order( $order_id );
    
        // Get items
        $items = $order->get_items();
    
        // Set variable
        $found = false;
    
        // Set variable
        $output = '';
    
        // Loop
        foreach ( $items as $item ) {
            // The WC_Product object
            $product = $item->get_product();
    
            // Add whatever attribute you want below here.
            if ( !empty( $product->get_attribute( 'pa_kleur' ) ) ) {
                $output = 'Thank you...1';
                $found = true;
                break;
            }
    
            if ( !empty( $product->get_attribute( 'pa_jaar' ) ) ) {
                $output = 'Thank you...2';
                $found = true;
                break;
            }
        }
    
        // Get payment method
        $payment_method = $order->get_payment_method();
    
        // Payment method = basc & found = true
        if ( $payment_method == 'bacs' && $found ) {
            $output .= ' YOUR PAYMENT IS BACS';
        }
    
        // Print result
        echo $output;
    }
    add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 2019-01-23
      相关资源
      最近更新 更多