【问题标题】:Duplicate per-product shipping extension information - WooCommerce每个产品的运输扩展信息重复 - WooCommerce
【发布时间】:2017-09-14 06:47:24
【问题描述】:

在我的 wordpress 网站上,我有 WooCommerce 和 WooCommerce 每个产品的运输扩展 (https://docs.woocommerce.com/document/per-product-shipping/)。我目前有一个问题,即每当我复制 woocommerce 产品时,不会复制每个产品的运输信息。我试图为此找到解决方案并获得了以下代码,但是我无法让它工作,任何人都可以看到下面的问题

add_action( 'woocommerce_product_duplicate', 'wdm_duplicate_pps_entries',10,2);

function wdm_duplicate_pps_entries( $new_id, $post) {
   global $wpdb;
   $id = isset( $_REQUEST['post'] ) ? absint( $_REQUEST['post'] ) : '';
   if(!empty($id)) {
   $query = "Select * From " . $wpdb->prefix . "woocommerce_per_product_shipping_rule 
   Where product_id = '" . $id . "'";

   $result = $wpdb->get_results($query);
   $table_name =  $wpdb->prefix . "woocommerce_per_product_shipping_rule";

   foreach($result as $single_result) {

      $data = array('product_id' => $new_id, 'rule_country' => $single_result->rule_country, 'rule_state' => $single_result->rule_state,'rule_postcode' => $single_result->rule_postcode,'rule_cost' => $single_result->rule_cost,'rule_item_cost' => $single_result->rule_item_cost,'rule_order' => $single_result->rule_order);

     $wpdb->insert($table_name,$data);
   }

   }
}

这是我试图复制的附加信息的一个示例 -

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    更新:Wordpress 已修复此错误

    【讨论】:

      【解决方案2】:

      以下代码完美运行。

      add_action( 'woocommerce_duplicate_product', 'wdm_duplicate_pps_entries',10,2);
      
      function wdm_duplicate_pps_entries( $new_id, $post) {
      
         global $wpdb;
         $id = isset( $_REQUEST['post'] ) ? absint( $_REQUEST['post'] ) : '';
         if(!empty($id)) {
             $query = "Select * From " . WC_PRODUCT_VENDORS_PER_PRODUCT_SHIPPING_TABLE . "
             Where product_id = '" . $id . "'";
      
             $result = $wpdb->get_results($query);
             $table_name =  WC_PRODUCT_VENDORS_PER_PRODUCT_SHIPPING_TABLE;
      
             foreach($result as $single_result) {
      
                $data = array('product_id' => $new_id, 'rule_country' => $single_result->rule_country, 'rule_state' => $single_result->rule_state,'rule_postcode' => $single_result->rule_postcode,'rule_cost' => $single_result->rule_cost,'rule_item_cost' => $single_result->rule_item_cost,'rule_order' => $single_result->rule_order);
      
               $wpdb->insert($table_name,$data);
             }
         }
      }
      

      【讨论】:

      • 此代码与问题中发布的代码有何不同?
      猜你喜欢
      • 2021-06-10
      • 2016-12-25
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 2013-06-07
      相关资源
      最近更新 更多