在发布产品 (WordPress) 时添加产品详细信息?

问题描述

add_action('transition_post_status','send_new_post',9876543210,3);

function send_new_post($new_status,$old_status,$post) {
    
    if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'product') {
        
            
        global $wpdb;
        
        $current_product_id = $post->ID;
        
        if(isset($current_product_id)) {
            $post_id = $current_product_id; 
        } else {
            $post_id = '131';
        }
        
        
        $sql = "SELECT 
                    post_id as id,(SELECT post_title FROM wp_posts WHERE id = pm.post_id) AS title,(SELECT post_name  FROM wp_posts WHERE id = pm.post_id) AS name,(SELECT Meta_value FROM wp_postMeta WHERE post_id = pm.post_id AND Meta_key = '_price' LIMIT 1) AS price,(SELECT Meta_value FROM wp_postMeta WHERE post_id = pm.post_id AND Meta_key = '_regular_price' LIMIT 1) AS 'regular_price',(SELECT Meta_value FROM wp_postMeta WHERE post_id = pm.post_id AND Meta_key = '_stock' LIMIT 1) AS stock,IFNULL((SELECT Meta_value FROM wp_postMeta WHERE post_id = pm.post_id AND Meta_key = '_sku' LIMIT 1),(SELECT Meta_value FROM wp_postMeta WHERE post_id = pm.post_id AND Meta_key = '_custom_field' LIMIT 1)) as sku


                    FROM `wp_postMeta` AS pm
                    JOIN wp_posts AS p ON p.ID = pm.post_id
                    WHERE p.ID = ".$post_id." AND Meta_key in ('_product_version')
                    AND p.post_status in ('publish')";
            
        $results = $wpdb->get_results($sql);

        $result = json_decode(json_encode($results),true);
        
        $data_init = $result[0];

        $tablename=$wpdb->prefix.'product_init';
            
        $data=array(
            'post_id'       => $data_init['id'],'post_title'    => $data_init['title'],'post_name'     => $data_init['name'],'price'         => $data_init['price'],'regular_price' => $data_init['regular_price'],'sku'           => $data_init['sku'],'stock'         => $data_init['stock'],'created_by'    => 'Created By Custom Code' 
        );
        
        $wpdb->insert( $tablename,$data);
        
        
    }
}

不知何故,我只得到了这个

enter image description here

我可以理解我没有得到其他值 price、regular_price、sku 和 stock.......

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)