在 WooCommerce 中自动生成简单或可变的产品描述

Auto generate simple or variable product description in WooCommerce

我构建了以下函数以与编辑产品页面上显示的按钮结合使用。它旨在根据产品的标题和 SKU 生成一些描述文本。该代码非常适合 'simple' 产品,但我正在努力让它也适用于 'variable' 产品。

我究竟需要做什么才能让它在简单产品和可变产品上正常工作?

当前行为是:

add_action('woocommerce_process_product_meta', 'update_and_save_utapd');

function update_and_save_utapd() {
    
    if(isset($_POST['button_new_update_description'])){

        $product_id = wc_get_product(get_the_ID());
        $wcProduct = new WC_Product($product_id);

        $get_title = $wcProduct->get_name();
        $get_mpn = $wcProduct->get_meta('sp_wc_barcode_field');

        $get_description = $wcProduct->get_description();
        $output = '';
            
        if(!empty($get_title)){

            $output .= "<p>The " . $get_title;
        
        }if(!empty($get_mpn)){
        
            $output .= " (MPN: " . $get_mpn . ").";
        
        }if(!empty($get_description)){
                
            $wcProduct->set_description($output);
            $wcProduct->save();
        
            return "<div>SUCCESS: YOU HAVE UPDATED YOUR DESCRIPTION.</div>";  
        
        }elseif(empty($get_description)){  
        
            $wcProduct->set_description($output);
            $wcProduct->save();
            
            return "<div>SUCCESS: YOU HAVE GENERATED A NEW DESCRIPTION.</div>";
        
        }
    }
}

使用 woocommerce_process_product_meta 挂钩,您已经拥有可用的产品 ID 和产品对象。 Here您会找到更多信息。

除了 isset() 函数之外,要验证按钮是否已被单击,您还必须检查它的值。

Replace value_button with the value of the element's value attribute

add_action('woocommerce_process_product_meta', 'update_and_save_utapd', 10, 2 );
function update_and_save_utapd( $product_id, $product ) {
    // replace "value_button" with the value of the element's "value" attribute
    if ( isset( $_POST['button_new_update_description'] ) && $_POST['button_new_update_description'] == 'value_button' ) {
        if ( $product->is_type('simple') || $product->is_type('variable') ) {
            $title       = $product->get_name();
            $mpn         = $product->get_meta('sp_wc_barcode_field');
            $description = $product->get_description();
            $output      = '';

            if ( ! empty($title) ) {
                $output .= "<p>The " . $title;
            }

            if ( ! empty($mpn) ) {
                $output .= " (MPN: " . $mpn . ").";
            }
            
            if ( ! empty($get_description) ) {
                $product->set_description($output);
                $product->save();
                return "<div>SUCCESS: YOU HAVE UPDATED YOUR DESCRIPTION.</div>";  
            } else { 
                $product->set_description($output);
                $product->save();
                return "<div>SUCCESS: YOU HAVE GENERATED A NEW DESCRIPTION.</div>";
            }
        }
    }
}

首先,当在后端使用操作挂钩保存产品数据时,您不能 return 一个字符串(文本),无论如何,它永远不会显示

现在,从 WooCommerce 3 开始,您可以使用 woocommerce_admin_process_product_object 更好的钩子,包括 WC_Product 对象作为函数参数,并且无需在您的末尾使用 save() 方法代码一旦触发此挂钩,就会自动应用 save() 方法。

所以我们可以简化您的代码:

add_action('woocommerce_admin_process_product_object', 'update_and_save_utapd');
function update_and_save_utapd( $product ) {
    if( isset($_POST['button_new_update_description']) ){
        $name    = $product->get_name();
        $barcode = $product->get_meta('sp_wc_barcode_field');
        $output  = '';
            
        if ( ! empty($name) ) {
            $output .= "<p>The " . $name;
        } 
        
        if ( ! empty($barcode) ) {
            $output .= " (MPN: " . $barcode . ").";
        }

        $product->set_description( $output );
    }
}

代码进入活动子主题(或活动主题)的 functions.php 文件。它现在应该可以更好地工作,不会引发错误。