php 在WooCommerce产品创建中通过soap自动插入产品

x33g5p2x  于 5个月前  发布在  PHP
关注(0)|答案(1)|浏览(64)

我有一个计费程序,并将该程序连接到我在WordPress的在线商店,我使用了一个插件,但现在我已经被要求做以下更新:

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

我向计费程序支持部门寻求帮助,他们给了我以下示例

$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);

字符串
但这不是我想要的,因为我们在这个例子中手动插入参数,我希望自动插入,我向计费程序的支持解释了这一点,但他们说他们不能帮助我,所以我在这里问,希望有人能帮助我。
注意事项:这些参数是计费程序参数,我想用来自woocommerce产品的内容填充这些参数,我使用webhook调用函数“insertProduct”

ugmeyewa

ugmeyewa1#

钩子woocommerce_new_product在产品创建时被触发,所以它是你需要的钩子,所以你不需要webhook,因为soap会完成这项工作。
在下面的代码中,你需要定义变量$soap$API_KEY
另外请注意,在WooCommerce产品中,变量$shortName$tax$obs$isService$vendorRef$ean不存在。
你的代码应该是这样的:

// On product creation
add_action( 'woocommerce_new_product', 'woocommerce_create_product_callback', 10, 2 );
function woocommerce_create_product_callback( $product_id, $product ) {
    // 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 ) {
        
        // 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文件中。它应该可以工作。

相关问题