在 WooCommerce 产品创建中通过肥皂自动插入产品

问题描述 投票:0回答:1

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

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

我向计费程序支持寻求帮助来做到这一点,他们给了我以下示例

 $result = $soap->authenticate( $API_KEY );
 $APISession = $结果[1];
 
 
 $ref =“002”;
 $designation = "产品测试";
 $shortName = "Ptest";
 $税=“23”;
 $obs = "睾丸";
 $isService = "0";
 $hasStocks = "0";
 $活动=“1”;
 $shortDesc = "描述 123";
 $longDesc = "描述长,测试 123。";
 $价格=“100”;
 $vendorRef = "";
 $伊恩=“”;
 

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

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

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

php wordpress soap woocommerce product
1个回答
1
投票

钩子

woocommerce_new_product
是在创建产品时触发的,所以它就是您需要的钩子。所以你不需要网络钩子,因为肥皂就可以完成这项工作。

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

$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 文件中。应该有效。

© www.soinside.com 2019 - 2024. All rights reserved.