php - 自定义 Woocommerce 添加到购物车在 Firefox 中不起作用,但在 Chrome 中不起作用

标签 php wordpress woocommerce

我有一个问题,我有一个创建自定义产品的自定义函数,然后将其添加到购物车。

最大的问题是它在 Chrome 中完美运行,但在 Firefox 和 Safari 中却不行?有人知道为什么它只能在 Chrome 中运行吗?

我在 functions.php 中的函数

add_action('wp_loaded', 'customcart');

function customcart() {

  if (isset($_POST["addcustomcarts"])) {

    global $woocommerce;      

    $my_post = array(
      'post_title'    => 'Design selv skilt',
      'post_content'  => '<div class="col-md-12">Dette er et design selv skilt, tjek egenskaber på produktet for at se hvad kunden har bestilt.</div>',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type'     => 'product'
    );

    // Insert the post into the database
    $product_ID = wp_insert_post( $my_post );

    $filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);

    //Decode the string
    $unencodedData=base64_decode($filteredData);

    //Save the image
    file_put_contents('img' . $product_ID . '.png', $unencodedData);

    if ( $product_ID ){
      wp_set_object_terms( $product_ID, 'design-selv-skilte', 'product_cat' );
      add_post_meta($product_ID, '_regular_price', $_POST["priceInput"] );
      add_post_meta($product_ID, '_price', $_POST["priceInput"] );
      add_post_meta($product_ID, '_stock_status', 'instock' );
      //add_post_meta($product_ID, '_manage_stock', 'yes' );    
      //add_post_meta($product_ID, '_stock', '10' ); 
      add_post_meta($product_ID, '_sku', 'designselvskilt-' . $product_ID );   
      add_post_meta($product_ID, '_visibility', 'hidden' );
      add_post_meta($product_ID, 'tekst-paa-linje-1', $_POST["textInput"] );
      add_post_meta($product_ID, 'tekst-paa-linje-2', $_POST["text2Input"] );
      add_post_meta($product_ID, 'stoerrelse', $_POST["størrelseInput"] );
      add_post_meta($product_ID, 'form', $_POST["formInput"] );
      add_post_meta($product_ID, 'farve', $_POST["farveInput"] );
      add_post_meta($product_ID, 'type-skilt', $_POST["typeInput"] );
      add_post_meta($product_ID, 'fastgoering', $_POST["fastgøringInput"] );
      add_post_meta($product_ID, 'font', $_POST["fontInput"] );
      add_post_meta($product_ID, 'linje-1-font-size', $_POST["fontSizeLine1Input"] );
      add_post_meta($product_ID, 'linje-2-font-size', $_POST["fontSizeLine2Input"] );
      add_post_meta($product_ID, 'product_image_gallery', $_POST["img_val"]);
      add_post_meta($product_ID, 'product_image_url', 'img' . $product_ID . '.png');  
      require_once(ABSPATH . 'wp-admin/includes/media.php');
      require_once(ABSPATH . 'wp-admin/includes/file.php');
      require_once(ABSPATH . 'wp-admin/includes/image.php');
      $home = home_url();
      $url = $home . '/img' . $product_ID . '.png';
      $post_id = $product_ID;
      $desc = $_POST["textInput"];
      $image = media_sideload_image($url, $post_id, $desc, src );

              function getImageId( $image ) {
            // Split the $url into two parts with the wp-content directory as the separator
            $parsed_url  = explode( parse_url( WP_CONTENT_URL, PHP_URL_PATH ), $image );

            // Get the host of the current site and the host of the $url, ignoring www
            $this_host = str_ireplace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
            $file_host = str_ireplace( 'www.', '', parse_url( $image, PHP_URL_HOST ) );

            // Return nothing if there aren't any $url parts or if the current host and $url host do not match
            if ( ! isset( $parsed_url[1] ) || empty( $parsed_url[1] ) || ( $this_host != $file_host ) ) {
                return;
            }

            // Now we're going to quickly search the DB for any attachment GUID with a partial path match
            // Example: /uploads/2013/05/test-image.jpg
            global $wpdb;

            $attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}posts WHERE guid RLIKE %s;", $parsed_url[1] ) );

            // Returns null if no attachment is found
            return $attachment[0];
        }    

    set_post_thumbnail($post_id, getImageId( $image ));    
    $woocommerce->cart->get_cart_from_session();
    $woocommerce->cart->add_to_cart($product_ID);

    wp_redirect( '/kurv' ); exit;

    }
  }

}

最佳答案

我遇到了类似的问题,只是它在 Chrome/Safari 中有效,在 Firefox/Opera 中无效。原来有人将产品的状态从已发布更改为待定。更改后,所有经过测试的浏览器都可以正常工作。奇怪的是为什么会发生这种情况,因为它应该是所有服务器端脚本控制从链接添加到购物车的过程。

关于php - 自定义 Woocommerce 添加到购物车在 Firefox 中不起作用,但在 Chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33737151/

相关文章:

php - 在自定义 php 文件中使用 Woocommerce 函数

php - Unity 是否支持 PHP session ?

php - 通过 php 使用 pdo 连接到 mysql

地理定位和 WordPress : best plugin for proximity search?

php - 向 Woocommerce 产品变体下拉列表添加变体库存数量和状态

jquery - 使用WordPress将视频上传到YouTube

Javascript JSON.parse 不返回任何内容

php - 根据第一个表的内容连接表

php - 从 WooCommerce 中的电子邮件通知中删除 BACS 指令

php - 从订单 ID 获取订阅 ID