好日子,我一直试图弄清楚如何使用php以编程方式选择配送类。我正在使用表单从前端创建woocommerce产品,并在表单提交时创建产品,但我可以选择任何运输类。下面的屏幕截图显示了从前端创建的产品上的送货类设置,并用ids元素选项卡显示配送类的ids(作为值)。

我使用下面的代码来选择电话托运费类别
$pShipping_Class = 25; //Where 25 is the id/value for Phone Shipping Fee Class
update_post_meta( $product_id, 'product_shipping_class', $pShipping_Class );update_post_meta适用于所有其他字段--甚至是我创建的自定义下拉字段--我能够使用update_post_meta( $product_id, '_list_of_stores', 'custom-order' );从我创建的自定义下拉字段中选择值自定义顺序,但是当我尝试对配送类进行相同的操作时,它不起作用。不知道我做错了什么。
请给我指出正确的方向。如何使用php更新传送类。我已经有身份证和鼻涕虫了。
谢谢
更新:我意识到,当我手动选择电话送货费,并点击更新产品按钮。它添加了选定的属性(即selected=" selected“),见下面的屏幕截图;

请说明如何进行此更新/选择任何运输类(由ID或段塞),因为配送类需要动态更新,以便向用户提供他们创建并添加到购物车中的产品的发货率。
发布于 2019-01-27 21:49:24
传送类不是由产品的post元数据管理的。它们是由自定义分类法
update_post_meta()管理的,因此不能使用函数。
在Woocommerce中,传送类由自定义分类法product_shipping_class管理,您需要使用条款()函数使其以编程方式工作:
$shipping_class_id = 25; // the targeted shipping class ID to be set for the product
// Set the shipping class for the product
wp_set_post_terms( $product_id, array($shipping_class_id), 'product_shipping_class' );或者,从Woocommerce 3开始,您可以这样使用WC_Product CRUD法 set_shipping_class_id():
$shipping_class_id = 25; // the targeted shipping class ID to be set for the product
$product = wc_get_product( $product_id ); // Get an instance of the WC_Product Object
$product->set_shipping_class_id( $shipping_class_id ); // Set the shipping class ID
$product->save(); // Save the product data to databasehttps://stackoverflow.com/questions/54390216
复制相似问题