我使用wp all import将woocommerce与ebay同步,最终我编写了此功能,以便在每个变体中添加一个sku
function set_value_for_all_vars($id) {
$prod = wc_get_product( $id );
if ( get_class( $prod ) == 'WC_Product_Variable' ) {
$value = get_post_meta( $id, '_ebay_ean', true );
$count=0;
foreach ( $prod->get_available_variations() as $var ) {
$count++;
update_post_meta( $var['variation_id'], '_ebay_ean', $value );
update_post_meta( $var['variation_id'], '_sku', $value . '-' . $var['variation_id'] . '-' . $count );
}
}
}
我的问题很简单,当wp-all-import运行更新时,此功能会更改变体sku,但我需要在创建变体时添加的skus永不改变。有人可以建议更改吗?
我认为最好的方法是,如果版本的sku不为空,则跳过更新的控件。 有人可以建议修改吗?
答案 0 :(得分:0)
function set_value_for_all_vars($id) {
$prod = wc_get_product( $id );
if ( get_class( $prod ) == 'WC_Product_Variable' ) {
$value = get_post_meta( $id, '_ebay_ean', true );
$count=0;
foreach ( $prod->get_available_variations() as $var ) {
$count++;
$ebay_product_sku = get_post_meta($var['variation_id'],'_ebay_ean',true);
if(!$ebay_product_sku)
{
update_post_meta( $var['variation_id'], '_ebay_ean', $value );
}
$woo_product_sku = get_post_meta($var['variation_id'],'_sku',true);
if(!$woo_product_sku)
{
update_post_meta( $var['variation_id'], '_sku', $value . '-' . $var['variation_id'] . '-' . $count );
}
}
}
}
答案 1 :(得分:0)
我的解决方案:
只需检查一下以确认“ sku”为空!因此,如果为空,则会写其他...退出...正确吗?
function set_value_for_all_vars($id) {
$prod = wc_get_product( $id );
if ( get_class( $prod ) == 'WC_Product_Variable' ) {
$value = get_post_meta( $id, '_ebay_ean', true );
$ebay_price = get_post_meta( $id, '_ebay_start_price', true );
$count=0;
foreach ( $prod->get_available_variations() as $var ) {
$count++;
update_post_meta( $var['variation_id'], '_ebay_start_price', $ebay_price );
$post_meta2 = get_post_meta($var['variation_id'], '_sku', true );
if (empty($post_meta2)) {
update_post_meta( $var['variation_id'], '_ebay_ean', $value );
update_post_meta( $var['variation_id'], '_sku', $value . '-' . $var['variation_id'] . '-' . $count );
}
}
}
}