WooCommerce中产品属性的新术语

时间:2019-05-19 22:09:53

标签: php mysql wordpress woocommerce taxonomy-terms

在WooCommerce中,我想知道如何通过php脚本和mysql更改woocommerce的产品属性。

这是我正在做的事情:我已经通过wordpress创建了属性,并且已经在产品上激活了主题:

  1. 我在wp_terms中创建了新名词。
  2. 我在wp_term_taxonomy的分类法中的pa_attribute表中创建了新记录。
  3. 我已分配了term_taxonomy_id表中的分类ID wp_term_relationships

现在,为什么新条款不在产品中显示?如果我执行该步骤?我错了什么或我想念什么?

问题与我在wp_term_relationship中添加或删除的内容无关紧要,产品属性不变。

我认为问题在于某些wp缓存,如何在不使用Wordpress函数的情况下将其全部删除。 附注:我使用该教程来编写脚本:https://www.webhat.in/article/woocommerce-tutorial/how-product-attribute-are-stored-in-database/ 我所有的代码:

function trova_termine_db($conn,$parola,$termine_aggiuntivo)
{
    /*
    find a term just created on database using slug
    the slug is "$parola-$termine_aggiuntivo" if $termine_aggiuntivo
    is not empty
    */
    $parola=trim($parola);
    $termine_aggiuntivo=trim($termine_aggiuntivo);
    if(strcasecmp($parola, "non-comunicato") == 0)
    {
        $query="SELECT `term_id` FROM `wp_terms` WHERE `slug`=\"$parola\"";
    }
    else
    {
        if(non_vuota($termine_aggiuntivo))//true if arg not empty
        {
            $query="SELECT `term_id` FROM `wp_terms` WHERE `slug`=\"$parola-$termine_aggiuntivo\"";
        }
        else
        {
            $query="SELECT `term_id` FROM `wp_terms` WHERE `slug`=\"$parola\"";
        }
    }
    $result = $conn->query($query);
    if (!$result) 
    {
        echo "Errore della query in trova termine: " . $conn->error . ".";
        exit();
    }
    $term_id=-1;
    if($result->num_rows==1)
    {
        $data=$result->fetch_assoc();
        $term_id=$data["term_id"];
    }
    $result->close();
    return $term_id;
}
function inserisci_termine_db($conn,$parola,$termine_aggiuntivo,$t_group)
{
/*
insert new term if there isn't
slug "$parola-$termine_aggiuntivo"
$t_group is term_group
*/
    $parola=trim($parola);
    $termine_aggiuntivo=trim($termine_aggiuntivo);
    if(strcasecmp($parola, "non-comunicato") == 0)
    {
        $query="INSERT INTO `wp_terms`(`name`, `slug`, `term_group`) VALUES (\"Non comunicato\",\"$parola\",\"$t_group\");";
    }
    else if(non_vuota($termine_aggiuntivo))//true if arg not empty
    {
        $query="INSERT INTO `wp_terms`(`name`, `slug`, `term_group`) VALUES (\"$parola $termine_aggiuntivo\",\"$parola-$termine_aggiuntivo\",\"$t_group\");";
    } 
    else
    {
        $query="INSERT INTO `wp_terms`(`name`, `slug`, `term_group`) VALUES (\"$parola\",\"$parola\",\"$t_group\");
        ";
    }
    $result = $conn->query($query);
    if (!$result) 
    {
      echo "Errore della query in inserisci termine: " . $conn->error . ".";
      exit();
    }
    $id_inserito=$conn->insert_id;
    return $id_inserito;
}
function inserisci_term_taxonomy($conn,$id_valore,$taxonomy)
{
/*
create a term taxonomy for a term
*/
    $query="INSERT INTO `wp_term_taxonomy`(`term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (\"$id_valore\",\"$taxonomy\",\"\",\"0\",\"1\")";
    $result = $conn->query($query);
    if (!$result) 
    {
      echo "Errore della query in inserisci taxonomy: " . $conn->error . ".";
      exit();
    }
    $id_inserito=$conn->insert_id;
    return $id_inserito;
}
function incrementa_decrementa_contatore_termine($conn,$id_valore,$operatore)
{
/*
update the count in wp_term_taxonomy for the right number of istance of the term
*/
    $query="UPDATE `wp_term_taxonomy` SET `count` = (`count` $operatore 1) WHERE  `term_id`=\"$id_valore\"";
    $result = $conn->query($query);
    if (!$result) 
    {
        echo "Errore della query incr/decr cont: " . $conn->error . ".";
        exit();
    }
}
function trova_term_taxonomy_id($conn,$id_valore,$taxonomy)
{
/*
find if a term_id with right taxonomy just have a record in the wp_term_taxonomy table
*/
    $query="SELECT `term_taxonomy_id` FROM `wp_term_taxonomy` WHERE `term_id`=\"$id_valore\" AND `taxonomy`=\"$taxonomy\"";
    $result = $conn->query($query);
    if (!$result) 
    {
        echo "Errore della query trova term taxonomy: " . $conn->error . ".";
        exit();
    }
    $id=-1;
    if($result->num_rows==1)
    {
        $data=$result->fetch_assoc();
        $id=$data["term_taxonomy_id"];
    }
    $result->close();
    return $id;
}
function trova_post_id_db($conn,$codice_articolo)
{
/*
find a post_id by sku($codice_articolo) of product
*/
    $query="
    SELECT post_id
    FROM `wp_postmeta` 
    where `meta_value` = \"$codice_articolo\" AND `meta_key`=\"_sku\"";
    $result = $conn->query($query);
    if (!$result) 
    {
      echo "Errore della query trova id post: " . $conn->error . ".";
    }

    $post=-1;
    if ($result->num_rows == 1) 
    {
        $data=$result->fetch_assoc();
        $post=$data["post_id"];
    }
    $result->close();
    return $post;
}
function scrivi_su_db_arrivi($conn,$codice_articolo,$valore,$termine_aggiuntivo,$taxonomy)
{
/*
is the main function and use the functions up, to find/create new terms
and find/create the related record on wp_term_taxonomy
and update/create new record in wp_term_relationship for assign term to product
*/
    $valore=trim($valore);
    if(strcasecmp($termine_aggiuntivo,"giorni")==0)
    {
        if(!ctype_digit($valore))
        {
            $valore="non-comunicato";
        }
    }
    else 
    {
        if(!ctype_digit($valore))
        {
            $valore=0;
        }
    }

    $id_valore=-1;
    $id_term_taxonomy=-1;
    $id_valore=trova_termine_db($conn,$valore,$termine_aggiuntivo);
    if($id_valore == -1)
    {
        $id_valore=inserisci_termine_db($conn,$valore,$termine_aggiuntivo,0);
        $id_term_taxonomy=trova_term_taxonomy_id($conn,$id_valore,$taxonomy);
        if($id_term_taxonomy==-1)
        {
            $id_term_taxonomy=inserisci_term_taxonomy($conn,$id_valore,$taxonomy);
        }
    }
    else
    {
        incrementa_decrementa_contatore_termine($conn,$id_valore,'+');
        $id_term_taxonomy=trova_term_taxonomy_id($conn,$id_valore,$taxonomy);
        if($id_term_taxonomy==-1)
        {
            $id_term_taxonomy=inserisci_term_taxonomy($conn,$id_valore,$taxonomy);
        }

    }

    if($id_valore==-1 || $id_term_taxonomy==-1)
    {
        echo "errore id termine sono -1\n";
        exit();
    }
    $post_id=trova_post_id_db($conn,$codice_articolo);

    $query="
    SELECT `term_id`,wptr.`term_taxonomy_id` FROM `wp_term_relationships`wptr INNER JOIN `wp_term_taxonomy`wptt on wptr.`term_taxonomy_id`=wptt.`term_taxonomy_id` where `taxonomy`=\"$taxonomy\" and `object_id`=\"$post_id\"
    ";


    $result = $conn->query($query);
    if (!$result) 
    {
      echo "Errore della query: " . $conn->error . ".";
    }

    $term_id_da_togliere=-1;
    $term_taxonomy_id_da_togliere=-1;
    if ($result->num_rows == 1) 
    {
        $data=$result->fetch_assoc();
        $term_id_da_togliere=$data["term_id"];
        $term_taxonomy_id_da_togliere=$data["term_taxonomy_id"];
    }
    $result->close();
    if($term_taxonomy_id_da_togliere!==$id_term_taxonomy)
    {
        if($term_id_da_togliere==-1 ||$term_taxonomy_id_da_togliere==-1)
        {
            $query="INSERT INTO `wp_term_relationships` (`object_id`,`term_taxonomy_id`,`term_order`) VALUES(\"$post_id\",\"$id_term_taxonomy\",\"0\")";

        }
        else
        {
            incrementa_decrementa_contatore_termine($conn,$term_id_da_togliere,'-');

            $query="UPDATE `wp_term_relationships` SET `term_taxonomy_id`=\"$id_term_taxonomy\" WHERE `object_id`=\"$post_id\" AND `term_taxonomy_id`=\"$term_taxonomy_id_da_togliere\"";

        }
        $result = $conn->query($query);
        if (!$result) 
        {
            echo "Errore della query: " . $conn->error . ".";
        }
    }
}

WP_TERMS

WP_TERMS

WP_TERM_TAXONOMY

WP_TERM_TAXONOMY

WP_TERM_RELATIONSHIP

WP_TERM_RELATIONSHIP

值属性不变:

Value attribute not change

0 个答案:

没有答案