Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过soap在WooCommerce产品创建中自动插入产品_Php_Wordpress_Soap_Woocommerce_Product - Fatal编程技术网

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

Php 通过soap在WooCommerce产品创建中自动插入产品,php,wordpress,soap,woocommerce,product,Php,Wordpress,Soap,Woocommerce,Product,我有一个计费程序,要将该程序连接到wordpress中的在线商店,我使用了一个插件,但现在我被要求进行以下更新: 每当我们在woocommerce上创建产品时,它也会在计费程序上自动创建 我向计费程序支持部门寻求帮助,他们给了我以下示例 $result = $soap->authenticate( $API_KEY ); $APISession = $result[1]; $ref = "002"; $designation = &

我有一个计费程序,要将该程序连接到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); $result=$soap->authenticate($API\u KEY); $APISession=$result[1]; $ref=“002”; $designation=“Produto de teste”; $shortName=“Ptest”; $tax=“23”; $obs=“teste”; $isService=“0”; $hasStocks=“0”; $active=“1”; $shortDesc=“descripao 123”; $longDesc=“描述longa,teste 123。”; $price=“100”; $vendorRef=“”; $ean=“”; $product=$soap->insertProduct($APISCESSION、$ref、$DESTATION、$shortName、$tax、$obs、$isService、$hasStocks、$active、$shortDesc、$longDesc、$price、$vendorRef、$ean); 但这不是我想要的,因为我们在这个示例中手动插入参数,我想要自动插入参数,我向计费程序的支持人员解释了这一点,但他们说他们无法帮助我,所以我在这里询问,希望有人能在这方面帮助我


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

在创建产品时会触发hook
woocommerce\u new\u product
,因此这是您需要的钩子。因此,您不需要webhook,因为soap将完成这项工作

现在,在下面的代码中,您需要定义变量
$soap
$API\u KEY

还请注意,在WooCommerce产品中,
$shortName
$tax
$obs
$isService
$vendorRef
$ean
等变量不存在

您的代码应该类似于:

// 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);
    }
}
//关于产品创建
添加操作('woocommerce\u new\u product'、'woocommerce\u create\u product\u Callback',10,4);
函数woocommerce\u create\u product\u callback($product\u id){
//首先定义变量$soap和$API_KEY
$soap=;//获取_状态();
$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\u id=get\u post\u字段('post\u author',$product\u id);
//产品元数据(和后期术语)
$type=$product->get_type();
$tax_class=$product->get_tax_class();
$stock_status=$product->get_stock_status();
$price=$product->get_price();
$sku=$product->get_sku();
//特别的
$active=$product->get_status()==='publish'?'1':'0';
$hasStocks=$product->U是否在库存中()?“1”:“0”;
//未定义(未在手册中定义)
$shortName='';
$tax='';
$obs='';
$isService='0';
$vendorRef='';//可能是作者ID
$ean='';//可能是SKU
//发送数据并插入产品
$soap->insertProduct($session、$product\u id、$name、$shortName、$tax、$obs、$isService、$hasStocks、$active、$short\u descr、$description、$price、$vendorRef、$ean);
}
}

代码进入活动子主题(或活动主题)的functions.php文件。它应该可以工作。

你是什么意思?我想知道我是否将此代码放在代码段而不是functions.php中,它将具有相同的效果?您可以使用代码段插件、主题的function.php文件或您制作的自定义插件…如果此代码回答了您的问题,请您回答,谢谢。是的,我仍然是ting这就是为什么我没有将其标记为正确的原因,我正在使用webhook,所以我不需要“添加操作”我说得对吗?@HugoAndré我认为你不需要那个webhook,因为当在WooCommerce上创建产品时,是WooCommerce将触发向计费程序插入产品的操作…所以我的完整代码应该可以做到这一点,但你需要正确设置SOAP部分和计费程序需要的每个变量使用您的
insertProduct()
方法。可能会再次询问计费程序的支持,添加指向此答案的链接…