在 WooCommerce 产品创建中通过肥皂自动插入产品 [英] Automatic product insert via soap on WooCommerce product creation

查看:51
本文介绍了在 WooCommerce 产品创建中通过肥皂自动插入产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计费程序,为了将该程序连接到我在 wordpress 中的在线商店,我使用了一个插件,但现在我被要求进行以下更新:

I have a billing program and to connect that program to my online store in wordpress i used a plugin but now i've been requested to do the following update :

  • 每当我们在 woocommerce 上创建产品时,它也会在计费程序中自动创建

我向计费计划支持人员寻求帮助来完成此操作,他们给了我以下示例

i asked the billing program support for help to do this and they gave me the following example

 $result = $soap->authenticate( $API_KEY );
 $APISession = $result[1];
 
 
 $ref          = "002";
 $designation  = "Produto de teste";
 $shortName    = "Ptest";
 $tax          = "23";
 $obs          = "teste";
 $isService    = "0";
 $hasStocks    = "0";
 $active       = "1";
 $shortDesc    = "Descricao 123";
 $longDesc     = "Descricao longa, teste 123.";
 $price        = "100";
 $vendorRef    = "";
 $ean          = "";
 

 $product = $soap->insertProduct( $APISession, $ref, $designation, $shortName, $tax, $obs, $isService, $hasStocks, $active, $shortDesc, $longDesc, $price, $vendorRef, $ean);

但这不是我想要的,因为我们在这个例子中手动插入了参数,我想要自动,我向计费程序的支持人员解释了这一点,但他们说他们无法帮助我,所以我在这里问,希望有人能帮助我.

But thats not what i want since we insert the parameters manually on this example and i want that automatically, i explained this to the support of the billing program but they said they couldn't help me with that, so i'm asking here with the hope that someone can help me on this.

注意:这些参数是计费程序参数,我想用来自 woocommerce 产品的内容填充这些参数,我正在使用 webhook 调用函数insertProduct"

Note : These parameters are the billing program parameters and i want to fill these parameter with what comes from the woocommerce product, i am using a webhook to call the function "insertProduct"

推荐答案

钩子 woocommerce_new_product 在创建产品时被触发,因此它是您需要的钩子.所以你不需要 webhook,因为soap 可以完成这项工作.

The hook woocommerce_new_product is triggered when a product is created, so it's the hook you need. So you don't need a webhook, as soap is going to do the job.

现在在下面的代码中,您需要定义变量 $soap$API_KEY.

Now in the code below you will need to define variables $soap and $API_KEY.

还要注意,在 WooCommerce 产品中,变量 $shortName$tax$obs$isService$vendorRef$ean 不会退出.

Also note that in WooCommerce Products, the variables $shortName, $tax, $obs, $isService, $vendorRef, $ean doesn't exits.

您的代码应该类似于:

// On product creation
add_action( 'woocommerce_new_product', 'woocommerce_create_product_callbback', 10, 4 );
function woocommerce_create_product_callbback( $product_id ) {
    // First define variables $soap and $API_KEY
    $soap       = ; // <== To be defined !
    $API_KEY    = ; // <== To be defined !
    
    // Connect
    $result     = $soap->authenticate( $API_KEY );
    $APISession = $result[1];

    if( $APISession ) {
        // Get_the WC_Product Object
        $product = wc_get_product( $product_id );
        
        // Product data
        $status             = $product->get_status();
        $name               = $product->get_name();
        $description        = $product->get_description();
        $short_descr        = $product->get_short_description();
        $parent_id          = $product->get_parent_id();
        $menu_order         = $product->get_menu_order();
        $date_created       = $product->get_date_created()->getOffsetTimestamp();
        $date_created_gmt   = $product->get_date_created()->getTimestamp();
        $slug               = $product->get_slug();
        $author_id          = get_post_field ('post_author', $product_id);
        
        // Product meta data (and post terms)
        $type               = $product->get_type();
        $tax_class          = $product->get_tax_class();
        $stock_status       = $product->get_stock_status();
        $price              = $product->get_price();
        $sku                = $product->get_sku();
        
        // Special
        $active             = $product->get_status() ==='publish' ? '1' : '0';
        $hasStocks          = $product->is_in_stock() ? '1' : '0';
         
        // Undefined (not defined in WooCommerce
        $shortName  = '';
        $tax        = '';
        $obs        = '';
        $isService  = '0';
        $vendorRef  = ''; // May be the author ID
        $ean        = ''; // May be the SKU
        
        // Send data and insert product
        $soap->insertProduct( $session, $product_id, $name, $shortName, $tax, $obs, $isService, $hasStocks, $active, $short_descr, $description, $price, $vendorRef, $ean);
    }
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.它应该有效.

Code goes in functions.php file of your active child theme (or active theme). It should works.

这篇关于在 WooCommerce 产品创建中通过肥皂自动插入产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆